Tutorial

Python Basics Workbook: Control Flow and Functions With 20 Drills

Master Python basics — if/elif/else, for/while loops, functions, and return values — with 20 bite-size drills and solutions.

Anuj SainiAug 23, 20265 min read

Control flow and functions are the gate to automation. This workbook makes if/elif/else, for/while, and def automatic through 20 bite-size drills referenced to Bro Code video visuals.

What 20 drills are inside and who finishes them fastest?

Part 1 If statements (6 Q) -> While loops -> For loops -> Functions -> Return vs print, each with a line starter like age = 20 so you only write the branch or loop body. If you already pass String assignments and Lists and dicts, you will finish in one sitting. Otherwise do those first — this workbook assumes slicing and append fluency.

Ingredients: 20 prompts with variable stubs pre-declared; no dataset, pure logic.

How do branches work (if / elif / else)?

python
# Q1: Age check
age = 20
if age >= 18:
    print("You are an adult")
 
# Q2: Pass/Fail
score = 45
if score >= 50:
    print("Pass")
else:
    print("Fail")  # prints Fail for 45
 
# Q3: Grading with elif
grade = 85
if grade >= 90:
    print("A")
elif grade >= 80:
    print("B")  # 85 -> B
elif grade >= 70:
    print("C")
else:
    print("F")
 
# Q4-Q5: String compare + AND/OR
password = "guest"
if password == "secret123":
    print("Access Granted")
else:
    print("Access Denied")
 
# Logical combo
if 18 <= age < 65 and score >= 50:
    print("Eligible")

Rendered output for the starters: Q1 prints "You are an adult", Q2 prints "Fail", Q3 prints "B", Q4 prints "Access Denied" — each one-line branch maps to the video timestamp cited in the prompt.

How do loops and functions click?

python
# Loops — prefer for over while when you know the iterable
for i in range(3):
    print(i)  # 0 1 2
 
# While — stops when condition flips
n=3
while n>0:
    print(f"countdown {n}")
    n-=1
 
# Slice a string inside a loop
for ch in "Topfolio":
    if ch.lower() in "aeiou":
        print(ch)
 
# Functions — return vs print
def add(a, b):
    return a + b
 
x = add(2, 3)
print(x)  # 5 — usable value
 
def greet(name):
    return f"Hello, {name}!"
 
print(greet("Alice"))
 
def is_adult(age):
    return age >= 18
 
print(is_adult(20))
Feature / Criteria

Gotcha: Mutable Default Arguments

def add_item(item, lst=[]) reuses the same list across calls — the second call appends to the first's leftovers. Always write def add_item(item, lst=None): lst = [] if lst is None else lst. The workbook shows a failing hidden test until this fix.

What do you do after basics?

Translate every loop into a vectorised Pandas or NumPy verb where possible: loops become masks and groupby. Continue to Python fundamentals master 30Q for strings/regex, then to Pandas fundamentals 40Q to replace loops with DataFrames.


Download the Notebook and Practise

This article is a walkthrough of a runnable Jupyter notebook. Download the original .ipynb and run it locally or on Colab — every code block above appears in order.

Download the Python Basics Workbook Notebook

Get the complete .ipynb with outputs — runs on any Python 3.10+ environment with pandas, numpy, and the libraries listed in setup.

Download .ipynb

Continue your track: Data Analyst Roadmap · Python and Pandas Guide · SQL NULL Handbook · SQL JOIN Fan-Out · Topfolio Practice · Data Analyst vs Engineer

Dataset generators where applicable are in courses/workbooks/generators/ — see citations atop for the exact *.py source for this notebook.


Frequently Asked Questions

What is the difference between == and is in Python?

== compares values, is compares identity (same object). Use == for numbers/strings; is only for None checks (x is None).

When do you use for vs while loops?

for when you know the iterable (for x in list), while when you loop until a condition flips. Prefer for for readability.

What does return do vs print in a function?

return hands a value back to the caller for assignment; print only displays. def add(a,b): return a+b lets x=add(2,3).

Why does my function see the wrong variable?

Python LEGB scope: local -> enclosing -> global -> builtin. Reusing a global name inside a function without global creates a shadowing local.

Frequently Asked Questions

What is the difference between == and is in Python?

== compares values, is compares identity (same object). Use == for numbers/strings; is only for None checks (x is None).

When do you use for vs while loops?

for when you know the iterable (for x in list), while when you loop until a condition flips. Prefer for for readability.

What does return do vs print in a function?

return hands a value back to the caller for assignment; print only displays. def add(a,b): return a+b lets x=add(2,3).

Why does my function see the wrong variable?

Python LEGB scope: local -> enclosing -> global -> builtin. Reusing a global name inside a function without global creates a shadowing local.

Anuj Saini

Written by

Anuj SainiFounder & Lead Instructor

Founder at Topfolio with 6+ years in data & analytics across JPMC, Ultrahuman, and high-growth startups. Sat on hiring panels, reviewed 500+ resumes, and writes practical SQL & data guides.