Fundamentals — Cheat Sheet
Python A–Z · 6 topics. Download the PDF or the Instagram carousel and share it.
Python Introduction — Why Python?
Python is a high-level, dynamically typed, interpreted language famous for readable syntax and a massive ecosystem — the default choice for backend APIs, automation, data engineering, and AI.
- ✓Python is interpreted and dynamically typed — types live on objects, not variables
- ✓Indentation IS the syntax — blocks are defined by consistent spaces (use 4)
- ✓Batteries included: a huge standard library + PyPI ecosystem (pip install)
- ✓CPython is the reference implementation; "Python is slow" usually means CPython bytecode interpretation — real apps offload hot paths to C libraries (NumPy) or async I/O
# hello.py — this is a comment
print("Hello, AiCanCode!")
name = "Akshay" # no type declaration
age = 24 # int, inferred
print(name, "is", age) # Hello-style printing
# Run it:
# $ python hello.pyInstalling Python, pip & Virtual Environments
Every real Python project isolates its dependencies in a virtual environment (venv) and installs packages with pip — the #1 habit that separates beginners from professionals.
- ✓One project = one venv. Always. Activate before installing anything
- ✓requirements.txt (pip freeze) makes environments reproducible — commit it, never commit .venv
- ✓pip installs from PyPI; pip install -U upgrades; pip uninstall removes
- ✓"ModuleNotFoundError but I installed it" almost always means wrong interpreter/venv active
# Create and activate a virtual environment python -m venv .venv # Windows: .venv\Scripts\activate # macOS/Linux: source .venv/bin/activate # Install packages INSIDE the venv pip install fastapi uvicorn requests # Save exact versions for teammates/servers pip freeze > requirements.txt # On another machine — recreate everything pip install -r requirements.txt # Leave the environment deactivate
Syntax, Variables & Dynamic Typing
Python variables are names bound to objects. Indentation defines blocks, snake_case is the convention, and everything — numbers, strings, functions — is an object.
- ✓Assignment binds a name to an object — it never copies
- ✓== compares values; is compares identity (same object in memory)
- ✓snake_case functions/variables, PascalCase classes, UPPER_CASE constants (PEP 8)
- ✓Use 4 spaces per indent level; never mix tabs and spaces
a = [1, 2, 3] b = a # b now points to the SAME list (no copy!) b.append(4) print(a) # [1, 2, 3, 4] — a sees the change c = [1, 2, 3, 4] print(a == c) # True — same VALUE print(a is c) # False — different OBJECTS print(a is b) # True — same object # Multiple assignment & swap (no temp variable needed) x, y = 10, 20 x, y = y, x # swap in one line print(x, y) # 20 10
Numbers, Strings & f-strings
Python ints have unlimited precision, floats are IEEE-754 doubles, and strings are immutable sequences with a rich method set — formatted beautifully with f-strings.
- ✓int is arbitrary precision — 2**1000 works; no integer overflow in Python
- ✓/ always returns float; // floors (careful: -7 // 2 == -4, unlike Java)
- ✓Strings are immutable — s[0] = "x" is a TypeError; use join() to build strings in loops
- ✓f-strings: f"{value:.2f}", f"{x=}" for debug, f"{n:,}" for thousands separators
print(7 / 2) # 3.5 — true division, always float
print(7 // 2) # 3 — floor division
print(-7 // 2) # -4 — floors toward negative infinity! (Java: -3)
print(7 % 2) # 1
print(2 ** 100) # 1267650600228229401496703205376 — no overflow
# Float precision — never compare floats with ==
print(0.1 + 0.2 == 0.3) # False!
import math
print(math.isclose(0.1 + 0.2, 0.3)) # True — the right way
# Money? Use Decimal
from decimal import Decimal
print(Decimal("0.1") + Decimal("0.2")) # 0.3 exactlyType Conversion & Reading Input
input() always returns a string — converting between str, int, float, list and friends explicitly is how Python programs take and validate data, and how every DSA judge feeds your code.
- ✓input() ALWAYS returns str — convert explicitly with int()/float()
- ✓list(map(int, input().split())) is the standard array-reading idiom
- ✓Truthiness: 0, "", [], {}, set(), None are falsy — write "if items:" not "if len(items) > 0:"
- ✓For multi-line judge input, sys.stdin.read().split() is the safest pattern
int("42") # 42
float("3.14") # 3.14
str(99) # "99"
int("4.2") # ValueError! use int(float("4.2")) -> 4
int("ff", 16) # 255 — base conversion
list("abc") # ['a', 'b', 'c']
set([1, 2, 2, 3])# {1, 2, 3} — dedupe trick
# Truthiness — what bool() says
bool(0), bool(""), bool([]), bool(None) # all False
bool(42), bool("hi"), bool([0]) # all True
# so instead of: if len(items) > 0:
if items: # pythonic
print("has data")Operators, Comparisons & Short-Circuiting
Python operators read like English — and, or, not — with short-circuit evaluation, chained comparisons (0 < x < 10), and identity/membership operators (is, in) that interviews love.
- ✓Comparisons chain: 0 < x < 10 is valid and efficient
- ✓and/or short-circuit AND return an operand, not a boolean — enables "value or default"
- ✓in is O(n) for list/tuple, O(1) average for set/dict — say this in interviews
- ✓Ternary: "yes" if condition else "no"
age = 25
print(18 <= age <= 60) # True — chained, reads like math
nums = [3, 7, 1]
print(7 in nums) # True O(n) on list
print(7 in set(nums)) # True O(1) avg on set
user = {"name": "Ravi", "role": "student"}
print("role" in user) # True — checks KEYS
print("Can" in "AiCanCode") # True — substring check
# not in reads naturally
if "admin" not in user:
print("regular user")