A Cheat Sheet π to revise Python syntax in less time. Particularly useful for solving Data Structure and Algorithmic problems or a quick overview before an interview.
Click here for similar Java Resource (not made by me)
Get a PDF of this sheet at the end.
Leave a β if you like the cheat sheet (contributions welcome!)
nums = [1,2,3]
# Common Operations
nums.index(1) # Find index
nums.append(1) # Add to end
nums.insert(0,10) # Add 10 from left (at index 0 which is start)
nums.remove(3) # Remove value
nums.pop() # Remove & return last element
nums.sort() # In-place sort (TimSort: O(n log n))
nums.reverse() # In-place reverse
nums.copy() # Return shallow copy
# List Slicing
nums[start:stop:step] # Generic slice syntax
nums[-1] # Last item
nums[::-1] # Reverse list
nums[1:] # Everything after index 1
nums[:3] # First three elements
d = {'a':1, 'b':2}
# Essential Operations
d.get('key', default) # Safe access with default
d.setdefault('key', 0) # Set if missing
d.items() # Key-value pairs
d.keys() # Just keys
d.values() # Just values
d.pop(key) # Remove and return value
d.update({key: value}) # Batch update
# Advanced Usage
from collections import defaultdict
d = defaultdict(list) # Auto-initialize missing keys
d = defaultdict(int) # Useful for counting
from collections import Counter
# Initialize
c = Counter(['a','a','b']) # From iterable
c = Counter("hello") # From string
# Operations
c.most_common(2) # Top 2 frequent elements
c['a'] += 1 # Increment count
c.update("more") # Add counts from iterable
c.total() # Sum of all counts
from collections import deque
# Perfect for BFS - O(1) operations on both ends
d = deque()
d.append(1) # Add right
d.appendleft(2) # Add left
d.pop() # Remove right
d.popleft() # Remove left
d.extend([1,2,3]) # Extend right
d.extendleft([1,2,3])# Extend left
d.rotate(n) # Rotate n steps right (negative for left)
import heapq
# MinHeap Operations - All O(log n) except heapify
nums = [3,1,4,1,5]
heapq.heapify(nums) # Convert to heap in-place: O(n)
heapq.heappush(nums, 2) # Add element: O(log n)
smallest = heapq.heappop(nums) # Remove smallest: O(log n)
# MaxHeap Trick: Multiply by -1
nums = [-x for x in nums] # Convert to maxheap: O(n)
heapq.heapify(nums) # O(n)
largest = -heapq.heappop(nums) # Get largest: O(log n)
# Advanced Operations
k_largest = heapq.nlargest(k, nums) # O(n * log k)
k_smallest = heapq.nsmallest(k, nums) # O(n * log k)
# Custom Priority Queue
heap = []
heapq.heappush(heap, (priority, item)) # Sort by priority
s = {1,2,3}
# Common Operations
s.add(4) # Add element
s.remove(4) # Remove (raises error if missing)
s.discard(4) # Remove (no error if missing)
s.pop() # Remove and return arbitrary element
# Set Operations
a.union(b) # Elements in a OR b
a.intersection(b) # Elements in a AND b
a.difference(b) # Elements in a but NOT in b
a.symmetric_difference(b) # Elements in a OR b but NOT both
a.issubset(b) # True if all elements of a are in b
a.issuperset(b) # True if all elements of b are in a
# Tuples are immutable lists
t = (1, 2, 3, 1)
# Essential Operations
t.count(1) # Count occurrences of value
t.index(2) # Find first index of value
# Useful Patterns
x, y = (1, 2) # Tuple unpacking
coords = [(1,2), (3,4)] # Tuple in collections
s = "hello world"
# Essential Methods
s.split() # Split on whitespace
s.split(',') # Split on comma
s.strip() # Remove leading/trailing whitespace
s.lower() # Convert to lowercase
s.upper() # Convert to uppercase
s.isalnum() # Check if alphanumeric
s.isalpha() # Check if alphabetic
s.isdigit() # Check if all digits
s.find('sub') # Index of substring (-1 if not found)
s.count('sub') # Count occurrences
s.replace('old', 'new') # Replace all occurrences
# ASCII Conversion
ord('a') # Char to ASCII (97)
chr(97) # ASCII to char ('a')
# Join Lists
''.join(['a','b']) # Concatenate list elements
# Iteration Helpers
enumerate(lst) # Index + value pairs
zip(lst1, lst2) # Parallel iteration
map(fn, lst) # Apply function to all elements
filter(fn, lst) # Keep elements where fn returns True
any(lst) # True if any element is True
all(lst) # True if all elements are True
# Binary Search (import bisect)
bisect.bisect(lst, x) # Find insertion point
bisect.bisect_left(lst, x)# Find leftmost insertion point
bisect.insort(lst, x) # Insert maintaining sort
# Type Conversion
int('42') # String to int
str(42) # Int to string
list('abc') # String to list
''.join(['a','b']) # List to string
set([1,2,2]) # List to set
# Math
abs(-5) # Absolute value
pow(2, 3) # Power
round(3.14159, 2) # Round to decimals
from functools import cmp_to_key
def compare(item1, item2):
# Return -1: item1 comes first
# Return 1: item2 comes first
# Return 0: items are equal
if item1 < item2:
return -1
elif item1 > item2:
return 1
return 0
# Sort using custom comparison
sorted_list = sorted(items, key=cmp_to_key(compare))
# Basic multiple input
x, y = input("Enter two values: ").split()
# Multiple integers
x, y = map(int, input("Enter two numbers: ").split())
# List of integers
nums = list(map(int, input("Enter numbers: ").split()))
# Multiple inputs with custom separator
values = input("Enter comma-separated values: ").split(',')
# List comprehension method
x, y = [int(x) for x in input("Enter two numbers: ").split()]
import math
# Constants
math.pi # 3.141592653589793
math.e # 2.718281828459045
# Common Functions
math.ceil(2.3) # 3 - Smallest integer greater than x
math.floor(2.3) # 2 - Largest integer less than x
math.gcd(a, b) # Greatest common divisor
math.log(x, base) # Logarithm with specified base
math.sqrt(x) # Square root
math.pow(x, y) # x^y (prefer x ** y for integers)
# Trigonometry
math.degrees(rad) # Convert radians to degrees
math.radians(deg) # Convert degrees to radians
# Binary representation
bin(10) # '0b1010'
format(10, 'b') # '1010' (without prefix)
# Division and Modulo
divmod(10, 3) # (3, 1) - returns (quotient, remainder)
# Negative number handling
x = -3
y = 2
print(x // y) # -2 (floor division)
print(int(x/y)) # -1 (preferred for negative numbers)
print(x % y) # 1 (Python's modulo with negative numbers)
def binary_search(arr, target):
"""
Find target in sorted array using binary search.
Args:
arr: Sorted list of numbers
target: Number to find
Returns:
Index of target or -1 if not found
"""
pass
# Use assertions for edge cases
assert binary_search([], 1) == -1, "Empty array should return -1"
assert binary_search([1], 1) == 0, "Single element array should work"
- Integer Division:
# Use int() for consistent negative number handling
print(-3//2) # Returns -2
print(int(-3/2)) # Returns -1 (usually desired)
- Default Dictionaries:
# Prefer defaultdict for frequency counting
from collections import defaultdict
freq = defaultdict(int)
for x in lst:
freq[x] += 1 # No KeyError if x is new
- Heap Priority:
# For custom priority in heapq, use tuples
heap = []
heapq.heappush(heap, (priority, item))
- List Comprehension:
# Often clearer than map/filter
squares = [x*x for x in range(10) if x % 2 == 0]
- String Building:
# Use join() instead of += for strings
chars = ['a', 'b', 'c']
word = ''.join(chars) # More efficient
- Using Sets for Efficiency:
# O(1) lookup for contains operations
seen = set()
if x in seen: # Much faster than list lookup
print("Found!")
- Custom Sort Keys:
# Sort by length then alphabetically
words.sort(key=lambda x: (len(x), x))
- Default Arguments Warning:
# Don't use mutable defaults
def bad(lst=[]): # This can cause bugs
lst.append(1)
return lst
def good(lst=None): # Do this instead
if lst is None:
lst = []
lst.append(1)
return lst
Made with β€οΈ for fellow leetcoders.