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

Python Conditionals and Loops

The document covers Python programming concepts related to conditional statements and loops. It explains the use of if, if-else, and if-elif statements for executing code based on conditions, as well as while and for loops for repeating code blocks. Additionally, it introduces loop control statements like break, continue, and pass.

Uploaded by

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

Python Conditionals and Loops

The document covers Python programming concepts related to conditional statements and loops. It explains the use of if, if-else, and if-elif statements for executing code based on conditions, as well as while and for loops for repeating code blocks. Additionally, it introduces loop control statements like break, continue, and pass.

Uploaded by

hmbegham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Python Programming: Conditional

Statements and Loops


By Hyacinthe Raoul
Conditional Statements

• Used to execute code based on conditions.


• Three main types: if, if-else, if-elif-else.
• Can be nested for more complex logic.
The if Statement

The if statement evaluates a


condition; if it's true, the
indented block of code beneath
it runs.

In this example, since age is 20 (which is greater than 18), the condition
evaluates to true, and the message is printed
if-else

The if-else statement


provides an alternative block
of code that runs if the
condition is false

Here, since age is 16 (which is less than 18), the condition is false, and the
else block executes.
If-elif statement

When you have multiple


conditions to check, you can
use elif (short for "else if") to
evaluate additional conditions.​

In this case, marks is 85, so the first condition is false, but the second
condition (marks >= 80) is true, assigning grade 'B'.
Loops in Python

• Used to repeat a block of code.


• Two main types: while and for loops.
while Loop

The while loop continues to


execute as long as the
specified condition is true.

In this example, count starts at 1 and increments by 1 each time the loop runs
until it exceeds 5
for Loop

The for loop is used to iterate


over a sequence (like a list,
tuple, dictionary, set, or
string).
range() Function

The range() function


generates a sequence of
numbers, which is especially
useful in for loops.
break: exits the
loop early.

Loop Control continue: skips


current iteration.
Statements

pass: does nothing


(placeholder).

You might also like