0% found this document useful (0 votes)
2 views12 pages

Python Control Flow

This document covers Python control flow concepts for beginners, including conditional statements (if/elif/else), loops (for and while), and loop control statements (break, continue, pass). It explains how control flow allows programs to make decisions and repeat actions, providing examples and best practices. Additionally, it introduces nested control flow and offers a quick reference cheatsheet for the discussed topics.

Uploaded by

chaitanyakommu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views12 pages

Python Control Flow

This document covers Python control flow concepts for beginners, including conditional statements (if/elif/else), loops (for and while), and loop control statements (break, continue, pass). It explains how control flow allows programs to make decisions and repeat actions, providing examples and best practices. Additionally, it introduces nested control flow and offers a quick reference cheatsheet for the discussed topics.

Uploaded by

chaitanyakommu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

BEGINNER SERIES · SESSION 3

Python
Control Flow
for Beginners
if / elif / else for loops while loops break / continue

Make your programs think and repeat — like a real programmer!

Python Programming · Control Flow · Conditionals · Loops · Loop Control


What is Control Flow? while Loops

01 04
Decision-making in programs Repeat while condition is True

WHAT
WE'LL if / elif / else break / continue / pass

COVER 02
Conditional branching
05
Loop control statements

Python Control Flow


for Loops Nested Control Flow

03 06
Iterating over sequences Loops inside loops & more
01 What is Control Flow?

Control Flow is the order in which Python executes statements. Instead of running top-to-bottom always, programs can make
decisions and repeat actions.

Without Control Flow With Conditionals With Loops

Line 1: print('Hi') age >= 18? for i in range(3):


print(i) # 0
Line 2: print('Bye') YES NO
↓ print(i) # 1
Line 3: print('Done') 'Adult' 'Minor'

print(i) # 2

↻ repeats automatically
02 if / elif / else — Conditional Statements

if elif else
Runs if condition is True Else-if: another condition Runs if nothing else matched

[Link] Rules & Tips

score = 75 1️⃣ if is REQUIRED — always first

if score >= 90: 2️⃣ elif is OPTIONAL — use 0 or many


print('Grade: A ')
3️⃣ else is OPTIONAL — must be last
elif score >= 75:
print('Grade: B ') Always indent body 4 spaces
elif score >= 60:
print('Grade: C ') End condition with colon :
else:
Only first True block runs
print('Grade: F ')

Combine with: and, or, not


# Output: Grade: B
02+ Operators Used in Conditions

Comparison Operators Logical Operators

x == y Equal to 5 == 5 → True BOTH conditions True

and age>18 and citizen

x != y Not equal to 5 != 3 → True → True only if both

x > y Greater than 7 > 4 → True AT LEAST one True

or sun or umbrella

x < y Less than 3 < 8 → True → True if either

x >= y Greater than or equal 5 >= 5 → True FLIPS True False

not not is_raining

x <= y Less than or equal 4 <= 7 → True → inverts result


03 for Loops — Iterate Over Sequences

for item in sequence : # indented body runs each iteration


keyword loop var keyword iterable

Loop over a list range() function Loop over a string

fruits = ['apple','mango'] for i in range(5): for ch in "Hello":

for fruit in fruits: print(i) print(ch)

print(fruit)

# 0 1 2 3 4 # H e l l o

# apple

# mango range(1, 6) → 1..5 # Each char one turn


03+ The range() Function — Your Loop Counter

range(stop) range(start, stop)

range(5) range(2, 7)

→ 0, 1, 2, 3, 4 → 2, 3, 4, 5, 6

Starts at 0 by default Custom start point

range(start,stop,step) range(step negative)

range(0,10,2) range(5, 0, -1)

→ 0, 2, 4, 6, 8 → 5, 4, 3, 2, 1

Count by 2s (step) Count DOWN!

Tip: range() generates numbers on-the-fly — it doesn't create a list in memory, making it very efficient for large counts!
04 while Loops — Repeat Until Condition is False

while condition : # body runs again and again while condition stays True

while_counter.py while_input.py

# Count from 1 to 5 # Keep asking until correct


count = 1 password = ""

while count <= 5: while password != "secret":


print(count) password = input(
count += 1 # MUST update! "Enter password: ")

# 1 print(" Access granted!")


# 2
# 3 4 5 # for vs while:
# for → known iterations
# Forget count+=1? # while → unknown iterations
# → INFINITE LOOP!
05 break / continue / pass — Loop Control

break continue pass


EXITS the loop immediately SKIPS current iteration DOES NOTHING (placeholder)

Stop when target found Jump over certain items Empty block to avoid errors

for n in range(10): for n in range(6): for n in range(5):

if n == 5: if n == 3: if n == 2:

break continue pass # TODO later

print(n) print(n) print(n)

# 0 1 2 3 4 # 0 1 2 4 5 # 0 1 2 3 4

# stops at 5! # skips 3! # all print — pass silent!


Nested Control Flow — Loops & Conditions inside each
06 other

Nested for Loops if inside for — Filter Pattern

# Multiplication table students = [


for i in range(1, 4): ('Alice', 88),
for j in range(1, 4): ('Bob', 62),
print(i*j, end=' ') ('Carol', 95),
print() # new line ]

# Output: for name, score in students:


# 1 2 3 if score >= 80:
# 2 4 6 print(name, "→ Pass ")
# 3 6 9 else:
print(name, "→ Fail ")
# Outer loop → rows (i)
# Inner loop → cols (j) # Alice → Pass
# Bob → Fail
Python Control Flow — Quick Reference Cheatsheet

if / elif / else for Loops while Loops break/continue/pass

if condition: for i in range(5): x = 0 for i in range(10):

# True block print(i) # 0-4 while x < 5: if i == 5:

elif other_cond: for x in my_list: print(x) break # exit

# elif block print(x) x += 1 if i == 3:

else: for c in "hello": # Always update var! continue # skip

# fallback print(c) # Unknown iterations if i == 1:

# Only 1 block runs # Known iterations # → use while pass # noop

Always indent with 4 spaces · Always end conditions with a colon : · Always update variables in while loops
What You Learned Today!

if / elif / else
What's Next?
Make decisions — run different code based on conditions

→ Functions (def)
for Loops
Iterate over lists, strings, range() — known iterations → List Comprehensions

→ Error Handling
while Loops
Repeat while a condition holds — unknown iteration count
→ File I/O

break / continue / pass → Modules & Libraries

Control exactly how your loops behave mid-execution


→ Object-Oriented Python

Nested Control Flow


Combine loops and conditions for powerful programs

Practice on [Link] · [Link] · [Link]/python · [Link]

You might also like