Skip to content
AI Stack Nest
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

SyntaxWhat it does
x = 5Assigns integer 5 to x
name = "Alice"Assigns a string
pi = 3.14Assigns a float
active = TrueAssigns a boolean
type(x)Returns the type of x
x = NoneAssigns the null value

Strings

SyntaxWhat 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

SyntaxWhat 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

SyntaxWhat 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"] = 1Adds 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 dChecks if key exists

Control Flow

SyntaxWhat 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
breakExits the loop immediately
continueSkips to the next iteration

Functions

SyntaxWhat it does
def greet(name):Defines a function
return valueReturns a value and exits the function
def add(a, b=0):Parameter with a default value
greet("Alice")Calls the function
lambda x: x * 2Anonymous one-line function

File Handling

SyntaxWhat 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

FunctionWhat 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

More cheat sheets

Not sure your computer can run a model? Use the will-it-run checker.