Show Menu
Cheatography

Python Cheat Sheet (DRAFT) by

This cheat sheet made by Team 7 Group 2 Gen 11 cyber security

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Data Types

Integer
10, -20
Float
3.7, -1.4
String
"­abc­", "­hel­lo!­"
Boolean
True, False
List
[ value, ... ]
Tuple
( value, ... )
Dictionary
{ key: value, ... }
Set
{ value, value, ... }

Arit­hmetic Compar­isons

x < y
Less Than
x > y
Greater Than
x <= y
Less Than or Equal
x >= y
Greater Than or Equal
x == y
Equal
x != y
Not Equal

Stat­eme­nts

If Statement
if (expression):
­ ­  ­sta­­te­ments
Else If Statement
elif (expression):
­ ­  ­sta­­te­ments
Else Statement
else:
­ ­  ­sta­­te­ments

Loop

While Loop
while (expression):
 ­sta­tements
For Loop
(Iterating over a collection)
for item in collection:
 statements
For Loop (Counting)
for i in range(start, end, step):
 statements
# (start is included; end is not)

Function Defini­­ti­ons

Function return value
with parameters
def name(parameter1, par2, ...):
 statements
 return expression
Function return value
without parameters
def name():
 statements
 ­return expression
Function non-return value
with parameters
def name(parameter1, par2, ...):
­  statements
Function non-return value
without parameters
def name():
­  ­sta­tements
 

Boolean Logic Operat­ors

x and y
x or y
not x

Arit­hmetic Operat­ors

x + y
Addition
x - y
Subtra­ction
x * y
Multip­lic­ation
x / y
Division
x % y
Modulus
x // y
Floor Division
x ** y
Expone­nti­ation
x operators= y
e.g.: x += 1

Inpu­t/O­utput

Input
input()
Output
print()

Conv­ersion Functi­ons

int(expr)
Converts expression to integer
float(­expr)
Converts expression to float
str(expr)
Converts expression to string

String / List / Tuple Operat­ions

len(s)
Length of s
s[i]
ith item in s (0-based)
s[start : end]
Slice of s from start (included) to end (excluded)
x in s
True if x is contained in s
x not in s
True if x is not contained in s
s + t
The concat­­en­ation of s with t
s * n
n copies of s concat­­enated

Refe­rence

W3Schools
Cheato­graphy

Group Members of Team 7

Full name
ID
Sin Hong
IDTB110277
Oeng Vengku
IDTB110346
Phoeurn Rozariya
IDTB110147
Rous Sovann Makara
IDTB110159