Today’s Topics:
If statements?
Else and Elif statements
Nested If statement
The While Loop
The For Loop
Loop control statements
Use range() in For Loop
By @Curious_.programmer
5. Control Flow
Made By:
Yadneyesh (Curious Coder) Find More PDFs on Our Telegram Channel
CodWithCurious.com Search Curious_Coder on Telegram
Control Flow
Control flow directs program execution through structures
like loops, conditionals, and functions, determining the order
and path of operations. Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
If statements
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
An `if` statement in Python checks whether a condition is
true or false. If the condition is true, the code inside the `if`
block runs. If false, the code is skipped. It's used to make
decisions in the program, executing specific actions based
on conditions
example:
Else and elif statements
In Python, else and elif statements are used alongside if to
handle multiple conditions and alternative actions.
elif (else if): Checks another condition if the previous if
was false. You can have multiple elif statements.
else: Runs when none of the if or elif conditions are true.
It's the "default" action.
example:
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Nested if Statement
A nested `if` statement in Python is an `if` statement inside
another `if`. It lets you check multiple related conditions in
sequence.
For example:
If you first check the weather and it’s sunny, you can then
check how many guests are coming. Depending on the
number of guests, you decide between different activities,
like a barbecue or picnic.
If the weather isn’t sunny, you skip the nested checks and
go straight to an alternative action, like staying indoors.
example:
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
The While Loop
A `while` loop in Python repeatedly executes a block of code
as long as a specified condition is true.
It first checks the condition; if true, the code inside runs.
After each iteration, the condition is rechecked. The loop
continues until the condition becomes false.
For example
A `while` loop can keep counting up as long as the count is
below a certain number. It's useful for scenarios where you
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
don't know in advance how many iterations
@Curious_Coder are needed.
Want Complete PDF? Download From Our Telegram
Channel
The For Loop
A `for` loop in Python is used to iterate over a sequence,
such as a list, tuple, or range, executing a block of code
for each item in the sequence.
Unlike a `while` loop, which runs until a condition is false, a
`for` loop runs a set number of times based on the length of
the sequence.
For example
It can go through a list of numbers, processing each one in
turn. It's ideal for repetitive tasks like iterating over data
collections.
example:
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Loop Control Statements
Loop control statements in Python allow you to alter the flow
of a loop’s execution. They include:
`break`: This statement immediately exits the loop,
regardless of whether the loop's condition is still true. It’s
useful for stopping a loop when a specific condition is
met, like when searching for an item in a list and finding
it before the loop has iterated through the entire list.
example:
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
'continue': This statement skips the rest of the current
loop iteration and proceeds to the next iteration. It’s
helpful for bypassing certain parts of the loop based on
a condition, like skipping even numbers in a loop that
processes a range of numbers.
example:
Using Range() in For Loop
The range() function in Python is commonly used in for
loops to iterate over a sequence of numbers.
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Here's a basic rundown of how it works:
start: The starting value of the sequence (inclusive). If
omitted, it defaults to 0.
stop: The ending value of the sequence (exclusive). The
loop will run until it reaches this value.
step: The amount by which the sequence is
incremented. If omitted, it defaults to 1.
You can use range() in a for loop for various tasks like
iterating through lists, generating sequences of
numbers, or performing repetitive actions a specific
number of times.
example:
Copyrighted By : Instagram: Curious_.Programmer
CodeWithCurious.com
Want Complete PDF? Download From Our Telegram
Channel @Curious_Coder
Will Post Follow For More