Python Cheat Sheet
On This Page
Bonus Chapter: Python Cheat Sheet
A quick, print-friendly reference for the core Python syntax covered in Modules 1-12. Pin this to your desk while practicing!
CS1.1 Variables & Data Types
| Type | Example |
|---|
| int | age = 28 |
| float | price = 499.99 |
| str | name = "Samantus" |
| bool | is_active = True |
| Type check | type(x) |
| Type cast | int(x), float(x), str(x) |
CS1.2 Operators
| Category | Operators |
|---|
| Arithmetic | + - * / // % ** |
| Comparison | == != > < >= <= |
| Logical | and or not |
| Assignment | = += -= *= /= |
| Membership | in / not in |
| Identity | is / is not |
CS1.3 Control Flow
if condition:
...
elif other_condition:
...
else:
...
for item in sequence:
...
while condition:
...
# break, continue, pass control loop flow
CS1.4 Functions
def greet(name, course="SEO"): # default parameter
return f"{name} - {course}"
square = lambda x: x * x # lambda function
def factorial(n): # recursion
if n <= 1: return 1
return n * factorial(n-1)
CS1.5 Data Structures Quick Reference
| Structure | Syntax | Mutable? | Ordered? |
|---|
| List | [1,2,3] | Yes | Yes |
| Tuple | (1,2,3) | No | Yes |
| Set | {1,2,3} | Yes | No |
| Dict | {'a':1} | Yes | Yes (3.7+) |
CS1.6 String Methods
| Method | Purpose |
|---|
| upper() / lower() | Change case |
| strip() | Remove leading/trailing spaces |
| split(sep) | Split into a list |
| join(list) | Join list into a string |
| replace(a,b) | Replace substring |
| find(sub) | Find index of substring |
| startswith() / endswith() | Check prefix/suffix |
| f-string | f"{variable}" |
CS1.7 List Methods
| Method | Purpose |
|---|
| append(x) | Add to end |
| insert(i,x) | Insert at index |
| remove(x) | Remove first match |
| pop(i) | Remove & return item at index |
| sort() / reverse() | Sort or reverse in place |
| index(x) / count(x) | Find index / count occurrences |
| List comprehension | [x*2 for x in nums if x>0] |
CS1.8 Exception Handling
try:
risky_code()
except ValueError as e:
print("Error:", e)
else:
print("No errors occurred")
finally:
print("Always runs")
ℹ Reminder
Full explanations and worked examples for every item on this sheet are in Modules 1-12 of the main training manual.