SlideShare a Scribd company logo
1 of 16
Download to read offline
B A S I C S
PYTHON CHEAT SHEET
1 0 0 D A Y S O F C O D E
C O M P L E T E P R O F E S S I O N A L
P Y T H O N B O O T C A M P
www.appbrewery.com
Print
Prints a string into the console. print("Hello World")
Input
Prints a string into the console,
and asks the user for a string input.
input("What's your name")
Comments
Adding a # symbol in font of text
lets you make comments on a line of code.
The computer will ignore your comments.
#This is a comment
print("This is code")
Variables
A variable give a name to a piece of data.
Like a box with a label, it tells you what's
inside the box.
my_name = "Angela"
my_age = 12
The += Operator
This is a convient way of saying: "take the
previous value and add to it.
my_age = 12
my_age += 4
#my_age is now 16
D A T A T Y P E S
PYTHON CHEAT SHEET
1 0 0 D A Y S O F C O D E
C O M P L E T E P R O F E S S I O N A L
P Y T H O N B O O T C A M P
www.appbrewery.com
Integers
Integers are whole numbers. my_number = 354
Floating Point Numbers
Floats are numbers with decimal places.
When you do a calculation that results in
a fraction e.g. 4 ÷ 3 the result will always be
a floating point number.
my_float = 3.14159
Strings
A string is just a string of characters.
It should be surrounded by double quotes.
my_string = "Hello"
String Concatenation
You can add strings to string to create
a new string. This is called concatenation.
It results in a new string.
"Hello" + "Angela"
#becomes "HelloAngela"
Escaping a String
Because the double quote is special, it
denotes a string, if you want to use it in
a string, you need to escape it with a ""
speech = "She said: "Hi""
print(speech)
#prints: She said: "Hi"
www.appbrewery.com
Converting Data Types
You can convert a variable from 1 data
type to another.
Converting to float:
float()
Converting to int:
int()
Converting to string:
str()
n = 354
new_n = float(n)
print(new_n) #result 354.0
Checking Data Types
You can use the type() function
to check what is the data type of a
particular variable.
n = 3.14159
type(n) #result float
PYTHON CHEAT SHEET
1 0 0 D A Y S O F C O D E
C O M P L E T E P R O F E S S I O N A L
P Y T H O N B O O T C A M P
F-Strings
You can insert a variable into a string
using f-strings.
The syntax is simple, just insert the variable
in-between a set of curly braces {}.
days = 365
print(f"There are {days}
in a year")
PYTHON CHEAT SHEET
1 0 0 D A Y S O F C O D E
C O M P L E T E P R O F E S S I O N A L
P Y T H O N B O O T C A M P
www.appbrewery.com
The += Operator
This is a convenient way to modify a variable.
It takes the existing value in a variable
and adds to it.
You can also use any of the other
mathematical operators e.g. -= or *=
my_number = 4
my_number += 2
#result is 6
The Modulo Operator
Often you'll want to know what is the
remainder after a division.
e.g. 4 ÷ 2 = 2 with no remainder
but 5 ÷ 2 = 2 with 1 remainder
The modulo does not give you the result
of the division, just the remainder.
It can be really helpful in certain situations,
e.g. figuring out if a number is odd or even.
5 % 2
#result is 1
M A T H S
Arithmetic Operators
You can do mathematical calculations with
Python as long as you know the right
operators.
3+2 #Add
4-1 #Subtract
2*3 #Multiply
5/2 #Divide
5**2 #Exponent
PYTHON CHEAT SHEET
1 0 0 D A Y S O F C O D E
C O M P L E T E P R O F E S S I O N A L
P Y T H O N B O O T C A M P
www.appbrewery.com
Name Error
This happens when there is a variable
with a name that the computer
does not recognise. It's usually because
you've misspelt the name of a variable
you created earlier.
Note: variable names are case sensitive!
my_number = 4
my_Number + 2
Traceback (most recent call
last): File "<stdin>", line 1,
NameError: name 'my_Number'
is not defined
Zero Division Error
This happens when you try to divide by zero,
This is something that is mathematically
impossible so Python will also complain.
5 % 0
Traceback (most recent call
last): File "<stdin>", line 1,
ZeroDivisionError: integer
division or modulo by zero
E R R O R S
Syntax Error
Syntax errors happen when your code
does not make any sense to the computer.
This can happen because you've misspelt
something or there's too many brackets or
a missing comma.
print(12 + 4))
File "<stdin>", line 1
print(12 + 4))
^
SyntaxError: unmatched ')'
PYTHON CHEAT SHEET
1 0 0 D A Y S O F C O D E
C O M P L E T E P R O F E S S I O N A L
P Y T H O N B O O T C A M P
www.appbrewery.com
Functions with Inputs
In addition to simple functions, you can
give the function an input, this way, each time
the function can do something different
depending on the input. It makes your
function more useful and re-usable.
def add(n1, n2):
print(n1 + n2)
add(2, 3)
F U N C T I O N S
Creating Functions
This is the basic syntax for a function in
Python. It allows you to give a set of
instructions a name, so you can trigger it
multiple times without having to re-write
or copy-paste it. The contents of the function
must be indented to signal that it's inside.
def my_function():
print("Hello")
name = input("Your name:")
print("Hello")
Calling Functions
You activate the function by calling it.
This is simply done by writing the name of
the function followed by a set of round
brackets. This allows you to determine
when to trigger the function and how
many times.
my_function()
my_function()
#The function my_function
#will run twice.
PYTHON CHEAT SHEET
1 0 0 D A Y S O F C O D E
C O M P L E T E P R O F E S S I O N A L
P Y T H O N B O O T C A M P
www.appbrewery.com
Variable Scope
Variables created inside a function are
destroyed once the function has executed.
The location (line of code) that you use
a variable will determine its value.
Here n is 2 but inside my_function() n is 3.
So printing n inside and outside the function
will determine its value.
n = 2
def my_function():
n = 3
print(n)
print(n) #Prints 2
my_function() #Prints 3
Functions with Outputs
In addition to inputs, a function can also have
an output. The output value is proceeded by
the keyword "return".
This allows you to store the result from a
function.
def add(n1, n2):
return n1 + n2
result = add(2, 3)
Keyword Arguments
When calling a function, you can provide
a keyword argument or simply just the
value.
Using a keyword argument means that
you don't have to follow any order
when providing the inputs.
def divide(n1, n2):
result = n1 / n2
#Option 1:
divide(10, 5)
#Option 2:
divide(n2=5, n1=10)
PYTHON CHEAT SHEET
1 0 0 D A Y S O F C O D E
C O M P L E T E P R O F E S S I O N A L
P Y T H O N B O O T C A M P
www.appbrewery.com
Elif
In addition to the initial If statement
condition, you can add extra conditions to
test if the first condition is false.
Once an elif condition is true, the rest of
the elif conditions are no longer checked
and are skipped.
weather = "sunny"
if weather == "rain":
print("bring umbrella")
elif weather == "sunny":
print("bring sunglasses")
elif weather == "snow":
print("bring gloves")
C O N D I T I O N A L S
If
This is the basic syntax to test if a condition
is true. If so, the indented code will be
executed, if not it will be skipped.
n = 5
if n > 2:
print("Larger than 2")
Else
This is a way to specify some code that will be
executed if a condition is false.
age = 18
if age > 16:
print("Can drive")
else:
print("Don't drive")
PYTHON CHEAT SHEET
1 0 0 D A Y S O F C O D E
C O M P L E T E P R O F E S S I O N A L
P Y T H O N B O O T C A M P
www.appbrewery.com
not
This will flip the original result of the
condition. e.g. if it was true then it's now
false.
if not 3 > 1:
print("something")
#Will not be printed.
and
This expects both conditions either side
of the and to be true.
s = 58
if s < 60 and s > 50:
print("Your grade is C")
or
This expects either of the conditions either
side of the or to be true. Basically, both
conditions cannot be false.
age = 12
if age < 16 or age > 200:
print("Can't drive")
comparison operators
These mathematical comparison operators
allow you to refine your conditional checks.
> Greater than
< Lesser than
>= Greater than or equal to
<= Lesser than or equal to
== Is equal to
!= Is not equal to
PYTHON CHEAT SHEET
1 0 0 D A Y S O F C O D E
C O M P L E T E P R O F E S S I O N A L
P Y T H O N B O O T C A M P
www.appbrewery.com
_ in a For Loop
If the value your for loop is iterating through,
e.g. the number in the range, or the item in
the list is not needed, you can replace it with
an underscore.
for _ in range(100):
#Do something 100 times.
L O O P S
While Loop
This is a loop that will keep repeating itself
until the while condition becomes false.
n = 1
while n < 100:
n += 1
For Loop
For loops give you more control than
while loops. You can loop through anything
that is iterable. e.g. a range, a list, a dictionary
or tuple.
all_fruits = ["apple",
"banana", "orange"]
for fruit in all_fruits:
print(fruit)
break
This keyword allows you to break free of the
loop. You can use it in a for or while loop.
scores = [34, 67, 99, 105]
for s in scores:
if s > 100:
print("Invalid")
break
print(s)
PYTHON CHEAT SHEET
1 0 0 D A Y S O F C O D E
C O M P L E T E P R O F E S S I O N A L
P Y T H O N B O O T C A M P
www.appbrewery.com
continue
This keyword allows you to skip this iteration
of the loop and go to the next. The loop will
still continue, but it will start from the top.
n = 1
while n < 100:
if n % 2 == 0:
continue
print(n)
#Prints all the odd numbers
Infinite Loops
Sometimes, the condition you are checking
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.
while 5 > 1:
print("I'm a survivor")
PYTHON CHEAT SHEET
1 0 0 D A Y S O F C O D E
C O M P L E T E P R O F E S S I O N A L
P Y T H O N B O O T C A M P
www.appbrewery.com
List Index
To get hold of a particular item from a
list you can use its index number.
This number can also be negative, if you
want to start counting from the end of the
list.
letters = ["a", "b", "c"]
letters[0]
#Result:"a"
letters[-1]
#Result: "c"
L I S T M E T H O D S
Adding Lists Together
You can extend a list with another list by
using the extend keyword, or the + symbol.
list1 = [1, 2, 3]
list2 = [9, 8, 7]
new_list = list1 + list2
list1 += list2
Adding an Item to a List
If you just want to add a single item to a
list, you need to use the .append() method.
all_fruits = ["apple",
"banana", "orange"]
all_fruits.append("pear")
List Slicing
Using the list index and the colon symbol
you can slice up a list to get only the
portion you want.
Start is included, but end is not.
#list[start:end]
letters = ["a","b","c","d"]
letters[1:3]
#Result: ["b", "c"]
PYTHON CHEAT SHEET
1 0 0 D A Y S O F C O D E
C O M P L E T E P R O F E S S I O N A L
P Y T H O N B O O T C A M P
www.appbrewery.com
Round
This does a mathematical round.
So 3.1 becomes 3, 4.5 becomes 5
and 5.8 becomes 6.
round(4.6)
# result 5
B U I L T I N F U N C T I O N S
Randomisation
The random functions come from the
random module which needs to be
imported.
In this case, the start and end are both
included
start <= randint <= end
import random
# randint(start, end)
n = random.randint(2, 5)
#n can be 2, 3, 4 or 5.
Range
Often you will want to generate a range
of numbers. You can specify the start, end
and step.
Start is included, but end is excluded:
start >= range < end
# range(start, end, step)
for i in range(6, 0, -2):
print(i)
# result: 6, 4, 2
# 0 is not included.
abs
This returns the absolute value.
Basically removing any -ve signs. abs(-4.6)
# result 4.6
PYTHON CHEAT SHEET
1 0 0 D A Y S O F C O D E
C O M P L E T E P R O F E S S I O N A L
P Y T H O N B O O T C A M P
www.appbrewery.com
M O D U L E S
Aliasing
You can use the as keyword to give
your module a different name.
import random as r
n = r.randint(1, 5)
Importing
Some modules are pre-installed with python
e.g. random/datetime
Other modules need to be installed from
pypi.org
import random
n = random.randint(3, 10)
Importing Everything
You can use the wildcard (*) to import
everything from a module. Beware, this
usually reduces code readability.
from random import *
list = [1, 2, 3]
choice(list)
# More readable/understood
#random.choice(list)
Importing from modules
You can import a specific thing from a
module. e.g. a function/class/constant
You do this with the from keyword.
It can save you from having to type the same
thing many times.
from random import randint
n = randint(1, 5)
PYTHON CHEAT SHEET
1 0 0 D A Y S O F C O D E
C O M P L E T E P R O F E S S I O N A L
P Y T H O N B O O T C A M P
www.appbrewery.com
C L A S S E S & O B J E C T S
Creating an Object from a Class
You can create a new instance of an object
by using the class name + ()
class Car:
pass
my_toyota = Car()
Creating a Python Class
You create a class using the class keyword.
Note, class names in Python are PascalCased.
So to create an empty class
class MyClass:
#define class
Class Methods
You can create a function that belongs
to a class, this is known as a method.
class Car:
def drive(self):
print("move")
my_honda = Car()
my_honda.drive()
Class Variables
You can create a varaiable in a class.
The value of the variable will be available
to all objects created from the class.
class Car:
colour = "black"
car1 = Car()
print(car1.colour) #black
PYTHON CHEAT SHEET
1 0 0 D A Y S O F C O D E
C O M P L E T E P R O F E S S I O N A L
P Y T H O N B O O T C A M P
www.appbrewery.com
The __init__ method
The init method is called every time a new
object is created from the class.
class Car:
def __init__(self):
print("Building car")
my_toyota = Car()
#You will see "building car"
#printed.
Class Inheritance
When you create a new class, you can
inherit the methods and properties
of another class.
class Animal:
def breathe(self):
print("breathing")
class Fish(Animal):
def breathe(self):
super.breathe()
print("underwater")
nemo = Fish()
nemo.breathe()
#Result:
#breathing
#underwater
Class Properties
You can create a variable in the init() of
a class so that all objects created from the
class has access to that variable.
class Car:
def __init__(self, name):
self.name = "Jimmy"

More Related Content

What's hot (11)

Basic for Loop in C
Basic for Loop in CBasic for Loop in C
Basic for Loop in C
 
Procedure n functions
Procedure n functionsProcedure n functions
Procedure n functions
 
Loops in c
Loops in cLoops in c
Loops in c
 
Subnetting - Perhitungan Subnet
Subnetting - Perhitungan SubnetSubnetting - Perhitungan Subnet
Subnetting - Perhitungan Subnet
 
Dts x dicoding #1 memulai pemrograman kotlin
Dts x dicoding #1 memulai pemrograman kotlinDts x dicoding #1 memulai pemrograman kotlin
Dts x dicoding #1 memulai pemrograman kotlin
 
02 c++ Array Pointer
02 c++ Array Pointer02 c++ Array Pointer
02 c++ Array Pointer
 
Control statements
Control statementsControl statements
Control statements
 
MySQL查询优化浅析
MySQL查询优化浅析MySQL查询优化浅析
MySQL查询优化浅析
 
C basics
C   basicsC   basics
C basics
 
Kamailio - API Based SIP Routing
Kamailio - API Based SIP RoutingKamailio - API Based SIP Routing
Kamailio - API Based SIP Routing
 
FreeSWITCH as a Kickass SBC
FreeSWITCH as a Kickass SBCFreeSWITCH as a Kickass SBC
FreeSWITCH as a Kickass SBC
 

Similar to Python+Syntax+Cheat+Sheet+Booklet.pdf

Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa Thapa
 
Python programing
Python programingPython programing
Python programinghamzagame
 
1_1_python-course-notes-sections-1-7.pdf
1_1_python-course-notes-sections-1-7.pdf1_1_python-course-notes-sections-1-7.pdf
1_1_python-course-notes-sections-1-7.pdfJavier Crisostomo
 
loopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfloopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfDheeravathBinduMadha
 
Introduction to Python - Training for Kids
Introduction to Python - Training for KidsIntroduction to Python - Training for Kids
Introduction to Python - Training for KidsAimee Maree Forsstrom
 
Control structure of c
Control structure of cControl structure of c
Control structure of cKomal Kotak
 
E1 – FundamentalsPlease refer to announcements for details about.docx
E1 – FundamentalsPlease refer to announcements for details about.docxE1 – FundamentalsPlease refer to announcements for details about.docx
E1 – FundamentalsPlease refer to announcements for details about.docxjacksnathalie
 
Types For Frontend Developers
Types For Frontend DevelopersTypes For Frontend Developers
Types For Frontend DevelopersJesse Williamson
 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.comGreen Ecosystem
 

Similar to Python+Syntax+Cheat+Sheet+Booklet.pdf (20)

Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptx
 
Python programing
Python programingPython programing
Python programing
 
pythonQuick.pdf
pythonQuick.pdfpythonQuick.pdf
pythonQuick.pdf
 
python notes.pdf
python notes.pdfpython notes.pdf
python notes.pdf
 
python 34💭.pdf
python 34💭.pdfpython 34💭.pdf
python 34💭.pdf
 
C tutorial
C tutorialC tutorial
C tutorial
 
Python Control structures
Python Control structuresPython Control structures
Python Control structures
 
1_1_python-course-notes-sections-1-7.pdf
1_1_python-course-notes-sections-1-7.pdf1_1_python-course-notes-sections-1-7.pdf
1_1_python-course-notes-sections-1-7.pdf
 
C code examples
C code examplesC code examples
C code examples
 
loopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfloopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdf
 
Introduction to Python - Training for Kids
Introduction to Python - Training for KidsIntroduction to Python - Training for Kids
Introduction to Python - Training for Kids
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
E1 – FundamentalsPlease refer to announcements for details about.docx
E1 – FundamentalsPlease refer to announcements for details about.docxE1 – FundamentalsPlease refer to announcements for details about.docx
E1 – FundamentalsPlease refer to announcements for details about.docx
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 
C important questions
C important questionsC important questions
C important questions
 
Types For Frontend Developers
Types For Frontend DevelopersTypes For Frontend Developers
Types For Frontend Developers
 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.com
 
Python for Beginners(v2)
Python for Beginners(v2)Python for Beginners(v2)
Python for Beginners(v2)
 
Loops in Python.pptx
Loops in Python.pptxLoops in Python.pptx
Loops in Python.pptx
 
Pythonintro
PythonintroPythonintro
Pythonintro
 

Recently uploaded

Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Call Girls in Nagpur High Profile
 
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service BhiwandiVIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service BhiwandiSuhani Kapoor
 
CBD Belapur Individual Call Girls In 08976425520 Panvel Only Genuine Call Girls
CBD Belapur Individual Call Girls In 08976425520 Panvel Only Genuine Call GirlsCBD Belapur Individual Call Girls In 08976425520 Panvel Only Genuine Call Girls
CBD Belapur Individual Call Girls In 08976425520 Panvel Only Genuine Call Girlsmodelanjalisharma4
 
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...Call Girls in Nagpur High Profile
 
Punjabi Housewife Call Girls Service Gomti Nagar \ 9548273370 Indian Call Gir...
Punjabi Housewife Call Girls Service Gomti Nagar \ 9548273370 Indian Call Gir...Punjabi Housewife Call Girls Service Gomti Nagar \ 9548273370 Indian Call Gir...
Punjabi Housewife Call Girls Service Gomti Nagar \ 9548273370 Indian Call Gir...nagunakhan
 
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...Yantram Animation Studio Corporation
 
VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...
VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...
VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...Suhani Kapoor
 
Peaches App development presentation deck
Peaches App development presentation deckPeaches App development presentation deck
Peaches App development presentation decktbatkhuu1
 
Fashion trends before and after covid.pptx
Fashion trends before and after covid.pptxFashion trends before and after covid.pptx
Fashion trends before and after covid.pptxVanshNarang19
 
The history of music videos a level presentation
The history of music videos a level presentationThe history of music videos a level presentation
The history of music videos a level presentationamedia6
 
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...Amil baba
 
VIP Kolkata Call Girl Gariahat 👉 8250192130 Available With Room
VIP Kolkata Call Girl Gariahat 👉 8250192130  Available With RoomVIP Kolkata Call Girl Gariahat 👉 8250192130  Available With Room
VIP Kolkata Call Girl Gariahat 👉 8250192130 Available With Roomdivyansh0kumar0
 
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdfThe_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdfAmirYakdi
 
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceanilsa9823
 
Petrosains Drama Competition (PSDC).pptx
Petrosains Drama Competition (PSDC).pptxPetrosains Drama Competition (PSDC).pptx
Petrosains Drama Competition (PSDC).pptxIgnatiusAbrahamBalin
 
Nepali Escort Girl Gomti Nagar \ 9548273370 Indian Call Girls Service Lucknow...
Nepali Escort Girl Gomti Nagar \ 9548273370 Indian Call Girls Service Lucknow...Nepali Escort Girl Gomti Nagar \ 9548273370 Indian Call Girls Service Lucknow...
Nepali Escort Girl Gomti Nagar \ 9548273370 Indian Call Girls Service Lucknow...nagunakhan
 
Design Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptxDesign Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptxTusharBahuguna2
 
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...ranjana rawat
 
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Delhi Call girls
 

Recently uploaded (20)

Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
 
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service BhiwandiVIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
 
CBD Belapur Individual Call Girls In 08976425520 Panvel Only Genuine Call Girls
CBD Belapur Individual Call Girls In 08976425520 Panvel Only Genuine Call GirlsCBD Belapur Individual Call Girls In 08976425520 Panvel Only Genuine Call Girls
CBD Belapur Individual Call Girls In 08976425520 Panvel Only Genuine Call Girls
 
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
 
Punjabi Housewife Call Girls Service Gomti Nagar \ 9548273370 Indian Call Gir...
Punjabi Housewife Call Girls Service Gomti Nagar \ 9548273370 Indian Call Gir...Punjabi Housewife Call Girls Service Gomti Nagar \ 9548273370 Indian Call Gir...
Punjabi Housewife Call Girls Service Gomti Nagar \ 9548273370 Indian Call Gir...
 
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
 
VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...
VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...
VIP Russian Call Girls in Saharanpur Deepika 8250192130 Independent Escort Se...
 
Peaches App development presentation deck
Peaches App development presentation deckPeaches App development presentation deck
Peaches App development presentation deck
 
Fashion trends before and after covid.pptx
Fashion trends before and after covid.pptxFashion trends before and after covid.pptx
Fashion trends before and after covid.pptx
 
The history of music videos a level presentation
The history of music videos a level presentationThe history of music videos a level presentation
The history of music videos a level presentation
 
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...
 
VIP Kolkata Call Girl Gariahat 👉 8250192130 Available With Room
VIP Kolkata Call Girl Gariahat 👉 8250192130  Available With RoomVIP Kolkata Call Girl Gariahat 👉 8250192130  Available With Room
VIP Kolkata Call Girl Gariahat 👉 8250192130 Available With Room
 
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdfThe_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
 
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
 
Petrosains Drama Competition (PSDC).pptx
Petrosains Drama Competition (PSDC).pptxPetrosains Drama Competition (PSDC).pptx
Petrosains Drama Competition (PSDC).pptx
 
Nepali Escort Girl Gomti Nagar \ 9548273370 Indian Call Girls Service Lucknow...
Nepali Escort Girl Gomti Nagar \ 9548273370 Indian Call Girls Service Lucknow...Nepali Escort Girl Gomti Nagar \ 9548273370 Indian Call Girls Service Lucknow...
Nepali Escort Girl Gomti Nagar \ 9548273370 Indian Call Girls Service Lucknow...
 
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
 
Design Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptxDesign Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptx
 
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
 
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
 

Python+Syntax+Cheat+Sheet+Booklet.pdf

  • 1. B A S I C S PYTHON CHEAT SHEET 1 0 0 D A Y S O F C O D E C O M P L E T E P R O F E S S I O N A L P Y T H O N B O O T C A M P www.appbrewery.com Print Prints a string into the console. print("Hello World") Input Prints a string into the console, and asks the user for a string input. input("What's your name") Comments Adding a # symbol in font of text lets you make comments on a line of code. The computer will ignore your comments. #This is a comment print("This is code") Variables A variable give a name to a piece of data. Like a box with a label, it tells you what's inside the box. my_name = "Angela" my_age = 12 The += Operator This is a convient way of saying: "take the previous value and add to it. my_age = 12 my_age += 4 #my_age is now 16
  • 2. D A T A T Y P E S PYTHON CHEAT SHEET 1 0 0 D A Y S O F C O D E C O M P L E T E P R O F E S S I O N A L P Y T H O N B O O T C A M P www.appbrewery.com Integers Integers are whole numbers. my_number = 354 Floating Point Numbers Floats are numbers with decimal places. When you do a calculation that results in a fraction e.g. 4 ÷ 3 the result will always be a floating point number. my_float = 3.14159 Strings A string is just a string of characters. It should be surrounded by double quotes. my_string = "Hello" String Concatenation You can add strings to string to create a new string. This is called concatenation. It results in a new string. "Hello" + "Angela" #becomes "HelloAngela" Escaping a String Because the double quote is special, it denotes a string, if you want to use it in a string, you need to escape it with a "" speech = "She said: "Hi"" print(speech) #prints: She said: "Hi"
  • 3. www.appbrewery.com Converting Data Types You can convert a variable from 1 data type to another. Converting to float: float() Converting to int: int() Converting to string: str() n = 354 new_n = float(n) print(new_n) #result 354.0 Checking Data Types You can use the type() function to check what is the data type of a particular variable. n = 3.14159 type(n) #result float PYTHON CHEAT SHEET 1 0 0 D A Y S O F C O D E C O M P L E T E P R O F E S S I O N A L P Y T H O N B O O T C A M P F-Strings You can insert a variable into a string using f-strings. The syntax is simple, just insert the variable in-between a set of curly braces {}. days = 365 print(f"There are {days} in a year")
  • 4. PYTHON CHEAT SHEET 1 0 0 D A Y S O F C O D E C O M P L E T E P R O F E S S I O N A L P Y T H O N B O O T C A M P www.appbrewery.com The += Operator This is a convenient way to modify a variable. It takes the existing value in a variable and adds to it. You can also use any of the other mathematical operators e.g. -= or *= my_number = 4 my_number += 2 #result is 6 The Modulo Operator Often you'll want to know what is the remainder after a division. e.g. 4 ÷ 2 = 2 with no remainder but 5 ÷ 2 = 2 with 1 remainder The modulo does not give you the result of the division, just the remainder. It can be really helpful in certain situations, e.g. figuring out if a number is odd or even. 5 % 2 #result is 1 M A T H S Arithmetic Operators You can do mathematical calculations with Python as long as you know the right operators. 3+2 #Add 4-1 #Subtract 2*3 #Multiply 5/2 #Divide 5**2 #Exponent
  • 5. PYTHON CHEAT SHEET 1 0 0 D A Y S O F C O D E C O M P L E T E P R O F E S S I O N A L P Y T H O N B O O T C A M P www.appbrewery.com Name Error This happens when there is a variable with a name that the computer does not recognise. It's usually because you've misspelt the name of a variable you created earlier. Note: variable names are case sensitive! my_number = 4 my_Number + 2 Traceback (most recent call last): File "<stdin>", line 1, NameError: name 'my_Number' is not defined Zero Division Error This happens when you try to divide by zero, This is something that is mathematically impossible so Python will also complain. 5 % 0 Traceback (most recent call last): File "<stdin>", line 1, ZeroDivisionError: integer division or modulo by zero E R R O R S Syntax Error Syntax errors happen when your code does not make any sense to the computer. This can happen because you've misspelt something or there's too many brackets or a missing comma. print(12 + 4)) File "<stdin>", line 1 print(12 + 4)) ^ SyntaxError: unmatched ')'
  • 6. PYTHON CHEAT SHEET 1 0 0 D A Y S O F C O D E C O M P L E T E P R O F E S S I O N A L P Y T H O N B O O T C A M P www.appbrewery.com Functions with Inputs In addition to simple functions, you can give the function an input, this way, each time the function can do something different depending on the input. It makes your function more useful and re-usable. def add(n1, n2): print(n1 + n2) add(2, 3) F U N C T I O N S Creating Functions This is the basic syntax for a function in Python. It allows you to give a set of instructions a name, so you can trigger it multiple times without having to re-write or copy-paste it. The contents of the function must be indented to signal that it's inside. def my_function(): print("Hello") name = input("Your name:") print("Hello") Calling Functions You activate the function by calling it. This is simply done by writing the name of the function followed by a set of round brackets. This allows you to determine when to trigger the function and how many times. my_function() my_function() #The function my_function #will run twice.
  • 7. PYTHON CHEAT SHEET 1 0 0 D A Y S O F C O D E C O M P L E T E P R O F E S S I O N A L P Y T H O N B O O T C A M P www.appbrewery.com Variable Scope Variables created inside a function are destroyed once the function has executed. The location (line of code) that you use a variable will determine its value. Here n is 2 but inside my_function() n is 3. So printing n inside and outside the function will determine its value. n = 2 def my_function(): n = 3 print(n) print(n) #Prints 2 my_function() #Prints 3 Functions with Outputs In addition to inputs, a function can also have an output. The output value is proceeded by the keyword "return". This allows you to store the result from a function. def add(n1, n2): return n1 + n2 result = add(2, 3) Keyword Arguments When calling a function, you can provide a keyword argument or simply just the value. Using a keyword argument means that you don't have to follow any order when providing the inputs. def divide(n1, n2): result = n1 / n2 #Option 1: divide(10, 5) #Option 2: divide(n2=5, n1=10)
  • 8. PYTHON CHEAT SHEET 1 0 0 D A Y S O F C O D E C O M P L E T E P R O F E S S I O N A L P Y T H O N B O O T C A M P www.appbrewery.com Elif In addition to the initial If statement condition, you can add extra conditions to test if the first condition is false. Once an elif condition is true, the rest of the elif conditions are no longer checked and are skipped. weather = "sunny" if weather == "rain": print("bring umbrella") elif weather == "sunny": print("bring sunglasses") elif weather == "snow": print("bring gloves") C O N D I T I O N A L S If This is the basic syntax to test if a condition is true. If so, the indented code will be executed, if not it will be skipped. n = 5 if n > 2: print("Larger than 2") Else This is a way to specify some code that will be executed if a condition is false. age = 18 if age > 16: print("Can drive") else: print("Don't drive")
  • 9. PYTHON CHEAT SHEET 1 0 0 D A Y S O F C O D E C O M P L E T E P R O F E S S I O N A L P Y T H O N B O O T C A M P www.appbrewery.com not This will flip the original result of the condition. e.g. if it was true then it's now false. if not 3 > 1: print("something") #Will not be printed. and This expects both conditions either side of the and to be true. s = 58 if s < 60 and s > 50: print("Your grade is C") or This expects either of the conditions either side of the or to be true. Basically, both conditions cannot be false. age = 12 if age < 16 or age > 200: print("Can't drive") comparison operators These mathematical comparison operators allow you to refine your conditional checks. > Greater than < Lesser than >= Greater than or equal to <= Lesser than or equal to == Is equal to != Is not equal to
  • 10. PYTHON CHEAT SHEET 1 0 0 D A Y S O F C O D E C O M P L E T E P R O F E S S I O N A L P Y T H O N B O O T C A M P www.appbrewery.com _ in a For Loop If the value your for loop is iterating through, e.g. the number in the range, or the item in the list is not needed, you can replace it with an underscore. for _ in range(100): #Do something 100 times. L O O P S While Loop This is a loop that will keep repeating itself until the while condition becomes false. n = 1 while n < 100: n += 1 For Loop For loops give you more control than while loops. You can loop through anything that is iterable. e.g. a range, a list, a dictionary or tuple. all_fruits = ["apple", "banana", "orange"] for fruit in all_fruits: print(fruit) break This keyword allows you to break free of the loop. You can use it in a for or while loop. scores = [34, 67, 99, 105] for s in scores: if s > 100: print("Invalid") break print(s)
  • 11. PYTHON CHEAT SHEET 1 0 0 D A Y S O F C O D E C O M P L E T E P R O F E S S I O N A L P Y T H O N B O O T C A M P www.appbrewery.com continue This keyword allows you to skip this iteration of the loop and go to the next. The loop will still continue, but it will start from the top. n = 1 while n < 100: if n % 2 == 0: continue print(n) #Prints all the odd numbers Infinite Loops Sometimes, the condition you are checking 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. while 5 > 1: print("I'm a survivor")
  • 12. PYTHON CHEAT SHEET 1 0 0 D A Y S O F C O D E C O M P L E T E P R O F E S S I O N A L P Y T H O N B O O T C A M P www.appbrewery.com List Index To get hold of a particular item from a list you can use its index number. This number can also be negative, if you want to start counting from the end of the list. letters = ["a", "b", "c"] letters[0] #Result:"a" letters[-1] #Result: "c" L I S T M E T H O D S Adding Lists Together You can extend a list with another list by using the extend keyword, or the + symbol. list1 = [1, 2, 3] list2 = [9, 8, 7] new_list = list1 + list2 list1 += list2 Adding an Item to a List If you just want to add a single item to a list, you need to use the .append() method. all_fruits = ["apple", "banana", "orange"] all_fruits.append("pear") List Slicing Using the list index and the colon symbol you can slice up a list to get only the portion you want. Start is included, but end is not. #list[start:end] letters = ["a","b","c","d"] letters[1:3] #Result: ["b", "c"]
  • 13. PYTHON CHEAT SHEET 1 0 0 D A Y S O F C O D E C O M P L E T E P R O F E S S I O N A L P Y T H O N B O O T C A M P www.appbrewery.com Round This does a mathematical round. So 3.1 becomes 3, 4.5 becomes 5 and 5.8 becomes 6. round(4.6) # result 5 B U I L T I N F U N C T I O N S Randomisation The random functions come from the random module which needs to be imported. In this case, the start and end are both included start <= randint <= end import random # randint(start, end) n = random.randint(2, 5) #n can be 2, 3, 4 or 5. Range Often you will want to generate a range of numbers. You can specify the start, end and step. Start is included, but end is excluded: start >= range < end # range(start, end, step) for i in range(6, 0, -2): print(i) # result: 6, 4, 2 # 0 is not included. abs This returns the absolute value. Basically removing any -ve signs. abs(-4.6) # result 4.6
  • 14. PYTHON CHEAT SHEET 1 0 0 D A Y S O F C O D E C O M P L E T E P R O F E S S I O N A L P Y T H O N B O O T C A M P www.appbrewery.com M O D U L E S Aliasing You can use the as keyword to give your module a different name. import random as r n = r.randint(1, 5) Importing Some modules are pre-installed with python e.g. random/datetime Other modules need to be installed from pypi.org import random n = random.randint(3, 10) Importing Everything You can use the wildcard (*) to import everything from a module. Beware, this usually reduces code readability. from random import * list = [1, 2, 3] choice(list) # More readable/understood #random.choice(list) Importing from modules You can import a specific thing from a module. e.g. a function/class/constant You do this with the from keyword. It can save you from having to type the same thing many times. from random import randint n = randint(1, 5)
  • 15. PYTHON CHEAT SHEET 1 0 0 D A Y S O F C O D E C O M P L E T E P R O F E S S I O N A L P Y T H O N B O O T C A M P www.appbrewery.com C L A S S E S & O B J E C T S Creating an Object from a Class You can create a new instance of an object by using the class name + () class Car: pass my_toyota = Car() Creating a Python Class You create a class using the class keyword. Note, class names in Python are PascalCased. So to create an empty class class MyClass: #define class Class Methods You can create a function that belongs to a class, this is known as a method. class Car: def drive(self): print("move") my_honda = Car() my_honda.drive() Class Variables You can create a varaiable in a class. The value of the variable will be available to all objects created from the class. class Car: colour = "black" car1 = Car() print(car1.colour) #black
  • 16. PYTHON CHEAT SHEET 1 0 0 D A Y S O F C O D E C O M P L E T E P R O F E S S I O N A L P Y T H O N B O O T C A M P www.appbrewery.com The __init__ method The init method is called every time a new object is created from the class. class Car: def __init__(self): print("Building car") my_toyota = Car() #You will see "building car" #printed. Class Inheritance When you create a new class, you can inherit the methods and properties of another class. class Animal: def breathe(self): print("breathing") class Fish(Animal): def breathe(self): super.breathe() print("underwater") nemo = Fish() nemo.breathe() #Result: #breathing #underwater Class Properties You can create a variable in the init() of a class so that all objects created from the class has access to that variable. class Car: def __init__(self, name): self.name = "Jimmy"