Python for Beginners
ෙනාහැක්කක් ෙනාමැත
Week -06 : Flow Control
(Looping techniques)
www.heladevs.org
Where we are ??
What is
Intro Functions
Computing
Programming
Languages
Variables &
Data Types
Flow Control
Flow Control
Continuation
Loops
week-01 week-14
Previous This Week
Week
Python type system
● Statements
● while loop
● Conditions ● for loop
● If statements
● break statement
● Switch statement
● continue statement
● Nested loops
What is a loop
● In computer programming, a loop is a way to execute a block of code repeatedly
until a certain condition is met. Loops are used to automate repetitive tasks or to
iterate over a collection of data to perform a specific operation.
● Loops can be terminated prematurely using break statement
● A for loop is used to iterate over a sequence of data, such as a list, tuple,
string, or dictionary. The loop variable takes on each value in the sequence in
turn, and the block of code within the loop is executed for each value.
Loops in real world
● In production chains.
● Traffic lights
● Autopilot system
● Analysis of financial data
while loop
Demo
Click here
for loop
Demo
Click here
range() Function
● In Python, the range function is used to generate a sequence of numbers.
● Mostly used together with loops to control loop iterations
● Syntax : range(start, stop, step)
break statement
continue statement
Demo
break statement Demo
continue statement Demo
Nesting Loops
● Nesting loops refers to the practice of placing one loop inside another
loop. This means that the inner loop will be executed multiple times for
each iteration of the outer loop.
● Is a very powerful concept, Also can be computationally
intensive/expensive
● Can increase the complexity of the code
● Better to optimize (ie. minimizing iterations)
Nesting Loops
Demo
Click here
Next Week – Week 07
Week 14
Week 07
• Functions
Week 06
• Flow Control
Continuation
• Loops
THANK YOU
ෙනාහැක්කක් ෙනාමැත.