Syntax, Types, and Control Flow
What You'll Learn
Python's core syntax rules, the five built-in types every program uses, and how to control program flow with if, for, and while.
Python Syntax Rules
1. Indentation defines blocks
# Correct: 4 spaces
if x > 0:
print("positive")
x = x - 1
# Wrong: no indentation
if x > 0:
print("this causes IndentationError")
2. Colons start blocks
Every if, for, while, def, class ends with ::
if x > 0: # colon required
...
for i in range(3): # colon required
...
def greet(): # colon required
...
3. One statement per line (usually)
x = 1
y = 2
z = x + y # keep it readable
Semicolons work but are not Pythonic:
x = 1; y = 2 # ❌ avoid this style
The Five Core Types
str — Text
name = "Alice"
greeting = 'Hello, World!'
multiline = """Line 1
Line 2"""
print(type(name)) # <class 'str'>
print(len(name)) # 5
print(name[0]) # A (indexing)
print(name.upper()) # ALICE
int — Whole Numbers
age = 25
year = 2024
negative = -10
print(type(age)) # <class 'int'>
print(age + 5) # 30
print(age ** 2) # 625 (power)
print(age // 7) # 3 (floor division)
print(age % 7) # 4 (remainder)
float — Decimal Numbers
price = 19.99
pi = 3.14159
print(type(price)) # <class 'float'>
print(round(price, 1)) # 20.0
bool — True or False
is_active = True
is_admin = False
print(type(is_active)) # <class 'bool'>
print(True + True) # 2 (True == 1, False == 0)
NoneType — The Absence of a Value
result = None # "nothing here yet"
print(type(result)) # <class 'NoneType'>
print(result is None) # True (use `is`, not `==`)
Control Flow: if / elif / else
score = 75
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
else:
grade = "F"
print(f"Score {score} → Grade {grade}")
# Score 75 → Grade C
Comparison operators:
| Operator | Meaning | Example |
|---|---|---|
== | equals | x == 5 |
!= | not equals | x != 5 |
> | greater than | x > 5 |
< | less than | x < 5 |
>= | greater or equal | x >= 5 |
<= | less or equal | x <= 5 |
Logical operators:
age = 25
has_ticket = True
if age >= 18 and has_ticket:
print("Allowed")
if age < 18 or not has_ticket:
print("Not allowed")
Control Flow: for Loops
Loop over any sequence:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
# apple
# banana
# cherry
Loop a fixed number of times:
for i in range(5):
print(i)
# 0 1 2 3 4
for i in range(1, 6):
print(i)
# 1 2 3 4 5
for i in range(0, 10, 2):
print(i)
# 0 2 4 6 8
Control Flow: while Loops
Repeat while a condition is True:
count = 0
while count < 5:
print(count)
count += 1
# 0 1 2 3 4
break exits the loop early:
while True:
user = input("Enter 'quit' to stop: ")
if user == "quit":
break
print(f"You typed: {user}")
continue skips to the next iteration:
for i in range(10):
if i % 2 == 0:
continue # skip even numbers
print(i) # prints 1 3 5 7 9
Combining Types and Control Flow
temperatures = [22, 35, 18, 41, 29]
hot_days = 0
for temp in temperatures:
if temp > 30:
hot_days += 1
print(f"Hot day: {temp}°C")
print(f"\nTotal hot days: {hot_days}")
Output:
Hot day: 35°C
Hot day: 41°C
Total hot days: 2
Common Mistakes
| Mistake | Example | Fix |
|---|---|---|
Using = instead of == | if x = 5: | Use == for comparison |
| Infinite while loop | while True: with no break | Always have a break condition |
| Off-by-one in range | range(5) gives 0–4, not 1–5 | Use range(1, 6) for 1–5 |
Comparing None with == | if x == None: | Use if x is None: |
Quick Reference
# Types
str, int, float, bool, None
# if/elif/else
if condition:
...
elif other:
...
else:
...
# for
for item in iterable:
...
for i in range(n):
...
# while
while condition:
...
break # exit loop
continue # next iteration