Study Guide
Python Basics
A reference guide to the core building blocks of Python — variables, data types, control flow, functions, and the data structures you'll reach for constantly.
01
Variables
Python is dynamically typed — you don't declare a type, it's inferred automatically.
name = "Alice" age = 25 height = 5.6 is_student = True
Naming rules
- Must start with a letter or underscore (not a number)
- Case-sensitive —
ageandAgeare different - Convention:
snake_casefor variables and functions
02
Data Types
| Type | Example | Description |
|---|---|---|
str | "hello" | Text |
int | 42 | Whole numbers |
float | 3.14 | Decimal numbers |
bool | True / False | Boolean values |
list | [1, 2, 3] | Ordered, changeable collection |
tuple | (1, 2, 3) | Ordered, unchangeable |
dict | {"name": "Alice"} | Key-value pairs |
set | {1, 2, 3} | Unordered, unique items |
Check a variable's type anytime with type(x).
print(type(42)) # <class 'int'> print(type("hi")) # <class 'str'>
03
Printing & Input
print("Hello, world!") name = input("What's your name? ") print(f"Hi, {name}!") # f-string: embeds variables directly in text
f-strings are the standard way to format strings in modern Python:
age = 25 print(f"You are {age} years old.") print(f"Next year you'll be {age + 1}.")
04
Operators
Arithmetic
3 + 2 # 5 addition 3 - 2 # 1 subtraction 3 * 2 # 6 multiplication 3 / 2 # 1.5 division (always returns float) 3 // 2 # 1 floor division (rounds down) 3 % 2 # 1 modulus (remainder) 3 ** 2 # 9 exponent
Comparison
3 == 2 # False 3 != 2 # True 3 > 2 # True 3 <= 2 # False
Logical
True and False # False True or False # True not True # False
05
Conditionals
Python uses indentation (not curly braces) to define code blocks. This is mandatory, not a style choice.
age = 18 if age >= 18: print("Adult") elif age >= 13: print("Teen") else: print("Child")
⚠️ Indentation matters. Mixing tabs and spaces, or inconsistent spacing, will cause errors.
06
Loops
for loop — iterate over a sequence
for i in range(5): print(i) # 0, 1, 2, 3, 4 fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit)
while loop — repeat while a condition is true
count = 0 while count < 5: print(count) count += 1
Loop control
for i in range(10): if i == 3: continue # skip this iteration if i == 7: break # exit the loop entirely print(i)
07
Lists
Ordered, changeable collections — the most commonly used data structure.
fruits = ["apple", "banana", "cherry"] fruits.append("date") # add to end fruits.remove("banana") # remove by value fruits.insert(0, "mango") # insert at position fruits.pop() # remove & return last item print(fruits[0]) # access by index print(fruits[-1]) # last item print(fruits[1:3]) # slice (items 1 and 2) print(len(fruits)) # number of items for fruit in fruits: print(fruit)
08
Dictionaries
Store data as key-value pairs — useful for structured, labeled data.
person = {"name": "Alice", "age": 25}
print(person["name"]) # access value by key
person["city"] = "Manila" # add a new key-value pair
person["age"] = 26 # update existing value
for key, value in person.items():
print(key, value)
09
Functions
Reusable blocks of code that take inputs and (optionally) return outputs.
def greet(name): return f"Hello, {name}!" message = greet("Alice") print(message)
Default arguments
def greet(name, greeting="Hello"): return f"{greeting}, {name}!" print(greet("Bob")) # Hello, Bob! print(greet("Bob", "Hi")) # Hi, Bob!
10
Comments
# This is a single-line comment """ This is a multi-line comment (often used as documentation) """
Quick Reference Cheat Sheet
| Concept | Syntax |
|---|---|
| Variable | x = 5 |
| String format | f"{x} items" |
| If statement | if x > 0: |
| For loop | for i in range(n): |
| While loop | while x < 10: |
| Function | def name(param): |
| List | [1, 2, 3] |
| Dictionary | {"key": "value"} |
| Comment | # comment |
Suggested Next Steps
- Practice writing small scripts that combine loops + conditionals (e.g., FizzBuzz)
- Learn list comprehensions:
[x*2 for x in range(5)] - Explore error handling with
try/except - Learn how to work with files (
open(), reading/writing text) - Get comfortable with built-ins:
len(),sum(),sorted(),max(),min()