Python Basics & Flow Control
Install Python, write your first programs, and learn how to make decisions in code with if/else and loops.
Resources
Why Python?
Python is the best first "real" programming language for several reasons:
- It reads like English:
if age >= 18: print("You can vote")— you can almost read this without knowing any Python. - It's incredibly useful: Data analysis, automation, web scraping, AI, web development — Python does it all.
- It's what professionals use: Google, Netflix, Instagram, and thousands of companies run on Python.
You started learning Python before but didn't finish. That's completely normal. This time, we'll connect every concept to something real you can use in your work.
Installing Python
Before anything else, install Python on your computer:
- Go to python.org/downloads
- Download the latest version (3.12 or newer)
- Important on Mac: Python might already be installed, but make sure you have Python 3 (type
python3 --versionin Terminal)
To run Python interactively, open Terminal and type python3. You'll see the >>> prompt — this is the Python interactive shell where you can type code and see results immediately.
Expressions and Data Types
In Python, an expression is anything that produces a value:
2 + 3 # → 5 (integer math)
"Hello" # → "Hello" (a string of text)
10 / 3 # → 3.3333 (float — decimal number)
10 // 3 # → 3 (integer division — rounds down)
10 % 3 # → 1 (modulo — the remainder)
2 ** 10 # → 1024 (exponent — 2 to the power of 10)
Data types are the categories of values:
- int: Whole numbers →
42,-7,0 - float: Decimal numbers →
3.14,-0.5 - str: Text (strings) →
"Hello",'World' - bool: True or False →
True,False
You can check a value's type: type(42) returns <class 'int'>
Variables — Storing Values
A variable is a name that points to a value. Think of it as a labeled box:
client_name = "Lamaria Honey"
deal_value = 50000
is_exported = True
print(client_name) # → Lamaria Honey
print(deal_value * 2) # → 100000
Variable names should be descriptive. x = 50000 works but deal_value = 50000 tells you what the number means. This matters when you or someone else reads the code later.
Rules for variable names:
- Can contain letters, numbers, underscores
- Cannot start with a number
- Python convention: use
snake_case(words separated by underscores)
String Operations
Strings (text) have special operations:
first = "Lia"
last = "Kereselidze"
# Concatenation (joining strings)
full_name = first + " " + last # → "Lia Kereselidze"
# Replication
line = "-" * 30 # → "------------------------------"
# f-strings (modern way to insert variables into text)
message = f"Hello, {first}! Your deal is worth ${deal_value}."
f-strings are incredibly useful. Instead of awkward concatenation, you just put variables inside {} within a string that starts with f.
Flow Control — Making Decisions
Real programs need to make decisions. Flow control is how your code chooses what to do.
Boolean Values and Comparisons
10 > 5 # → True
10 == 10 # → True (note: == checks equality, = assigns)
10 != 5 # → True (not equal)
"abc" == "ABC" # → False (case sensitive)
if / elif / else
deal_value = 50000
if deal_value >= 100000:
print("Major deal — escalate to director")
elif deal_value >= 50000:
print("Significant deal — prepare presentation")
elif deal_value >= 10000:
print("Standard deal — proceed normally")
else:
print("Small deal — handle directly")
Indentation matters in Python! The code inside an if block must be indented (4 spaces). This is how Python knows which code belongs to which block.
while Loops
A while loop repeats as long as a condition is true:
attempts = 0
while attempts < 3:
print(f"Attempt {attempts + 1}")
attempts = attempts + 1
# prints: Attempt 1, Attempt 2, Attempt 3
Warning: If the condition never becomes false, your loop runs forever. Always make sure something changes inside the loop.
for Loops
A for loop repeats for each item in a sequence:
countries = ["France", "Belgium", "Turkey", "Qatar"]
for country in countries:
print(f"Exporting to {country}")
break and continue
breakexits the loop immediatelycontinueskips to the next iteration
for country in countries:
if country == "Turkey":
print("Skipping Turkey — sanctions")
continue
print(f"Shipping to {country}")
What to Do This Week
- Install Python if you haven't already
- Read Chapters 1 and 2 of Automate the Boring Stuff (links above)
- Practice in the interactive shell: Open Terminal, type
python3, and try every code example above. Change the values. Break things. Fix them. - Write a small script: Create a file called
export_checker.pyand write a program that asks for a deal value and tells you how to handle it (using the if/elif/else pattern above). - Take the quiz below.
Quiz
What is the result of `10 % 3` in Python?