Python Cheat Sheet (Printable)
Whether you are a beginner learning core programming fundamentals or an experienced software engineer switching languages, having a quick, comprehensive reference guide is invaluable. Python's clean syntax, powerful built-in data structures, and expressive libraries make it one of the most productive languages in the world.
This exhaustive, printable Python Cheat Sheet covers essential syntax, data structures, control flow, functions, object-oriented programming, and file operations.
1. Variables and Data Types
Python is dynamically typed. You do not need to declare variable types explicitly; assignment determines the type automatically.
# Basic Data Types
age = 25 # int
price = 19.99 # float
name = "CodeCrown" # str
is_active = True # bool
data = None # NoneType
# Multiple assignment
x, y, z = 1, 2.5, "Python"
2. Core Data Structures (Lists, Tuples, Dictionaries, Sets)
Python provides versatile built-in collections for managing data collections efficiently.
# Lists (Ordered, Mutable)
framworks = ["Flask", "FastAPI", "Django"]
framworks.append("Sanic")
framworks.pop(0) # Removes "Flask"
# Tuples (Ordered, Immutable)
rgb_code = (255, 128, 0)
# Dictionaries (Key-Value Pairs, Mutable)
user = {"username": "alex_dev", "role": "admin", "active": True}
print(user.get("username"))
# Sets (Unordered, Unique Elements)
unique_tags = {"python", "backend", "api", "python"}
3. Control Flow and Loops
Conditional logic and looping constructs control execution flow.
# If-Else Conditional
score = 88
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
else:
grade = "C"
# For Loop with range and enumeration
for i in range(3):
print(i) # 0, 1, 2
for idx, lang in enumerate(["Python", "Node.js", "Go"]):
print(f"{idx}: {lang}")
# While Loop
count = 0
while count < 2:
print(count)
count += 1
4. Functions and Lambda Expressions
Functions encapsulate reusable logic using the def keyword, while anonymous functions use lambda.
# Standard Function with type hints
def calculate_total(price: float, tax: float = 0.05) -> float:
return price + (price * tax)
# Lambda Expression
square = lambda x: x * x
print(square(5)) # 25
# List Comprehension
squares = [x**2 for x in range(5)]
5. Exception Handling
Handle runtime errors gracefully using try, except, else, and finally blocks.
try:
result = 10 / 0
except ZeroDivisionError as e:
print(f"Error caught: {e}")
else:
print("Execution successful")
finally:
print("Cleanup operations executed.")
Conclusion
Keep this quick-reference cheat sheet handy while building Python scripts, backend APIs, and automation pipelines to accelerate your development workflow.