Python Cheat Sheet
Syntax · Pandas · One-liners
Python syntax, list/dict/set operations, comprehensions, f-strings, file I/O and the pandas one-liners you will reach for daily — on a single page.
PDF quick reference
PythonCheat sheet
- Sections
- 7
- Snippets
- 16
- Pages
- 2
01
Variables & data types
x = 42 # intrate = 1.18 # floatname = "Aanya" # strflag = True # boolempty = None # NoneType
int("12") + 1 # 13float("87.5") # 87.5str(2026) # "2026"bool(0) # False
02
Strings & f-strings
s = "Albero Academy"s[:6] # "Albero"s.lower() # "albero academy"s.replace("a", "@")s.split(" ") # ["Albero", "Academy"]
price = 199.5f"₹{price:,.2f}" # ₹199.50f"{price * 1.18:.0f}" # 235
03
Lists, dicts, sets
[n * n for n in range(5)]# [0, 1, 4, 9, 16][w for w in words if len(w) > 3]
{x: x**2 for x in range(4)}# {0: 0, 1: 1, 2: 4, 3: 9}
a | b # uniona & b # intersectiona - b # difference
04
Control flow
if score >= 90:grade = "A+"elif score >= 75:grade = "A"else:grade = "B"
for i, v in enumerate(items):print(i, v)for a, b in zip(xs, ys):print(a, b)
05
Functions
def gst(amount, rate=0.18):return amount * (1 + rate)def log(*args, **kw):print(args, kw)
list(map(lambda x: x * 2, nums))list(filter(lambda x: x > 0, nums))
06
File I/O
with open("data.csv") as f:text = f.read()
import jsonwith open("out.json", "w") as f:json.dump(payload, f, indent=2)
07
Pandas one-liners
import pandas as pddf = pd.read_csv("sales.csv")df.head()df.info()df.describe()
df[df["region"] == "North"]df.groupby("region")["revenue"].sum()
df["margin"] = df["revenue"] - df["cost"]df["bucket"] = df["amount"].apply(lambda x: "high" if x > 1000 else "low")
