Python Basics: Control
Structures
Continuation of Python Basics — Variables
& Data Types
Immersion compiled by:
Prof. Mohammed TAMALI
Head Of SimulIA Team
ENERGARID Lab.
Source Code
Overview
• This lesson builds upon variables and data
types.
• It introduces control structures that control
program flow:
• If-Else Statements
• For Loops
• While Loops
• Break and Continue
• Nested Loops
If-Else Statements
• Used to execute code based on conditions.
• Example:
• score = 85
• if score >= 90:
• grade = 'A'
• elif score >= 80:
• grade = 'B'
• else:
• grade = 'C'
• → Decision making based on comparison operators.
For Loops
• Used to iterate over sequences such as lists or
ranges.
• Example:
• fruits = ['apple', 'banana', 'orange']
• for fruit in fruits:
• print(fruit)
• for i in range(1, 6):
• print(i)
• → Loops repeat code efficiently.
While Loops
• Repeat code while a condition is true.
• Example:
• count = 0
• while count < 5:
• print(count)
• count += 1
• → Use when the number of iterations is not known in
advance.
Break and Continue
• Used to control loop execution.
• Example:
• for num in range(1, 11):
• if num == 5:
• continue # Skip this iteration
• if num > 8:
• break # Stop the loop
• print(num)
• → 'continue' skips, 'break' stops.
Nested Loops
• Loops inside other loops allow structured repetition.
• Example:
• for i in range(1, 3):
• for j in range(1, 3):
• print(f'{i} x {j} = {i * j}')
• → Common in tables, grids, and matrix processing.
Relation to Variables & Data Types
• Variables store values that are used in conditions and
loops.
• Data types determine the type of comparison or operation
possible.
• Control structures make programs dynamic and
interactive.
• Together, these form the foundation of Python logic
flow.
Summary
• We learned how to control the execution flow in
Python using:
• Conditional statements (if-else)
• For and while loops
• Loop control (break, continue)
• Nested loops
Exercise: Write a Python code source for processing roots of a QE while
optimizing the code.
→ Next: Functions and modular programming!