Posts /

Python Cheat Sheets

10 Dec 2022

Python is an interpreted, high-level and general-purpose, dynamically typed programming language

Table of Content


Create and execute a program

  1. Open up a terminal/cmd
  2. Create the program: nano/cat >
  3. Write the program and save it
  4. python

Basic Datatypes

Data Type Description
int Integer values [0, 1, -2, 3]
float Floating point values [0.1, 4.532, -5.092]
char Characters [a, b, @, !, `]
str Strings [abc, AbC, A@B, sd!, `asa]
bool Boolean Values [True, False]
complex Complex numbers [2+3j, 4-1j]


Keyword Description Category
True Boolean value for not False or 1 Value Keyword
False Boolean Value for not True or 0 Value Keyword
None No Value Value keyword
and returns true if both (oprand) are true (other language && ) Operator keyword
or returns true of either operands is true (other language) Operator keyword
in returns true if word is in iterator Operator keyword
is returns true if id of variables are same Operator keyword
not returns opposite Boolean value Operator Keyword
if get into block if expression is true conditional
elif for more than 1 if checks conditional
else this block will be executed if condition is false conditional
for used for looping iteration
while used for looping iteration
break get out of loop iteration
continue skip for specific condition iteration
def make user defined function structure
class make user defined classes structure
lambda make anonymous function structure
with execute code within context manager’s scope structure
as alias for something structure
pass used for making empty structures(declaration) structure
return get value(s) from function, get out of function returning keyword
yield yields values instead of returning (are called generators) returning keyword
import import libraries/modules/packages import
from import specific function/classes from modules/packages import
try this block will be tried to get executed exception handling
except is any exception/error has occured it’ll be executed exception handling
finally It’ll be executed no matter exception occurs or not exception handling
raise throws any specific error/exception exception handling
assert throws an AssertionError if condition is false exception handling
async used to define asynchronous functions/co-routines asynchronous programming
await used to specify a point when control is taken back asynchronous programming
del deletes/unsets any user defined data variable handling
global used to access variables defined outside of function variable handling
nonlocal modify variables from different scopes variable handling


Operator Description  
( ) grouping parenthesis, function call, tuple declaration  
[ ] array indexing, also declaring lists etc.  
! relational not, complement, ! a yields true or false  
~ bitwise not, ones complement, ~a  
- unary minus, - a  
+ unary plus, + a  
* multiply, a * b  
/ divide, a / b  
% modulo, a % b  
+ add, a + b  
- subtract, a - b  
« shift left, left operand is shifted left by right operand bits (multiply by 2)  
>> shift right, left operand is shifted right by right operand bits (divide by 2)  
< less than, result is true or false, a %lt; b  
<= less than or equal, result is true or false, a <= b  
> greater than, result is true or false, a > b  
>= greater than or equal, result is true or false, a >= b  
== equal, result is true or false, a == b  
!= not equal, result is true or false, a != b  
& bitwise and, a & b  
^ bitwise exclusive or XOR, a ^ b  
| bitwise or, a b
&&, and relational and, result is true or false, a < b && c >= d  
||, or relational or, result is true or false, a < b || c >= d  
= store or assignment  
+= add and store  
-= subtract and store  
*= multiply and store  
/= divide and store  
%= modulo and store  
«= shift left and store  
>>= shift right and store  
&= bitwise and and store  
^= bitwise exclusive or and store  
|= bitwise or and store  
, separator as in ( y=x,z=++x )  

Basic Data Structures


thislist = ["apple", "banana", "cherry"] 


thistuple = ("apple", "banana", "cherry", "apple", "cherry")
thistuple = ("apple", "banana", "cherry")
thistuple = ("apple",)

# NOT a tuple
thistuple = ("apple")

thistuple = tuple(("apple", "banana", "cherry")) # note the double round-brackets


thisset = {"apple", "banana", "cherry"}
thisset = {"apple", "banana", "cherry"}
set1 = {"apple", "banana", "cherry"}
set2 = {1, 5, 7, 9, 3}
set3 = {True, False, False}
set4 = {"abc", 34, True, 40, "male"}
thisset = set(("apple", "banana", "cherry")) # note the double round-brackets
set1 = {"apple", "banana", "cherry"}


thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
thisdict = {
  "brand": "Ford",
  "electric": False,
  "year": 1964,
  "colors": ["red", "white", "blue"]
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964

x = car.pop("model")

print(x)# Mustang
print(car)#{'brand': 'Ford', 'year': 1964}

Conditional branching

    if condition:
    elif condition2:


Python has two primitive loop commands:

  1. while loops
  2. for loops

While loop

i = 1
while i < 6:
  i += 1

For loop

fruits = ["apple", "banana", "cherry"]
for x in fruits:
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]

for x in adj:
  for y in fruits:
    print(x, y)
for x in [0, 1, 2]:

Function definition

def function_name():

Function call


Reference: Awesome Cheet Sheets by LeCoup