Python Syntax Cheat Sheet Booklet
Python Syntax Cheat Sheet Booklet
BASICS
Input
Comments
Variables
The += Operator
www.appbrewery.com
PYTHON CHEAT SHEET
100 DAYS OF CODE
COMPLETE PROFESSIONAL
PYTHON BOOTCAMP
DATA TYPES
Integers
Strings
String Concatenation
Escaping a String
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
MATHS
Arithmetic Operators
You can do mathematical calculations with 3+2 #Add
Python as long as you know the right
operators.
4-1 #Subtract
2*3 #Multiply
5/2 #Divide
5**2 #Exponent
The += Operator
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
FUNCTIONS
Creating Functions
def my_function():
This is the basic syntax for a function in
print("Hello")
Python. It allows you to give a set of
instructions a name, so you can trigger it name = input("Your name:")
multiple times without having to re-write print("Hello
or copy-paste it. The contents of the function
must be indented to signal that it's inside.
Calling Functions
my_function()
You activate the function by calling it.
my_function()
This is simply done by writing the name of
the function followed by a set of round #The function my_function
brackets. This allows you to determine #will run twice.
when to trigger the function and how
many times.
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
CONDITIONALS
If
n = 5
This is the basic syntax to test if a condition if n > 2:
is true. If so, the indented code will be
print("Larger than 2")
executed, if not it will be skipped.
Else
age = 18
This is a way to specify some code that will be if age > 16:
executed if a condition is false.
print("Can drive")
else:
print("Don't drive")
Elif
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
LOOPS
While Loop
n = 1
This is a loop that will keep repeating itself while n < 100:
until the while condition becomes false.
n += 1
For Loop
all_fruits = ["apple",
For loops give you more control than "banana", "orange"]
while loops. You can loop through anything
for fruit in all_fruits:
that is iterable. e.g. a range, a list, a dictionary
or tuple. print(fruit)
_ in a For Loop
break
continue
n = 1
This keyword allows you to skip this iteration while n < 100:
of the loop and go to the next. The loop will
if n % 2 == 0:
still continue, but it will start from the top.
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
LIST METHODS
List Index
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 4
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
MODULES
Importing
import random
Some modules are pre-installed with python
n = random.randint(3, 10)
e.g. random/datetime
Other modules need to be installed from
pypi.org
Aliasing
import random as r
You can use the as keyword to give
n = r.randint(1, 5)
your module a different name.
Importing Everything
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
PYTHON CHEAT SHEET
100 DAYS OF CODE
COMPLETE PROFESSIONAL
PYTHON BOOTCAMP
Class Properties
Class Inheritance
class Animal:
When you create a new class, you can def breathe(self):
inherit the methods and properties
print("breathing")
of another class.
class Fish(Animal):
def breathe(self):
super.breathe()
print("underwater")
nemo = Fish()
nemo.breathe()
#Result:
#breathing
#underwater
www.appbrewery.com