003 Python-Syntax-Cheat-Sheet-Booklet
003 Python-Syntax-Cheat-Sheet-Booklet
Input
Comments
Variables
The += Operator
www. a p p b r e w e r y . c
PYTHON CHEAT SHEET
100 DAYS OF COD
C O M P L EET E P R O F E S S I O N A L
PYTHON BOOTCAM
DATATYPES P
Integers
Strings
String Concatenation
Escaping a String
www. a p p b r e w e r y . c
PYTHON CHEAT SHEET
100 DAYS OF COD
C O M P L EET E P R O F E S S I O N A L
PYTHON BOOTCAM
#prints: She said: "Hi" P
www. a p p b r e w e r y . c
PYTHON CHEAT SHEET
100 DAYS OF COD
C O M P L EET E P R O F E S S I O N A L
PYTHON BOOTCAM
P
F-Strings
www. a p p b r e w e r y . c
PYTHON CHEAT SHEET
100 DAYS OF COD
C O M P L EET E P R O F E S S I O N A L
PYTHON BOOTCAM
MATHS P
Arithmetic Operators
You can do mathematical calculations with 3+2 #Add
Python as long as you know the right
4-1 #Subtract
operators.
2*3 #Multiply
5/2 #Divide
5**2 #Exponent
The += Operator
www. a p p b r e w e r y . c
PYTHON CHEAT SHEET
100 DAYS OF COD
C O M P L EET E P R O F E S S I O N A L
PYTHON BOOTCAM
P
ERRORS
Syntax Error
Syntax errors happen when your code
print(12 + 4))
does not make any sense to the computer.
This can happen because you've misspelt File "<stdin>", line 1
something or there's too many brackets or print(12 + 4))
a missing comma. ^
SyntaxError: unmatched ')'
Name Error
www. a p p b r e w e r y . c
PYTHON CHEAT SHEET
100 DAYS OF COD
C O M P L EET E P R O F E S S I O N A L
PYTHON BOOTCAM
P
FUNCTIONS
Creating Functions
def my_function():
This is the basic syntax for a function in
Python. It allows you to give a set of print("Hello")
instructions a name, so you can trigger it name = input("Your name:")
multiple times without having to re-
print("Hello")
write
or copy-paste it. The contents of the function
must be indented to signal that it's inside.
Calling Functions
www. a p p b r e w e r y . c
PYTHON CHEAT SHEET
100 DAYS OF COD
C O M P L EET E P R O F E S S I O N A L
PYTHON BOOTCAM
P
Variable Scope
Keyword Arguments
def divide(n1, n2):
When calling a function, you can provide
a keyword argument or simply just the
result = n1 / n2
value. #Option 1:
Using a keyword argument means that divide(10, 5)
you don't have to follow any order
#Option 2:
when providing the inputs.
www. a p p b r e w e r y . c
PYTHON CHEAT SHEET
100 DAYS OF COD
C O M P L EET E P R O F E S S I O N A L
PYTHON BOOTCAM
divide(n2=5, P
n1=10)
www. a p p b r e w e r y . c
PYTHON CHEAT SHEET
100 DAYS OF COD
C O M P L EET E P R O F E S S I O N A L
PYTHON BOOTCAM
P
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
executed, if not it will be skipped. print("Larger than 2")
Else
age = 18
This is a way to specify some code that will
be executed if a condition is false.
if age > 16:
print("Can drive")
else:
print("Don't drive")
Elif
www. a p p b r e w e r y . c
PYTHON CHEAT SHEET
100 DAYS OF COD
C O M P L EET E P R O F E S S I O N A L
PYTHON BOOTCAM
print("bring gloves") P
www. a p p b r e w e r y . c
PYTHON CHEAT SHEET
100 DAYS OF COD
C O M P L EET E P R O F E S S I O N A L
PYTHON BOOTCAM
P
and
or
age = 12
This expects either of the conditions either
side of the or to be true. Basically, both
if age < 16 or age > 200:
conditions cannot be false. print("Can't drive")
not
comparison operators
www. a p p b r e w e r y . c
PYTHON CHEAT SHEET
100 DAYS OF COD
C O M P L EET E P R O F E S S I O N A L
PYTHON BOOTCAM
== Is equal to P
!= Is not equal to
www. a p p b r e w e r y . c
PYTHON CHEAT SHEET
100 DAYS OF COD
C O M P L EET E P R O F E S S I O N A L
PYTHON BOOTCAM
P
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
_ in a For Loop
break
This keyword allows you to break free of the scores = [34, 67, 99, 105]
loop. You can use it in a for or while loop. for s in scores:
if s > 100:
print("Invalid")
www. a p p b r e w e r y . c
PYTHON CHEAT SHEET
100 DAYS OF COD
C O M P L EET E P R O F E S S I O N A L
PYTHON BOOTCAM
break P
print(s)
www. a p p b r e w e r y . c
PYTHON CHEAT SHEET
100 DAYS OF COD
C O M P L EET E P R O F E S S I O N A L
PYTHON BOOTCAM
P
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
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. a p p b r e w e r y . c
PYTHON CHEAT SHEET
100 DAYS OF COD
C O M P L EET E P R O F E S S I O N A L
PYTHON BOOTCAM
P
LIST METHODS
List Slicing
www. a p p b r e w e r y . c
PYTHON CHEAT SHEET
100 DAYS OF COD
C O M P L EET E P R O F E S S I O N A L
PYTHON BOOTCAM
P
#list[start:end]
letters =
["a","b","c","d"]
letters[1:3]
#Result: ["b", "c"]
www. a p p b r e w e r y . c
PYTHON CHEAT SHEET
100 DAYS OF COD
C O M P L EET E P R O F E S S I O N A L
PYTHON BOOTCAM
P
BUILT IN FUNCTIONS
Range
# range(start, end, step)
Often you will want to generate a range
of numbers. You can specify the start, end for i in range(6, 0, -2):
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
random module which needs to be # randint(start, end)
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 round(4.6)
5
and 5.8 becomes 6. # result 5
abs
This returns the absolute value.
Basically removing any -ve signs. abs(-4.6)
www. a p p b r e w e r y . c
PYTHON CHEAT SHEET
100 DAYS OF COD
C O M P L EET E P R O F E S S I O N A L
PYTHON BOOTCAM
# result 4.6 P
www. a p p b r e w e r y . c
PYTHON CHEAT SHEET
100 DAYS OF COD
C O M P L EET E P R O F E S S I O N A L
PYTHON BOOTCAM
P
MODULES
Importing
import random
Some modules are pre-installed with python
e.g. random/datetime n = random.randint(3, 10)
Other modules need to be installed from
pypi.org
Aliasing
Importing Everything
You can use the wildcard (*) to import from random import *
everything from a module. Beware, this list = [1, 2, 3]
usually reduces code readability.
choice(list)
www. a p p b r e w e r y . c
PYTHON CHEAT SHEET
100 DAYS OF COD
C O M P L EET E P R O F E S S I O N A L
PYTHON BOOTCAM
# More P
readable/understood
#random.choice(list)
www. a p p b r e w e r y . c
PYTHON CHEAT SHEET
100 DAYS OF COD
C O M P L EET E P R O F E S S I O N A L
PYTHON BOOTCAM
P
CLASSES & OBJECTS
my_toyota = Car()
Class Methods
www. a p p b r e w e r y . c
PYTHON CHEAT SHEET
100 DAYS OF COD
C O M P L EET E P R O F E S S I O N A L
PYTHON BOOTCAM
print(car1.colour) P
#black
www. a p p b r e w e r y . c
PYTHON CHEAT SHEET
100 DAYS OF COD
C O M P L EET E P R O F E S S I O N A L
PYTHON BOOTCAM
P
Class Inheritance
class Animal:
When you create a new class, you
def breathe(self):
can inherit the methods and
properties of another class. print("breathing")
class Fish(Animal):
def breathe(self):
super.breathe()
print("underwater")
nemo = Fish()
nemo.breathe()
#Result:
#breathing
www. a p p b r e w e r y . c
PYTHON CHEAT SHEET
100 DAYS OF COD
C O M P L EET E P R O F E S S I O N A L
PYTHON BOOTCAM
#underwater P
www. a p p b r e w e r y . c