Python Programming Cheatsheet

Essential Python commands and concepts for programmers of all levels


Category Command/Concept Description Example Level
Variables Variable Assignment Assign a value to a variable x = 10 Beginner
Data Types Strings Text data enclosed in quotes name = "Python" Beginner
Data Types Integers Whole numbers without decimal points age = 25 Beginner
Data Types Floats Numbers with decimal points price = 19.99 Beginner
Data Types Booleans True or False values is_active = True Beginner
Operators Arithmetic Basic math operations x + y, x - y, x * y, x / y Beginner
Control Flow if-else Conditional execution if x > 10:
  print("Greater")
else:
  print("Less")
Beginner
Loops for loop Iterate over a sequence for i in range(5):
  print(i)
Beginner
Functions Function Definition Create reusable code blocks def greet(name):
  return f"Hello, {name}!"
Intermediate
Data Structures Lists Ordered, mutable collections fruits = ["apple", "banana", "cherry"] Intermediate
Data Structures Dictionaries Key-value pairs person = {"name": "John", "age": 30} Intermediate
Data Structures Tuples Ordered, immutable collections point = (10, 20) Intermediate
Data Structures Sets Unordered collections of unique items unique_numbers = {1, 2, 3, 4} Intermediate
String Methods String Manipulation Various string operations text.upper(), text.lower(), text.split() Intermediate
File I/O Reading Files Read data from files with open("file.txt", "r") as f:
  content = f.read()
Intermediate
File I/O Writing Files Write data to files with open("file.txt", "w") as f:
  f.write("Hello, World!")
Intermediate
OOP Classes Define custom objects class Person:
  def __init__(self, name):
    self.name = name
Advanced
OOP Inheritance Create class hierarchies class Student(Person):
  def __init__(self, name, grade):
    super().__init__(name)
    self.grade = grade
Advanced
Error Handling try-except Handle exceptions try:
  result = x / y
except ZeroDivisionError:
  print("Cannot divide by zero")
Advanced
Modules Importing Use external modules import math
from datetime import datetime
Advanced
List Comprehensions Concise List Creation Create lists using a compact syntax squares = [x**2 for x in range(10)] Advanced
Decorators Function Decorators Modify function behavior @timeit
def slow_function():
Expert
Generators Yield Statement Create iterators efficiently def fibonacci():
  a, b = 0, 1
  while True:
    yield a
    a, b = b, a + b
Expert
Context Managers with Statement Manage resources efficiently with open("file.txt") as f, lock:
&

with open("file.txt") as f, lock:
  data = f.read()
Expert
Multithreading Threads Execute code concurrently import threading
t = threading.Thread(target=func)
Expert
Metaclasses Custom Metaclasses Control class creation class Meta(type):
  def __new__(cls, name, bases, attrs):
Expert

Generating PDF...