Skip to main content
Albero Academy

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.

All sheets
PDF quick reference

PythonCheat sheet

Sections
7
Snippets
16
Pages
2
01

Variables & data types

Common scalars
x = 42 # int
rate = 1.18 # float
name = "Aanya" # str
flag = True # bool
empty = None # NoneType
Type conversion
int("12") + 1 # 13
float("87.5") # 87.5
str(2026) # "2026"
bool(0) # False
02

Strings & f-strings

Slicing & methods
s = "Albero Academy"
s[:6] # "Albero"
s.lower() # "albero academy"
s.replace("a", "@")
s.split(" ") # ["Albero", "Academy"]
f-string formatting
price = 199.5
f"₹{price:,.2f}" # ₹199.50
f"{price * 1.18:.0f}" # 235
03

Lists, dicts, sets

List comprehensions
[n * n for n in range(5)]
# [0, 1, 4, 9, 16]
[w for w in words if len(w) > 3]
Dict comprehensions
{x: x**2 for x in range(4)}
# {0: 0, 1: 1, 2: 4, 3: 9}
Set ops
a | b # union
a & b # intersection
a - b # difference
04

Control flow

if / elif / else
if score >= 90:
grade = "A+"
elif score >= 75:
grade = "A"
else:
grade = "B"
for + enumerate + zip
for i, v in enumerate(items):
print(i, v)
for a, b in zip(xs, ys):
print(a, b)
05

Functions

Defaults & *args / **kwargs
def gst(amount, rate=0.18):
return amount * (1 + rate)
def log(*args, **kw):
print(args, kw)
Lambda + map / filter
list(map(lambda x: x * 2, nums))
list(filter(lambda x: x > 0, nums))
06

File I/O

Read file
with open("data.csv") as f:
text = f.read()
Write JSON
import json
with open("out.json", "w") as f:
json.dump(payload, f, indent=2)
07

Pandas one-liners

Read & inspect
import pandas as pd
df = pd.read_csv("sales.csv")
df.head()
df.info()
df.describe()
Filter & group
df[df["region"] == "North"]
df.groupby("region")["revenue"].sum()
Apply & transform
df["margin"] = df["revenue"] - df["cost"]
df["bucket"] = df["amount"].apply(
lambda x: "high" if x > 1000 else "low"
)