Benjamin Patch

Guides for Building Ethical & Impactful AI Software

Python Kickstart (part 3): Control Flow and Conditionals

Written by Benjamin Patch

Published:


Welcome back to our Python Kickstart series! In part 1, we covered Python installation, variables, and basic concepts. In part 2, we explored data types and data structures. Now we're ready to make our programs truly dynamic with control flow and conditionals.

Control flow is what transforms static code into intelligent programs that can make decisions and repeat actions based on conditions. Think of it as the director's vision that determines how a film's story unfolds - sometimes linear, sometimes nonlinear with flashbacks and/or parallel narratives, but always purposeful. Just as a great director uses different techniques to guide the audience through a narrative, Python's control flow statements guide your program through different logical paths.

Understanding Program Flow

Before diving into specific statements, it's important to understand that Python programs execute line by line, from top to bottom. Control flow statements allow you to change this natural order by:

Let's explore each of these powerful tools.

Conditional Statements: Teaching Programs to Decide

Conditional statements are the foundation of program logic. They allow your code to examine conditions and execute different blocks of code based on whether those conditions are true or false.

The if Statement

The simplest conditional statement is if. It executes code only when a condition is true:

# Movie recommendation system
imdb_rating = 9.2
movie_title = "The Godfather"

if imdb_rating > 9.0:
    print(f"{movie_title} is a certified masterpiece!")
    print("This film belongs in every cinephile's collection.")
    print("Highly recommended for serious film study.")

Notice several important things about this code:

  1. The colon (:) at the end of the if line - this is required syntax
  2. Indentation - everything indented under the if statement only executes when the condition is true
  3. The condition - imdb_rating > 9.0 evaluates to either True or False

Python uses indentation (typically 4 spaces) to group code blocks together. This is different from many other programming languages that use curly braces {}. The indented code forms a "block" that executes as a unit.

The if-else Statement

Often you want to execute one piece of code when a condition is true, and different code when it's false. This is where else comes in:

# Film festival submission guidelines
runtime_minutes = 195
festival_max_runtime = 180

if runtime_minutes <= festival_max_runtime:
    print("✓ This film meets the festival's runtime requirements.")
    print("Status: Approved for submission.")
    submission_fee = 75
else:
    print("✗ This film exceeds the festival's maximum runtime.")
    print("Status: Requires editing or alternate festival selection.")
    submission_fee = 0

print(f"Submission fee: ${submission_fee}")

The else clause provides an alternative path when the if condition is false. Exactly one of the two code blocks will execute - never both, never neither.

The if-elif-else Chain

For multiple conditions, use elif (short for "else if"). This allows you to test several conditions in sequence:

# Movie era classification system
release_year = 1962
director = "David Lean"

if release_year < 1930:
    era = "Silent Era"
    characteristics = "Visual storytelling, title cards, live musical accompaniment"
elif release_year < 1960:
    era = "Golden Age of Hollywood"
    characteristics = "Studio system, star power, Technicolor innovation"
elif release_year < 1980:
    era = "New Hollywood"
    characteristics = "Auteur theory, social commentary, experimental techniques"
elif release_year < 2000:
    era = "Modern Era"
    characteristics = "Digital effects, blockbuster mentality, franchise development"
else:
    era = "Contemporary Cinema"
    characteristics = "Streaming platforms, international perspectives, diverse voices"

print(f"'{director}' directed films during the {era}")
print(f"Key characteristics: {characteristics}")

# Additional context based on specific era
if era == "New Hollywood":
    print("This was a revolutionary period that challenged traditional filmmaking.")

Python evaluates elif conditions in order and executes the first block where the condition is true. Once a condition matches, the remaining elif and else blocks are skipped.

Comparison Operators: The Building Blocks of Conditions

Python provides several operators for making comparisons. Understanding these is crucial for writing effective conditional statements:

# Comprehensive movie analysis using comparison operators
citizen_kane_year = 1941
vertigo_year = 1958
lawrence_rating = 8.3
kane_rating = 8.3
casablanca_votes = 558_278
kane_votes = 442_988

print("=== MOVIE COMPARISON ANALYSIS ===")

# Equality and inequality
print(f"Same IMDB rating? {lawrence_rating == kane_rating}")
print(f"Different release years? {citizen_kane_year != vertigo_year}")

# Numerical comparisons
print(f"Vertigo released after Citizen Kane? {vertigo_year > citizen_kane_year}")
print(f"Kane released before 1950? {citizen_kane_year < 1950}")
print(f"Lawrence rating at least 8.0? {lawrence_rating >= 8.0}")
print(f"Kane rating 8.3 or lower? {kane_rating <= 8.3}")

# Practical application
age_difference = vertigo_year - citizen_kane_year
if age_difference > 15:
    print(f"These films span different cinematic generations ({age_difference} years apart)")

Logical Operators: Combining Conditions

Real-world decisions often depend on multiple factors. Python's logical operators and, or, and not let you combine conditions:

# Advanced movie filtering for awards consideration
movie_title = "Lawrence of Arabia"
runtime = 228
imdb_rating = 8.3
won_best_picture = True
director = "David Lean"
box_office_millions = 70.0

print(f"Analyzing: {movie_title}")

# Using 'and' - ALL conditions must be true
if runtime > 180 and imdb_rating > 8.0 and won_best_picture:
    print("✓ Qualifies as an Epic Masterpiece")
    print("  - Extended runtime showcases ambitious scope")
    print("  - High critical acclaim confirms artistic merit")
    print("  - Academy recognition validates cultural impact")

# Using 'or' - AT LEAST ONE condition must be true
if director == "David Lean" or director == "Akira Kurosawa" or director == "John Ford":
    print("✓ Directed by a Master of Epic Cinema")

# Using 'not' - condition must be FALSE
if not (box_office_millions > 100):
    print("✗ Not a major commercial success")
    print("  Note: Artistic merit often differs from box office performance")

# Complex logical combinations
is_prestigious = (won_best_picture and imdb_rating > 8.0) or (director in ["Kubrick", "Hitchcock", "Welles"])
if is_prestigious:
    print("✓ Considered a prestigious film by multiple criteria")

String Comparisons and the in Operator

Python can compare strings and check for membership using the in operator:

# Genre analysis and director style identification
movie_title = "North by Northwest"
genre_tags = ["thriller", "suspense", "espionage", "romance"]
director_style = "Alfred Hitchcock"

# String comparison (case-sensitive)
if director_style == "Alfred Hitchcock":
    print("Master of Suspense film detected!")

# Membership testing with 'in'
if "thriller" in genre_tags:
    print("This film will keep you on the edge of your seat")

if "romance" in genre_tags and "suspense" in genre_tags:
    print("Perfect blend of heart and tension")

# Substring checking in strings
film_title_lower = movie_title.lower()
if "north" in film_title_lower:
    print("Geographic reference in title suggests a journey narrative")

# Checking multiple possibilities
hitchcock_classics = ["Psycho", "Vertigo", "North by Northwest", "Rear Window", "The Birds"]
if movie_title in hitchcock_classics:
    print(f"'{movie_title}' is among Hitchcock's most celebrated works")

Loops: Repeating Actions Efficiently

Loops allow you to execute code multiple times without writing repetitive statements. They're essential for processing collections of data and performing repetitive tasks.

The for Loop: Iterating Over Collections

Use for loops when you want to process each item in a collection:

# Displaying a curated film festival lineup
festival_films = [
    "8½",
    "Persona",
    "The Rules of the Game",
    "Tokyo Story",
    "The 400 Blows"
]

print("CANNES RETROSPECTIVE: International Cinema Masterpieces")
print("=" * 55)

for film in festival_films:
    print(f"🎬 {film}")

    # Add specific context for certain films
    if film == "8½":
        print("   Federico Fellini's surreal meditation on creativity")
    elif film == "Tokyo Story":
        print("   Yasujirō Ozu's profound family drama")

print(f"\nTotal films in retrospective: {len(festival_films)}")

You can iterate over strings character by character:

# Analyzing film title composition
title = "CASABLANCA"
vowel_count = 0
consonant_count = 0

print(f"Analyzing the title: {title}")
print("Character breakdown:")

for character in title:
    if character in "AEIOU":
        print(f"  '{character}' - vowel")
        vowel_count += 1
    elif character.isalpha():
        print(f"  '{character}' - consonant")
        consonant_count += 1

print(f"\nSummary: {vowel_count} vowels, {consonant_count} consonants")
print(f"The title has a {vowel_count/len(title):.1%} vowel ratio")

Using range() for Controlled Iteration

The range() function generates sequences of numbers, perfect for controlled loops:

# Film festival countdown and ranking system
print("🎭 VENICE FILM FESTIVAL COUNTDOWN")
print("Premiere begins in...")

for seconds in range(5, 0, -1):  # Start at 5, stop before 0, step by -1
    print(f"   {seconds}...")

print("🎬 Action! Festival begins!")

# Ranking films with position numbers
critically_acclaimed = [
    ("Citizen Kane", 8.3),
    ("The Godfather", 9.2),
    ("8½", 8.0),
    ("Vertigo", 8.3)
]

print("\n🏆 CRITICS' CHOICE RANKINGS:")
for i in range(len(critically_acclaimed)):
    title, rating = critically_acclaimed[i]
    rank = i + 1

    # Add special notation for top positions
    if rank == 1:
        medal = "🥇"
    elif rank == 2:
        medal = "🥈"
    elif rank == 3:
        medal = "🥉"
    else:
        medal = f"{rank}."

    print(f"{medal} {title} (IMDB: {rating})")

The while Loop: Conditional Repetition

Use while loops when you want to repeat code as long as a condition remains true:

# Box office tracking simulation
film_title = "Lawrence of Arabia"
opening_weekend = 4.2  # millions (1962 dollars)
total_gross = 0.0
week = 1
target_gross = 15.0  # Break-even point

print(f"📊 BOX OFFICE TRACKING: {film_title}")
print("=" * 45)

while total_gross < target_gross:
    # Simulate weekly box office decline
    weekly_earnings = opening_weekend * (0.85 ** (week - 1))
    total_gross += weekly_earnings

    print(f"Week {week:2}: ${weekly_earnings:4.1f}M (Total: ${total_gross:5.1f}M)")

    # Weekly performance analysis
    if week == 1:
        print("         Strong opening weekend performance")
    elif weekly_earnings < 1.0:
        print("         Box office momentum slowing")

    week += 1

    # Safety break to prevent infinite loops
    if week > 20:
        print("         Extended theatrical run completed")
        break

print(f"\n🎯 Final gross: ${total_gross:.1f}M after {week-1} weeks")

if total_gross >= target_gross:
    print("✅ Film achieved commercial success!")
else:
    print("📈 Film built audience through word-of-mouth")

Loop Control: break and continue

Fine-tune loop execution with break and continue statements:

# Film database search with early termination
directors_and_films = [
    ("Christopher Nolan", "Dunkirk"),
    ("Martin Scorsese", "Goodfellas"),
    ("Stanley Kubrick", "2001: A Space Odyssey"),
    ("Akira Kurosawa", "Seven Samurai"),
    ("Christopher Nolan", "Inception"),
    ("Wong Kar-wai", "In the Mood for Love")
]

target_director = "Stanley Kubrick"
search_count = 0

print(f"🔍 Searching for films by {target_director}:")

for director, film in directors_and_films:
    search_count += 1

    # Skip films by other directors
    if director != target_director:
        print(f"   Skipping: {film} by {director}")
        continue

    # Found a match!
    print(f"✅ Found: '{film}' by {director}")
    print(f"   Search completed after checking {search_count} entries")
    break  # Exit loop after finding first match

else:
    # This 'else' clause runs only if the loop completes without 'break'
    print(f"❌ No films by {target_director} found in database")

print("🏁 Search operation finished")

Nested Control Structures

You can combine conditional statements and loops to create sophisticated program logic:

# Film festival programming system
festival_submissions = [
    {"title": "Amour", "country": "Austria", "runtime": 127, "genre": "Drama"},
    {"title": "Holy Motors", "country": "France", "runtime": 115, "genre": "Fantasy"},
    {"title": "The Master", "country": "USA", "runtime": 137, "genre": "Drama"},
    {"title": "Beyond the Hills", "country": "Romania", "runtime": 150, "genre": "Drama"}
]

print("🎪 FESTIVAL PROGRAMMING COMMITTEE REVIEW")
print("=" * 50)

accepted_films = []
rejected_films = []

for submission in festival_submissions:
    title = submission["title"]
    runtime = submission["runtime"]
    genre = submission["genre"]
    country = submission["country"]

    print(f"\n📽️  Reviewing: {title} ({country})")

    # Multiple criteria evaluation
    if runtime > 180:
        print("   ❌ Rejected: Exceeds maximum runtime")
        rejected_films.append(title)
        continue

    if genre == "Drama" and runtime > 120:
        print("   ⚠️  Long drama - requires special consideration")

        if country in ["France", "Italy", "Germany"]:
            print("   ✅ Accepted: European arthouse cinema")
            accepted_films.append(title)
        else:
            print("   📋 Under review: Non-European long drama")
    elif runtime <= 120:
        print("   ✅ Accepted: Appropriate runtime")
        accepted_films.append(title)
    else:
        print("   📋 Under review: Standard evaluation needed")

print(f"\n📊 PROGRAMMING RESULTS:")
print(f"   Accepted: {len(accepted_films)} films")
print(f"   Rejected: {len(rejected_films)} films")

if accepted_films:
    print("   🎬 Accepted films:")
    for film in accepted_films:
        print(f"      • {film}")

Common Mistakes and How to Avoid Them

Understanding common pitfalls will help you write more reliable code:

Indentation Errors

# ❌ Incorrect - will cause IndentationError
rating = 8.5
if rating > 8.0:
print("Excellent film!")  # Not indented!

# ✅ Correct indentation
if rating > 8.0:
    print("Excellent film!")
    print("Highly recommended!")

Assignment vs. Comparison

# ❌ Common mistake - using assignment (=) instead of comparison (==)
director = "Kubrick"
if director = "Kubrick":  # SyntaxError!
    print("Master filmmaker")

# ✅ Correct comparison
if director == "Kubrick":
    print("Master filmmaker")

Infinite Loops

# ❌ Dangerous - infinite loop
counter = 1
while counter <= 5:
    print(f"Week {counter}")
    # Forgot to increment counter!

# ✅ Safe loop with proper increment
counter = 1
while counter <= 5:
    print(f"Week {counter}")
    counter += 1  # Essential!

What's Next?

You now have the tools to create programs that make intelligent decisions and efficiently process data through repetition. Control flow and conditionals are fundamental to virtually every meaningful program you'll write.

In our next installment, Python Kickstart Part 4: Functions, we'll explore how to organize your code into reusable blocks, making your programs more modular, maintainable, and powerful. Functions will allow you to avoid repetition and create more sophisticated applications by breaking complex problems into manageable pieces.

Practice Suggestions

Before moving on, try building these projects to reinforce your understanding:

Remember, the key to mastering these concepts is hands-on practice. Start with simple examples and gradually build more complex logic. Each conditional statement and loop you write strengthens your programming intuition.

The more comfortable you become with control flow, the more naturally you'll think in terms of program logic - and the better equipped you'll be to understand, debug, and improve any Python code you encounter.

Happy coding, and I'll see you in Part 4!