Python Cheat Sheet

Comments:

# This is a comment

Integer number variables:

x = 1
y = x + 1

RIGHT_ANGLE = 90

Floating point numbers (aka fractional numbers)

PO = 3.14

String variables:

message = "hello there!"

Operators

x = 1 + 2
y = x + 3
message = "hello" + " world"

x = 1 - 2
x = a * b
x = 16 / 2
x = 3 % 2  # modulo operator - useful for identifying even/odd numbers

x += 1  # same as x=x+1
x -= 1  # same as x=x-1

Endless loop:

while True:
    ...

Looping 5 times:

for _ in range(5):
    ...

Picking random numbers:

import random

# A random number between 1 and 10
x = random.randint(1, 11)

# A random number between 0.1 and 0.99
y = random.uniform(0.1, 1.0)

Example Programs