Python


Datatypes

str

String

"This is a string!"

int

Integer

64

float

Float (number with decimal point)

64.0

bool

Boolean value (either True or False)

True

complex

Complex number

1j

list

Ordered, mutable sequence of (possibly duplicate) values

["A", 2]

tuple

Ordered, immutable sequence of (possily duplicate) values

("A", 2)

set

Unordered, immutable sequence of unique values

("A", 2)

dict

Ordered, mutable sequence of unique data where each datum is mapped to a value

("A", 2)

{"A" : 1, "B" : 2}

method

Arithmetic operators

+

Addition

1 + 2

Unary positive, makes number positive

+2

-

Subtraction

2 - 1

Unary negative, makes number negative

-2

*

Multiplication

2 * 3

/

Division

4 / 2

//

Floor division (rounds division down)

4 // 2

%

Modulo (remainder of division)

4 % 2

**

Exponentiation

4 ** 2

Assignment operators

=

Assignment operator

x = 2

+=

Assigns the value stored in the variable plus the value

x += 2

-=

Assigns the value stored in the variable minus the value

x -= 2

*=

Assigns the value stored in the variable times the value

x *= 2

/=

Assigns the value stored in the variable divided by the value

x /= 2

//=

a/=b is a=a//b

x //= 2

%=

a%=b is a=a%b

x %= 2

&=

a&=b is a=a&b

x &= 2

|=

a|=b is a=a|b

x |= 2

^=

a^=b is a=a^b

x ^= 2

>>=

a>>>=b is a=a>>>b

x >>= 2

<<=

a><<=b is a=a><<b

x <<= 2

Unary arithmetic and bitwise operators

Binary arithmetic and bitwise operators

Comparison operators

Other operators

Delimiters

==

Is equal to

1 == 2

!=

Is not equal to

1 != 2

>

Greater than

1 > 2

<

Less than

1 < 2

>=

Greater than or equal to

1 >= 2

<=

Less than or equal to

1 <= 2

Bitwise operators

&

Bitwise AND

0b11001010&0b00100011

|

Bitwise OR

0b11001010|0b00100011

^

Bitwise XOR

0b11001010^0b00100011

<<

Arithmetic shift left (shifts all the bits one space to the left, essentially multiplying it by 2)

0b11001010<<2

>>

Logical shift right (shifts all the bits one space to the right, essentially dividing it by 2)

0b11001010>>2

~

Bitwise NOT

~0b11001010

Boolean operators

and

AND operator

True and False

or

OR operator

True or False

not

NOT operator

not True

Identity operators

is

Checks if two objects are the exact same object, not just the same value

is not

Checks if two objects aren't the exact same object, even if they have the same value

Identity operators

is in

Checks if an object is an element of another

is not in

Checks if an object isn't an element of another

Miscellaneous operators

.

Applies method to object

list.sort()

Denotes class of an object

math.sqrt(2)

Denotes instance of an object

,

Used to separate parameters

[]

Instantiates data values of a list

list = ["A", 2]

Reference operator for a sequence container

thirdElement = list[2]

Denotes generics of a function

()

Defines a tuple

tuple = ("A", 2)

Denotes parameters of a function

print("Hello world!")

Denotes a class' superclass

class Foo(Parent):

Declares a function

{}

Instantiates a dictionary

dict = {
1 : "A",
2 : "B",
}

Instantiates a class object

->

Return annotation for functions, although doesn't force that return type

def foo(args) -> int:

:

Defines a block

if x == 1:

Defines a lambda expression

x = lambda a : a+1

""

Declares a string

"This is a string!"

""""""

Declares a triple quote string, that can span multiple lines and treats special characters as normal

"""This is a triple quote string!"""

''

Declares a string

'This is a string!'

*

The splat; indicates unknown amount of arguments for a parameter and treats them as one tuple

def foo(*args):

+

Concatenates strings

'These are '+'two strings!'

start:stop:step

List indexing operator, where start represents first index to return, stop represents the last index to return, and step means that only nth index in the new list will be kept

list[3:6]

list[::2]

list[3:6:2]

@

Decorator operator; denotes a method decorator (a function wrapped around another function)

Key word

def

Used for declaring functions

def function(args):
function code

match

Switches logic flow based on an output

match object:
case 1:
  code executed if object == 1
case 2:
  code executed if object == 2

case

Used for declaring case statement in a match statement

default

Default case in a switch statement

if

Executes code if condition is true

if condition:
code

while

Loops code while a condition is true

while condition:
 code

async def

Used for declaring a coroutine expression that can be asynchronously run

await

for

Executes code for every object in another object

for object in list:
code

try

Attempts to execute code and jumps to the subsequent except on an error

try:
code

except

Executes code on failed try statement

except:
code

finally

Executes code after try and except blocks

elif

Executes code on true condition if the last if statement was false

if condition:
code
elif condition:
code

else

Executes code when if or elif isn't executed

if condition:
code
else:
code

is

Used for testing if two variables are the same

and

Represents logical AND operator

or

Represents logical OR operator

not

Represents logical NOT operator

in

Used for referencing objects in an array or string

as

Creates an alias name for an object

import

Imports classes and modules

return

Terminates a function, returning a value

pass

Passes by an empyt block

if 1 == 1:  pass

continue

Continues to the next iteration of a loop

break

Breaks from a loop

from

Used to import specific methods from class

with

Executes code with resource strictly within the block

with open("media", "option") as object:
 code

as

Used to handle class or variable with an alias name

raise

Raises an error object

default

Default case in a match statement

global

Used to declare public variable in a method

del

Deletes an object from memory

True

Represents a true boolean value

True

False

Represents a false boolean value

False

None

Represents a null value

None

lambda

Declares a lambda expression

x = lambda a : a+1

self

References the class instance

class

Declares a class

class class:
class code

class class(parent):
class code

Statements

If-then-else

Switch

For loop

While loop

Try-catch

Throw

Return

Variable assignment

Function assignment

Class assignment

Declares a class

class class:
class code

class class(parent):
class code

Expression

Lambda

Methods

__init__

Constructor of a class (method to make objects of a class)

def __init__(self):
 code

__iter__

Returns the iterator object of a class

__next__

Returns the iterator object of a class

Argument specifiers

%s

String

%d

Integer

%f

Float

%.1f

Float with 1 decimal place, the 1 can be modified to imply more decimal places

%x

Integer in hexadecimal (lowercase a b c d e f)

%X

Integer in hexadecimal (uppercase A B C D E F)

Escape characters

\a

Bell or alert, has hex code 0x07

\b

Backspace, has hex code 0x08

\cx

Control-X

\C-x

Control-X

\e

Escape, has hex code 0x1b

\f

\M-\C-x

\n

Newline, has hex code 0x0a

\nnn

\r

\s

Space, has hex code 0x20

\t

Space, has hex code 0x20

\v

Vertical tab, has hex code 0x0b

\x

\xnn

Commenting

#

Used to tell compiler the rest of this line is not to be compiled

#This isn't executed