2 min read · updated 18 Nov 2026
Python Basics Cheat Sheet
Common Python syntax for variables, data types, loops, functions, and file I/O
Variables and Data Types
| Syntax | What it does |
|---|
x = 5 | Assigns integer 5 to x |
name = "Alice" | Assigns a string |
pi = 3.14 | Assigns a float |
active = True | Assigns a boolean |
type(x) | Returns the type of x |
x = None | Assigns the null value |
Strings
| Syntax | What it does |
|---|
"hello" + " world" | Concatenates two strings |
len("hello") | Returns string length |
"hello".upper() | Returns "HELLO" |
"hello".strip() | Removes leading and trailing whitespace |
"hello".split(",") | Splits into a list on the delimiter |
"hello".replace("l", "r") | Returns "herro" |
f"Hi {name}" | f-string interpolation |
"hello"[0] | First character: "h" |
"hello"[1:3] | Slice: returns "el" |
"hello"[-1] | Last character: "o" |
Lists
| Syntax | What it does |
|---|
items = [1, 2, 3] | Creates a list |
items.append(4) | Adds 4 to the end |
items.pop() | Removes and returns last item |
items.insert(0, 9) | Inserts 9 at index 0 |
items.remove(2) | Removes first occurrence of 2 |
items[0] | First item (index 0) |
items[-1] | Last item |
items[1:3] | Slice: items at index 1 and 2 |
len(items) | Number of items |
items.sort() | Sorts list in place |
sorted(items) | Returns a new sorted list |
Dictionaries
| Syntax | What it does |
|---|
d = {"key": "value"} | Creates a dictionary |
d["key"] | Gets value by key (raises error if missing) |
d.get("key", "default") | Gets key, returns default if missing |
d["new"] = 1 | Adds or updates a key |
del d["key"] | Removes a key |
d.keys() | Returns all keys |
d.values() | Returns all values |
d.items() | Returns key-value pairs |
"key" in d | Checks if key exists |
Control Flow
| Syntax | What it does |
|---|
if x > 0: | Conditional block |
elif x == 0: | Else-if branch |
else: | Fallback branch |
for item in items: | Iterates over a list |
for i in range(5): | Loops 0 to 4 |
for i, v in enumerate(items): | Loop with index and value |
while x > 0: | Loops while condition is true |
break | Exits the loop immediately |
continue | Skips to the next iteration |
Functions
| Syntax | What it does |
|---|
def greet(name): | Defines a function |
return value | Returns a value and exits the function |
def add(a, b=0): | Parameter with a default value |
greet("Alice") | Calls the function |
lambda x: x * 2 | Anonymous one-line function |
File Handling
| Syntax | What it does |
|---|
open("f.txt", "r") | Opens for reading |
open("f.txt", "w") | Opens for writing (overwrites) |
open("f.txt", "a") | Opens for appending |
f.read() | Reads entire file as a string |
f.readlines() | Reads lines into a list |
f.write("text") | Writes text to the file |
Preferred pattern: always use with so the file is closed automatically:
with open("data.txt", "r") as f:
contents = f.read()
Useful Built-ins
| Function | What it does |
|---|
print(x) | Prints to the console |
input("msg") | Reads user input as a string |
int("5") | Converts string to int |
str(5) | Converts int to string |
range(start, stop, step) | Generates a sequence of numbers |
zip(a, b) | Pairs items from two iterables |
max(items) | Returns the largest item |
min(items) | Returns the smallest item |
sum(items) | Returns the sum of all items |