Python Cheat Sheets
Python Cheat Sheets
Python Cheat Sheets
BASICS
Input
Comments
Variables
The += Operator
www.appbrewery.com
PYTHON CHEAT SHEET
100 DAYS OF CODE
COMPLETE PROFESSIONAL
PYTHON BOOTCAMP
F-Strings
www.appbrewery.com
PYTHON CHEAT SHEET
100 DAYS OF CODE
COMPLETE PROFESSIONAL
PYTHON BOOTCAMP
ERRORS
Syntax Error
Syntax errors happen when your code print(12 + 4))
does not make any sense to the computer. File "<stdin>", line 1
This can happen because you've misspelt
print(12 + 4))
something or there's too many brackets or
a missing comma. ^
SyntaxError: unmatched ')'
Name Error
www.appbrewery.com
PYTHON CHEAT SHEET
100 DAYS OF CODE
COMPLETE PROFESSIONAL
PYTHON BOOTCAMP
Variable Scope
n = 2
Variables created inside a function are def my_function():
destroyed once the function has executed.
n = 3
The location (line of code) that you use
a variable will determine its value. print(n)
Here n is 2 but inside my_function() n is 3.
So printing n inside and outside the function
print(n) #Prints 2
will determine its value.
my_function() #Prints 3
Keyword Arguments
def divide(n1, n2):
When calling a function, you can provide result = n1 / n2
a keyword argument or simply just the
#Option 1:
value.
Using a keyword argument means that divide(10, 5)
you don't have to follow any order #Option 2:
when providing the inputs.
divide(n2=5, n1=10)
www.appbrewery.com
PYTHON CHEAT SHEET
100 DAYS OF CODE
COMPLETE PROFESSIONAL
PYTHON BOOTCAMP
and
s = 58
This expects both conditions either side if s < 60 and s > 50:
of the and to be true.
print("Your grade is C")
or
age = 12
This expects either of the conditions either if age < 16 or age > 200:
side of the or to be true. Basically, both
print("Can't drive")
conditions cannot be false.
not
comparison operators
www.appbrewery.com
PYTHON CHEAT SHEET
100 DAYS OF CODE
COMPLETE PROFESSIONAL
PYTHON BOOTCAMP
continue n = 0
while n < 100:
This keyword allows you to skip this iteration
of the loop and go to the next. The loop will
n += 1
still continue, but it will start from the top. if n % 2 == 0:
continue
print(n)
#Prints all the odd numbers
Infinite Loops
while 5 > 1:
Sometimes, the condition you are checking print("I'm a survivor")
to see if the loop should continue never
becomes false. In this case, the loop will
continue for eternity (or until your computer
stops it). This is more common with while
loops.
www.appbrewery.com
PYTHON CHEAT SHEET
100 DAYS OF CODE
COMPLETE PROFESSIONAL
PYTHON BOOTCAMP
BUILT IN FUNCTIONS
Range
# range(start, end, step)
Often you will want to generate a range
for i in range(6, 0, -2):
of numbers. You can specify the start, end
and step. print(i)
Start is included, but end is excluded:
start <= range < end
# result: 6, 4, 2
# 0 is not included.
Randomisation
import random
The random functions come from the
# randint(start, end)
random module which needs to be
imported. n = random.randint(2, 5)
In this case, the start and end are both #n can be 2, 3, 4 or 5.
included
start <= randint <= end
Round
This does a mathematical round.
So 3.1 becomes 3, 4.5 becomes 5 round(4.6)
and 5.8 becomes 6. # result 5
abs
This returns the absolute value.
Basically removing any -ve signs. abs(-4.6)
# result 4.6
www.appbrewery.com
PYTHON CHEAT SHEET
100 DAYS OF CODE
COMPLETE PROFESSIONAL
PYTHON BOOTCAMP
my_toyota = Car()
Class Methods
class Car:
You can create a function that belongs def drive(self):
to a class, this is known as a method.
print("move")
my_honda = Car()
my_honda.drive()
Class Variables
class Car:
You can create a varaiable in a class. colour = "black"
The value of the variable will be available
car1 = Car()
to all objects created from the class.
print(car1.colour) #black
www.appbrewery.com