Python Cheat Sheet
Python Cheat Sheet
CHEAT SHEET
allinpython.com
Variables and data types
# Variable declaration
variable_name = value
allinpython.com
Operators
# Arithmetic operators
addition = 5 + 2
subtraction = 5 - 2
multiplication = 5 * 2
division = 5 / 2
floor_division = 5 // 2
exponentiation = 5 ** 2
modulus = 5 % 2
# Logical operators
and_operator = True and False
or_operator = True or False
not_operator = not True
allinpython.com
Operators
# Comparison operators
greater_than = 5 > 2
less_than = 5 < 2
equal_to = 5 == 2
not_equal_to = 5 != 2
greater_than_or_equal_to = 5 >= 2
less_than_or_equal_to = 5 <= 2
allinpython.com
Conditional statements
# if statement
if condition:
# code block
# if-else statement
if condition:
# code block
else:
# code block
# if-elif-else statement
if condition:
# code block
elif condition:
# code block
else:
# code block
allinpython.com
Loops
# for loop
for variable in sequence:
# code block
# while loop
while condition:
# code block
allinpython.com
Functions
# Function declaration
def function_name(parameter1, parameter2):
# code block
return result
# Function call
function_name(argument1, argument2)
allinpython.com
Lists
# List declaration
list_name = [item1, item2, item3]
# List slicing
sliced_list =
list_name[start_index:end_index:step]
allinpython.com
Dictionaries
# Dictionary declaration
dictionary_name = {"k1": v1, "k2": v2}
allinpython.com
Strings
# String declaration
string_name = "Hello world!"
# String concatenation
concatenated_string = "Hello" + "world!"
# String interpolation
name = "John"
greeting = f"Hello, {name}!"
# String methods
string_length = len(string_name)
uppercase_string = string_name.upper()
lowercase_string = string_name.lower()
allinpython.com
Tuples
# Tuple declaration
tuple_name = (item1, item2, item3)
# Tuple slicing
sliced_tuple =
tuple_name[start_index:end_index:step]
allinpython.com
Sets
# Set declaration
set_name = {item1, item2, item3}
# Set operations
union_set = set1.union(set2)
intersection_set = set1.intersection(set2)
difference_set = set1.difference(set2)
allinpython.com
List Comprehensions
allinpython.com
Error Handling
# try-except block
try:
# code block
except ErrorType:
# code block
# try-except-else block
try:
# code block
except ErrorType:
# code block
else:
# code block
# try-except-finally block
try:
# code block
except ErrorType:
# code block
finally:
# code block
allinpython.com
Modules and Packages
# Importing a module
import module_name
# Importing a package
import package_name
allinpython.com
Classes and Objects
# Class declaration
class ClassName:
def __init__(self, parameter1,
parameter2):
self.parameter1 = parameter1
self.parameter2 = parameter2
def method_name(self):
# code block
# Object creation
object_name = ClassName(argument1,
argument2)
allinpython.com
Inheritance
# Parent class
class ParentClass:
def parent_method(self):
# code block
# Child class
class ChildClass(ParentClass):
def child_method(self):
# code block
# Object creation
object_name = ChildClass()
allinpython.com
Polymorphism
# Parent class
class ParentClass:
def polymorphic_method(self):
# code block
# Child class 1
class ChildClass1(ParentClass):
def polymorphic_method(self):
# code block
# Child class 2
class ChildClass2(ParentClass):
def polymorphic_method(self):
# code block
# Object creation
object1 = ChildClass1()
object2 = ChildClass2()
allinpython.com
Lambda Functions
allinpython.com
Map, Filter, and Reduce
# Map function
new_list = map(function, iterable)
# Filter function
new_list = filter(function, iterable)
# Reduce function
from functools import reduce
result = reduce(function, iterable)
allinpython.com
Decorators
# Decorator function
def decorator_function(original_function):
def wrapper_function(*args, **kwargs):
# code before original function
result = original_function(*args, **kwargs)
# code after original function
return result
return wrapper_function
allinpython.com
Generators
# Generator function
def generator_function():
for i in range(10):
yield i
# Using a generator
for value in generator_function():
# code block
allinpython.com
File Handling
# Opening a file
file = open("filename", "mode")
# Writing to a file
file.write("text")
# Closing a file
file.close()
allinpython.com
Virtual Environments
allinpython.com
Context Managers
allinpython.com
Threading and Multiprocessing
import threading
import multiprocessing
# Threading
thread = threading.Thread(target=function_name,
args=(argument1, argument2))
thread.start()
# Multiprocessing
process =
multiprocessing.Process(target=function_name,
args=(argument1, argument2))
process.start()
allinpython.com