Python Cheat Sheets

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

PYTHON CHEAT SHEET

100 DAYS OF CODE


COMPLETE PROFESSIONAL
PYTHON BOOTCAMP

BASICS

Print

Prints a string into the console. print("Hello World")

Input

Prints a string into the console, input("What's your name")


and asks the user for a string input.

Comments

Adding a # symbol in font of text #This is a comment


lets you make comments on a line of code. print("This is code")
The computer will ignore your comments.

Variables

A variable give a name to a piece of data. my_name = "Angela"


Like a box with a label, it tells you what's my_age = 12
inside the box.

The += Operator

This is a convient way of saying: "take the my_age = 12


previous value and add to it. my_age += 4
#my_age is now 16

www.appbrewery.com
PYTHON CHEAT SHEET
100 DAYS OF CODE
COMPLETE PROFESSIONAL
PYTHON BOOTCAMP

F-Strings

You can insert a variable into a string days = 365


using f-strings. print(f"There are {days}
The syntax is simple, just insert the variable in a year")
in-between a set of curly braces {}.

Converting Data Types

You can convert a variable from 1 data n = 354


type to another. new_n = float(n)
Converting to float:
float() print(new_n) #result 354.0
Converting to int:
int()
Converting to string:
str()

Checking Data Types

You can use the type() function


n = 3.14159
to check what is the data type of a type(n) #result float
particular variable.

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

This happens when there is a variable


my_number = 4
with a name that the computer my_Number + 2
does not recognise. It's usually because Traceback (most recent call
you've misspelt the name of a variable
last): File "<stdin>", line 1,
you created earlier.
NameError: name 'my_Number'
Note: variable names are case sensitive!
is not defined

Zero Division Error

This happens when you try to divide by zero,


5 % 0
This is something that is mathematically Traceback (most recent call
impossible so Python will also complain. last): File "<stdin>", line 1,
ZeroDivisionError: integer
division or modulo by zero

www.appbrewery.com
PYTHON CHEAT SHEET
100 DAYS OF CODE
COMPLETE PROFESSIONAL
PYTHON BOOTCAMP

Functions with Outputs

In addition to inputs, a function can also have def add(n1, n2):


an output. The output value is proceeded by return n1 + n2
the keyword "return".
This allows you to store the result from a
function.
result = add(2, 3)

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

This will flip the original result of the if not 3 > 1:


condition. e.g. if it was true then it's now print("something")
false. #Will not be printed.

comparison operators

These mathematical comparison operators > Greater than


allow you to refine your conditional checks. < Lesser than
>= Greater than or equal to
<= Lesser than or equal to
== Is equal to
!= Is not equal to

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

CLASSES & OBJECTS

Creating a Python Class


class MyClass:
You create a class using the class keyword.
#define class
Note, class names in Python are PascalCased.
So to create an empty class 👉

Creating an Object from a Class


class Car:
You can create a new instance of an object pass
by using the class name + ()

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy