Cheat Sheet v0.4press alt+f4 (win) or cmd+w (mac) to anonymously support cheating |
str int float bool None
| Type | Examples | Full Name |
|---|---|---|
| str | "z", "foo", "Hello!", "3.0", "int" | string |
| int | -11, -2, 0, 3, 101 | integer |
| float | -96.4, -2.0, 0.0, 0.8, 3.0, 101.1 | floating-point |
| bool | True, False | boolean |
| NoneType | None | none type |
+ - * / ** // %
| Operator | Name | Unary | Binary | Example | |||
|---|---|---|---|---|---|---|---|
| + | addition | x | x |
|
|||
| - | subtraction | x | x |
|
|||
| * | multiplication | x |
|
||||
| / | division | x |
|
||||
| ** | exponentiation | x |
|
||||
| // | floor division | x |
|
||||
| % | modulo | x |
|
== != > < >= <=
| Operator | Operation | Example Usage |
|---|---|---|
| == | is equal to (value equality) |
1 == 1 # evaluates to True 1 == 2 # evaluates to False |
| != | is not equal to (value inequality) |
1 != 2 # evaluates to True 1 != 1 # evaluates to False |
| > | greater than | 2 > 1 # evaluates to True 2 > 2 # evaluates to False 1 > 2 # evaluates to False |
| < | less than | 1 < 2 # evaluates to True 2 < 2 # evaluates to False 2 < 1 # evaluates to False |
| >= | greater than or equal |
2 >= 1 # evaluates to True 1 >= 1 # evaluates to True 0 >= 1 # evaluates to False |
| <= | less than or equal |
0 <= 1 # evaluates to True 1 <= 1 # evaluates to True 2 <= 1 # evaluates to False |
and or not
| Operator | Operation Description |
Example Usage | |||
|---|---|---|---|---|---|
| and | both are True |
|
|||
| or | at least one is True |
|
|||
| not | inverse (opposite) |
|
= += -= *= /= **= %=
variable_name
|
<assignment operator>
|
object_being_assigned
|
x = 3
y = .01
z = 'abc'
my_variable_name = 101
cat_lives = 9.0
city_name = "Austin"
chocolate = True
abc = 123
variable_name
|
<augment><assignment operator>
|
object_being_assigned
|
variable = variable + 1
is equivalent to
variable += 1
| Operator | Example | Explanation |
|---|---|---|
| = | variable = object |
binds object to variable |
| += | variable += <number> variable += "string" |
increases variable by <number> concatenates "string" |
| -= | variable -= <number> |
decreases variable by <number> |
| *= | variable *= <number> string *= <number> |
multiplies variable by <number> replicates string <number> times |
| /= | variable /= <number> |
divides variable by <number> |
| **= | variable **= <number> |
raises variable to the <number>th power |
| %= | variable %= <number> |
evaluates to variable modulo <number> |
is in
| Operator | Name | Operation Description |
Example Usage |
|---|---|---|---|
| is | identity operator |
checks for reference equality |
# evaluates to True: x = None x is None warning: avoid numbers |
| in | membership operator |
checks for membership |
# evaluates to True: 1 in ["dog", 1, 2.3] "a" in [False, "a"] -1.0 in [-1, 0, 2] # evaluates to False: 1.9 in [-1, 0, 2] |
& | ^ ~
| Operator | Name | Operation Description |
Example Usage | |||
|---|---|---|---|---|---|---|
| & | bitwise and | both are True (logical and) |
|
|||
| | | bitwise (inclusive) or |
at least one is True (logical or) |
|
|||
| ^ | bitwise xor (exclusive or) |
one is True but not both |
|
|||
| ~ | bitwise inversion (bitwise NOT) |
inverts value (logical negation) |
|
|||
| << | bitwise shift left |
bitshift first argument left by second argument |
|
|||
| >> | bitwise shift right |
bitshift first argument right by second argument |
|