Python sheet
Interpreted language (no compilation)
Indentation defines code blocks
Comments: # single line, """ multiline """
Variables: dynamically typed → x = 10
Common types: int, float, str, bool, list, tuple, dict, set
OPERATORS
Arithmetic: + - * / // % **
Comparison: == != > < >= <=
Logical: and or not
CONDITIONALS
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")
LOOPS
for i in range(5):
print(i)
while x < 5:
x += 1
FUNCTIONS
def add(a, b):
return a + b
LISTS AND DICTIONARIES
nums = [1, 2, 3]
[Link](4)
info = {"name": "Guy", "age": 20}
print(info["name"])
STRING FORMATTING
f"Hello {name}"
CLASSES
class Car:
def init(self, model):
[Link] = model
def drive(self):
print([Link], "is driving")
MODULES
import math
print([Link](16))
SHORTCUTS
List comprehension: [x**2 for x in range(5)]
Lambda: f = lambda x: x + 1
Built-ins: len(), type(), range(), zip(), sum()