0% found this document useful (0 votes)
39 views

Main - Python Java C Cheat Sheet

This document provides a simple cheat sheet comparing basics of the C, Java, and Python programming languages. It outlines key differences in syntax, comments, variables, conditions, loops, data types, strings, containers, I/O, and functions between the three languages.

Uploaded by

cxbola
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Main - Python Java C Cheat Sheet

This document provides a simple cheat sheet comparing basics of the C, Java, and Python programming languages. It outlines key differences in syntax, comments, variables, conditions, loops, data types, strings, containers, I/O, and functions between the three languages.

Uploaded by

cxbola
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Memento Python – Java – C

29/09/2016

This simple and stupid cheat sheet aims to compare basics of C, Java, and Python languages. It is far from to be exhaustive and readers should only consider this cheat sheet as
the first step before looking for more complete documentation (Internet is your friend). It can be read as following:

• bolt font is used for terminal symbols (syntax symbols, keywords)


• italic font is used for non-terminal symbols or natural language expressions that aim to explicit a non-terminal
• [blue brackets] denote an optional part of the construct (for instance parameters of a method)
• explanations or comments about a construct use a smaller font

⇒ For example, the rule "type variableName =value ;" is a general rule that can describe snippets of code such as int foo = 42;, where type is instanciated by int, variableName
by foo and value by 42. Much more complete documentation can be found here:
• Python : https://docs.python.org/3.4/
• Java : references, doc and tutorials (https://docs.oracle.com/javase/8/) and API (http://docs.oracle.com/javase/8/docs/api/)
• C : http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html

Python Java C (C99)


# python single-line comment //some single-line comment //yet another single-line comment
Comments
no multi-line comments, but multi-line strings can be used : /* some multi-line /* yet another multi-line
” ’ some comment ” ’ comment */ comment */
Variable declaration and variable = value type variable = value ; type variable = value ;
assignation
Condition if condition : if ( condition ) { if ( condition ) {
indented instructions semi-colon separated instructions semi-colon separated instructions
[elif condition : } [else if(condition ) { } [else if(condition ) {
indented instructions] semi-colon separated instructions semi-colon separated instructions
... ... ...
[else: }] [else { }] [else {
indented instructions] semi-colon separated instructions semi-colon separated instructions
}] }]
+-/*
Operators and (Logical AND) or (Logical OR) not (Logical NOT) && (Logical AND) || (Logical OR) ! (Logical NOT)
& (AND) | (OR) ˆ (XOR) « (Shift Left) » (Shift Right) ∼ (NOT, which is the one’s complement, do not confuse it with !)
Python Java C (C99)
for variable in sequence : for (initialization ; condition ; increment ) { for (initialization ; condition ; increment ) {
Loops indented instructions semi-colon separated instructions semi-colon separated instructions
} }
The variable declaration can be done before the for instruction The variable declaration can be done before the for instruc-
tion, mandatory for C prior C99
while condition : while (condition ) { while (condition ) {
indented instructions semi-colon separated instructions semi-colon separated instructions
} }
There is no do while construct in Python do { do {
semi-colon separated instructions semi-colon separated instructions
} while (condition ); } while (condition );
Primitive types bool, int, float, str int, float, double, boolean, byte, char, long, short int, char, eventually bool expanded to _Bool (starting
from C99, stdbool.h; true and false are respectively expanded
to the integer 1 and 0)
Character Strings str (primitive type) String (almost typical Object) char*
no standard arrays type []
Container types, collections
lists: (comma-separated elements ) Collection package (java.util.Collection): All other "collections" are user-defined (lists, maps, etc.)
dictionary: {comma-separated key :value pairs } java.util.List, java.util.Map, etc.
I/O print(comma-separated printable elements ) System.out.println(String ) printf("String format ", comma-separated vari-
ables respecting the format )
input(String ) import java.util.Scanner; scanf("String format ", string pointer to store the
Note that string is the default type of the input. If you need
to enter an int, you have to add an int conversion.
... input )
Scanner userInput = new Scanner(System.in);
String firstEntry;
firstEntry = userInput.next();
Function declaration def functionname ( [comma-separated parame- returnType functionName ( [comma-separated returnType functionName ( [comma-separated
ters]): Type name pairs] ) { Type name pairs] ) {
indented instructions semi-colon separated instructions semi-colon separated instructions
} }
Function call functionName ([comma-separated parameters])
switch-case construct No dedicated construct, use if elif elif else switch(variable ) { switch(variable ) {
case value : case value :
semi-colon separated instructions
[break;] semi-colon separated instructions
[case value : [break;]
semi-colon separated instructions [case value :
[break;] ] semi-colon separated instructions
[default: [break;] ]
semi-colon separated instructions] [default:
} semi-colon separated instructions]
This construct should be avoided when OO programming. If
used, note that break breaks the execution flow and exits from }
switch without testing other cases.

You might also like