0% found this document useful (0 votes)
18 views11 pages

Python Control Structures Explained

This document covers control structures in Python, including if-else statements, for loops, while loops, and loop control mechanisms like break and continue. It emphasizes how these structures enable decision-making and iteration, forming the basis of dynamic program flow. The lesson builds on previous knowledge of variables and data types, highlighting their role in controlling execution flow.

Uploaded by

wissalmn45
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)
18 views11 pages

Python Control Structures Explained

This document covers control structures in Python, including if-else statements, for loops, while loops, and loop control mechanisms like break and continue. It emphasizes how these structures enable decision-making and iteration, forming the basis of dynamic program flow. The lesson builds on previous knowledge of variables and data types, highlighting their role in controlling execution flow.

Uploaded by

wissalmn45
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

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!

You might also like