Python Cheatsheet Every Learner Must Know

Python is one of the most versatile and beginner-friendly programming languages. Whether you’re just starting out or looking to refresh your memory, this comprehensive cheatsheet will save you hours of time searching for syntax and common patterns. Keep this guide handy as your quick reference!

Quick Reference Table

Category Concept Syntax Example
Variables Assignment x = 5
x = 5
Multiple assignment a, b = 1, 2
a, b = 1, 2
Data Types String text = "Hello"
text = "Hello"
Integer num = 42
num = 42
Float pi = 3.14
pi = 3.14
Boolean is_true = True
is_true = True
List items = [1, 2, 3]
items = [1, 2, 3]
Tuple coords = (x, y)
coords = (10, 20)
Dictionary data = {"key": "value"}
data = {"name": "Alice"}
Set unique = {1, 2, 3}
unique = {1, 2, 3}
String Operations Concatenation "Hello" + " World"
"Hello" + " World"
Repetition "Ha" * 3
"Ha" * 3
Slicing text[0:5]
text = "Hello"
text[0:5]
Length len(text)
len("Hello")
Upper/Lower text.upper() / text.lower()
"hello".upper()
Strip whitespace text.strip()
"  text  ".strip()
Replace text.replace("old", "new")
"Hello World".replace("World", "Python")
Split text.split(",")
"a,b,c".split(",")
Join ", ".join(list)
", ".join(["a", "b", "c"])
List Operations Append list.append(item)
items = [1, 2]
items.append(3)
Extend list.extend([1, 2])
items = [1]
items.extend([2, 3])
Insert list.insert(0, item)
items = [1, 2]
items.insert(0, 0)
Remove list.remove(item)
items = [1, 2, 3]
items.remove(2)
Pop list.pop() / list.pop(index)
items = [1, 2, 3]
items.pop()
Index list.index(item)
items = [1, 2, 3]
items.index(2)
Count list.count(item)
items = [1, 2, 2, 3]
items.count(2)
Sort list.sort() / sorted(list)
items = [3, 1, 2]
items.sort()
Reverse list.reverse()
items = [1, 2, 3]
items.reverse()
List comprehension [x*2 for x in list]
[x*2 for x in [1, 2, 3]]
Dictionary Operations Access value dict["key"]
person = {"name": "Alice"}
person["name"]
Get with default dict.get("key", default)
person.get("age", 25)
Add key-value dict["new"] = "value"
person["city"] = "NY"
Remove key del dict["key"]
del person["age"]
Get keys dict.keys()
list(person.keys())
Get values dict.values()
list(person.values())
Get items dict.items()
list(person.items())
Check key exists "key" in dict
"name" in person
Control Flow If statement if condition:
if x > 5:
If-else if condition: else:
if x > 5:
    print("Big")
If-elif-else if: elif: else:
if x > 5:
    print("Big")
elif x < 0:
    print("Small")
For loop for item in iterable:
for i in range(5):
While loop while condition:
while x < 10:
    x += 1
Break break
for i in range(10):
    if i == 5:
        break
Continue continue
for i in range(10):
    if i % 2 == 0:
        continue
Loops & Iteration Range for i in range(5):
for i in range(5):
Enumerate for i, val in enumerate(list):
for i, val in enumerate(["a", "b", "c"]):
Zip for a, b in zip(list1, list2):
for a, b in zip([1, 2], [3, 4]):
List comprehension [x for x in list if condition]
[x for x in range(10) if x % 2 == 0]
Functions Define function def func_name(params):
def greet(name):
Return value return value
return "Hello"
Default parameter def func(param="default"):
def greet(name="World"):
*args def func(*args):
def sum_all(*args):
**kwargs def func(**kwargs):
def create(**kwargs):
Lambda lambda x: x*2
square = lambda x: x**2
Map list(map(func, iterable))
list(map(str, [1, 2, 3]))
Filter list(filter(func, iterable))
list(filter(lambda x: x > 5, [1, 2, 3, 6]))
Reduce from functools import reduce
reduce(lambda x, y: x + y, [1, 2, 3])
File Operations Open file open("file.txt", "r")
open("data.txt", "r")
Read file file.read() / file.readlines()
file.read()
Write file file.write("text")
file.write("Hello")
Close file file.close()
file.close()
Context manager with open("file") as f:
with open("data.txt", "r") as f:
Exception Handling Try-except try: except:
try:
    x = 1/0
except:
    print("Error")
Specific exception except ValueError:
except ValueError as e:
Finally finally:
finally:
    print("Done")
Raise exception raise Exception("msg")
raise ValueError("Invalid input")
Classes & OOP Define class class MyClass:
class Dog:
Constructor def __init__(self):
def __init__(self, name):
Method def method(self):
def bark(self):
Inheritance class Child(Parent):
class Puppy(Dog):
Super class super().__init__()
super().__init__()
Class variable class_var = value
species = "Canis"
Instance variable self.var = value
self.name = "Buddy"
Modules Import module import module
import math
Import specific from module import func
from math import sqrt
Import with alias import module as alias
import numpy as np
Import all from module import *
from math import *
String Formatting f-string f"Value: {var}"
f"Name: {name}"
Format method "{}".format(var)
"Hello {}".format(name)
Percent formatting "%s" % var
"Hello %s" % name
Math Operations Power 2 ** 3
2 ** 3
Floor division 7 // 2
7 // 2
Modulo 7 % 2
7 % 2
Absolute value abs(-5)
abs(-5)
Round round(3.14159, 2)
round(3.14159, 2)
Max/Min max([1, 2, 3]) / min([1, 2, 3])
max([1, 2, 3])
Sum sum([1, 2, 3])
sum([1, 2, 3])
Boolean Operations And condition1 and condition2
x > 5 and x < 10
Or condition1 or condition2
x < 5 or x > 10
Not not condition
not x > 5
Comparison ==, !=, >, <, >=, <=
x == 5
x != 5
x > 5
Type Conversion To string str(123)
str(123)
To integer int("123")
int("123")
To float float("3.14")
float("3.14")
To list list("abc")
list("abc")
To tuple tuple([1, 2, 3])
tuple([1, 2, 3])
To set set([1, 2, 2])
set([1, 2, 2])

Essential Code Snippets

1. String Manipulation

text = "  Hello, World!  "

# Remove whitespace
clean = text.strip()

# Convert to uppercase
upper = text.upper()

# Replace text
replaced = text.replace("World", "Python")

# Split into words
words = text.split(", ")

# Join list into string
joined = "-".join(["Python", "is", "awesome"])

print(clean, upper, replaced, words, joined)

2. List Comprehensions

# Basic list comprehension
squares = [x**2 for x in range(10)]

# With condition
evens = [x for x in range(20) if x % 2 == 0]

# Nested comprehension
matrix = [[i*j for j in range(3)] for i in range(3)]

print(squares)    # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
print(evens)      # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
print(matrix)     # [[0, 0, 0], [0, 1, 2], [0, 2, 4]]

3. Dictionary Operations

# Create dictionary
person = {
    "name": "Alice",
    "age": 30,
    "city": "New York"
}

# Access values
print(person["name"])           # Alice
print(person.get("country", "USA"))  # USA (default)

# Update dictionary
person["age"] = 31
person["email"] = "alice@example.com"

# Iterate through dictionary
for key, value in person.items():
    print(f"{key}: {value}")

# Get keys and values
keys = list(person.keys())
values = list(person.values())

4. File Handling

# Reading a file
with open("input.txt", "r") as file:
    content = file.read()
    lines = file.readlines()

# Writing to a file
with open("output.txt", "w") as file:
    file.write("Hello, World!\n")
    file.write("This is a new line.")

# Appending to a file
with open("log.txt", "a") as file:
    file.write(f"Log entry: {datetime.now()}\n")

# Reading CSV file
import csv
with open("data.csv", "r") as file:
    reader = csv.DictReader(file)
    for row in reader:
        print(row)

5. Exception Handling

try:
    # Code that might raise an exception
    result = 10 / 0
except ZeroDivisionError:
    print("Cannot divide by zero!")
except ValueError as e:
    print(f"Invalid value: {e}")
except Exception as e:
    print(f"An error occurred: {e}")
else:
    print("No exceptions occurred")
finally:
    print("This always executes")

# Raising custom exceptions
def validate_age(age):
    if age < 0:
        raise ValueError("Age cannot be negative")
    return age

6. Working with Sets

# Create sets
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}

# Set operations
union = set1 | set2           # {1, 2, 3, 4, 5, 6}
intersection = set1 & set2     # {3, 4}
difference = set1 - set2        # {1, 2}
symmetric_diff = set1 ^ set2    # {1, 2, 5, 6}

# Set methods
set1.add(5)
set1.remove(1)
set1.discard(10)  # No error if not exists
is_member = 3 in set1  # True

7. Lambda Functions

# Simple lambda
square = lambda x: x ** 2
print(square(5))  # 25

# Lambda with multiple arguments
add = lambda x, y: x + y
print(add(3, 4))  # 7

# Lambda with conditional
is_even = lambda x: x % 2 == 0
print(is_even(4))  # True

# Using lambda with map
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, numbers))
print(squared)  # [1, 4, 9, 16, 25]

# Using lambda with filter
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens)  # [2, 4]

8. Class Basics

class Dog:
    # Class variable
    species = "Canis familiaris"
    
    def __init__(self, name, age):
        # Instance variables
        self.name = name
        self.age = age
    
    def bark(self):
        return f"{self.name} says Woof!"
    
    def birthday(self):
        self.age += 1
        return f"{self.name} is now {self.age} years old"

# Create instance
my_dog = Dog("Buddy", 3)
print(my_dog.bark())        # Buddy says Woof!
print(my_dog.birthday())    # Buddy is now 4 years old
print(my_dog.species)        # Canis familiaris

9. Time and Date

from datetime import datetime, timedelta

# Current date and time
now = datetime.now()
print(now.strftime("%Y-%m-%d %H:%M:%S"))

# Create specific date
date = datetime(2026, 2, 17)
print(date)

# Date arithmetic
future = now + timedelta(days=7)
past = now - timedelta(hours=24)

# Parse string to date
date_str = "2026-02-17"
parsed = datetime.strptime(date_str, "%Y-%m-%d")

# Calculate difference
diff = future - now
print(f"Days until: {diff.days}")

10. Working with JSON

import json

# Dictionary to JSON
data = {
    "name": "Python",
    "version": 3.12,
    "features": ["easy", "powerful", "versatile"]
}

# Convert to JSON string
json_string = json.dumps(data, indent=2)
print(json_string)

# Parse JSON string
parsed = json.loads(json_string)
print(parsed["features"])

# Read JSON file
with open("data.json", "r") as file:
    data = json.load(file)

# Write JSON file
with open("output.json", "w") as file:
    json.dump(data, file, indent=2)

Common Import Statements

Purpose Import Statement
Math operations import math
Random numbers import random
Date and time from datetime import datetime
Regular expressions import re
JSON handling import json
CSV files import csv
File paths from pathlib import Path
HTTP requests import requests
Data analysis import pandas as pd
Plotting import matplotlib.pyplot as plt
NumPy arrays import numpy as np
System operations import os
Command line args import sys
Time operations import time
Copy objects import copy
Collections from collections import defaultdict, Counter
Itertools from itertools import count, cycle

⚡ Time-Saving Tips

1. Use f-strings for formatting

# Old way
name = "Alice"
age = 25
print("Name: %s, Age: %d" % (name, age))

# Better way
print(f"Name: {name}, Age: {age}")

2. Use enumerate for index and value

# Old way
items = ['a', 'b', 'c']
for i in range(len(items)):
    print(i, items[i])

# Better way
for i, item in enumerate(items):
    print(i, item)

3. Use zip for parallel iteration

names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 35]

for name, age in zip(names, ages):
    print(f"{name} is {age} years old")

4. Use context managers for files

# Old way
file = open("data.txt", "r")
content = file.read()
file.close()

# Better way
with open("data.txt", "r") as file:
    content = file.read()

5. Use list comprehensions instead of loops

# Old way
squares = []
for i in range(10):
    squares.append(i**2)

# Better way
squares = [i**2 for i in range(10)]

6. Use defaultdict for counting

from collections import defaultdict

# Old way
counts = {}
for item in items:
    if item not in counts:
        counts[item] = 0
    counts[item] += 1

# Better way
counts = defaultdict(int)
for item in items:
    counts[item] += 1

7. Use Counter for frequency analysis

from collections import Counter

words = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
word_counts = Counter(words)

print(word_counts.most_common(2))  # [('apple', 3), ('banana', 2)]

8. Use pathlib for file paths

from pathlib import Path

# Old way
import os
path = os.path.join('folder', 'file.txt')

# Better way
path = Path('folder') / 'file.txt'

# Check if exists
if path.exists():
    content = path.read_text()

Best Practices

  1. Use meaningful variable names
    # Bad
    x = 5
    y = 10
       
    # Good
    width = 5
    height = 10
    
  2. Write docstrings for functions
    def calculate_area(length, width):
        """Calculate the area of a rectangle.
           
        Args:
            length: The length of the rectangle
            width: The width of the rectangle
               
        Returns:
            The area of the rectangle
        """
        return length * width
    
  3. Use type hints (Python 3.5+)
    def greet(name: str) -> str:
        return f"Hello, {name}!"
    
  4. Handle exceptions gracefully
    try:
        result = risky_operation()
    except SpecificException as e:
        logger.error(f"Error: {e}")
        raise
    
  5. Use constants for magic numbers
    # Bad
    if temperature > 37:
        print("High temperature")
       
    # Good
    NORMAL_TEMP = 37
    if temperature > NORMAL_TEMP:
        print("High temperature")
    

📖 Common Mistakes to Avoid

  1. Modifying list while iterating
    # Bad - will cause issues
    items = [1, 2, 3, 4, 5]
    for item in items:
        if item % 2 == 0:
            items.remove(item)
       
    # Good - create new list
    items = [1, 2, 3, 4, 5]
    items = [item for item in items if item % 2 != 0]
    
  2. Using mutable default arguments
    # Bad - shared across calls
    def append_to(item, lst=[]):
        lst.append(item)
        return lst
       
    # Good - use None as default
    def append_to(item, lst=None):
        if lst is None:
            lst = []
        lst.append(item)
        return lst
    
  3. Comparing with None
    # Bad
    if x == None:
        pass
       
    # Good
    if x is None:
        pass
    
  4. Forgetting to use list() on map/filter
    # In Python 3, map returns iterator
    result = map(lambda x: x*2, [1, 2, 3])
    print(list(result))  # Need to convert to list
    

🔧 Debugging Tips

  1. Use print statements strategically
    print(f"Debug: x = {x}, y = {y}")
    
  2. Use pdb for interactive debugging
    import pdb; pdb.set_trace()
    
  3. Use type() to check variable types
    print(type(variable))
    
  4. Use dir() to explore objects
    print(dir(my_object))
    
  5. Use help() for documentation
    help(str.upper)
    

Conclusion

This Python cheatsheet covers the most essential concepts and operations you’ll use daily as a Python developer. Keep this guide bookmarked and refer to it whenever you need a quick reminder. The more you practice these patterns, the more natural they’ll become.

Remember, the best way to learn Python is by writing code. Start with small projects, experiment with these concepts, and gradually build up your skills. Happy coding!

Watch PyShine on YouTube

Contents