Python Cheat Sheet
Python Cheat Sheet
https://www.linkedin.com/in/vishal-kumar-
483461208/
BY – Vishal Kumar
Variables and data types
# Variable declaration
variable_name = value
# 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
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
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
Loops
# for loop
for variable in
sequence: # code block
# while loop
while condition:
# code block
Functions
# Function declaration
def function_name(parameter1,
parameter2): # code block
return result
# Function call
function_name(argument1, argument2)
Lists
# List declaration
list_name = [item1, item2, item3]
# List slicing
sliced_list =
list_name[start_index:end_index:step]
Dictionaries
# Dictionary declaration
dictionary_name = {"k1": v1, "k2": v2}
# 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()
Tuples
# Tuple declaration
tuple_name = (item1, item2, item3)
# Tuple slicing
sliced_tuple =
tuple_name[start_index:end_index:step]
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)
List Comprehensions
# 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
Modules and Packages
# Importing a module
import module_name
# Importing a package
import package_name
def method_name(self):
# code block
# Object creation
object_name =
ClassName(argument1, argument2)
# Child class
class
ChildClass(ParentClass): def
child_method(self):
# code block
# Object creation
object_name = ChildClass()
# 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()
# Map function
new_list = map(function, iterable)
# Filter function
new_list = filter(function, iterable)
# Reduce function
from functools import reduce
result = reduce(function, iterable)
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
# Generator function
def generator_function():
for i in range(10):
yield i
# Using a generator
for value in
generator_function(): # code
block
File Handling
# Opening a file
file = open("filename", "mode")
# Writing to a file
file.write("text")
# Closing a file
file.close()
Virtual Environments
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()