Everything in one place
Use this chapter as a quick reference once you've worked through the rest of the course. It collects every core construct of Python into runnable, copy-pasteable snippets you can scan whenever you forget the exact syntax.
Hello world & variables
print("Hello, World!")
age = 25
name = "Aman"
is_student = True
print(name, age, is_student)Data types & operators
n = 42 # int
f = 3.14 # float
s = "hello" # str
b = True # bool
lst = [1, 2, 3] # list
tup = (1, 2) # tuple
dct = {"x": 1} # dict
nothing = None
a, b = 10, 3
print(a + b, a - b, a * b, a / b, a // b, a % b, a ** b)
print(a > b, a == b, a != b)
print(a > 5 and b < 5)Conditionals & loops
score = 72
if score >= 90:
print("A")
elif score >= 60:
print("Pass")
else:
print("Fail")
for i in range(5):
print("i =", i)
n = 0
while n < 3:
n += 1Functions & arrays
def add(a, b):
return a + b
square = lambda x: x * x
print(add(2, 3), square(5))
nums = [10, 20, 30]
nums.append(40)
doubled = [n * 2 for n in nums]
total = sum(nums)
print(doubled, total)Strings & objects
name = "PlayLearn"
print(len(name))
print(name.upper())
print("Learn" in name)
greet = f"Hello, {name}!"
class User:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f"Hi {self.name}"
u = User("Aman", 20)
print(u.greet())Errors, modules & I/O
try:
n = int("not a number")
except ValueError as e:
print("Bad input:", e)
# math_utils.py
def add(a, b):
return a + b
# main.py
from math_utils import add
print(add(2, 3))
name = input("Your name? ")
print("Hello,", name)
with open("notes.txt") as f:
text = f.read()How to use this page
Bookmark it. When you start a new project, open this cheatsheet alongside your editor and use it as a syntax lookup. Once a construct becomes second nature you'll stop needing the reference for it — and that's exactly when you know you've mastered that part of Python.
Practice and verify
Run reference snippets separately; they are not a single source file to concatenate. Check required imports and entry points for each.