L10_M1_Python If-else_loops_
L10_M1_Python If-else_loops_
Technology
Here, the program evaluates the test expression and will execute statement(s)
only if the test expression is True.
num = 3
if num > 0:
print(num, "is a positive number.")
print("This is for 3.")
num = -1
if num > 0:
print(num, "is a negative number.")
print("This is for -1.")
3 is a positive number
This is for 3
This is for -1
Amity School of Engineering &
Technology
Indentation
Python relies on indentation (whitespace at the beginning of a line) to define
scope in the code. Other programming languages often use curly-brackets for
this purpose.
Amity School of Engineering &
Technology
Python if...else Statement
Syntax of if...else
if test expression:
Body of if
else:
Body of else
The if..else statement evaluates test expression and will execute the body of if
only when the test condition is True.
num = 3
if num >= 0:
print("Positive or Zero")
else:
print("Negative number")
Amity School of Engineering &
Technology
Python if...elif...else Statement
Syntax of if...elif...else
if test expression:
Body of if
elif test expression:
Body of elif
else:
Body of else
The elif is short for else if. It allows us to check for multiple expressions.
If the condition for if is False, it checks the condition of the next elif block and so
on.
Only one block among the several if...elif...else blocks is executed according to
the condition.
The if block can have only one else block. But it can have multiple elif blocks.
Amity School of Engineering &
Technology
Example of if...elif...else
'''In this program, we check if the number is positive or negative or zero and
display an appropriate message'''
num = 3.4
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Amity School of Engineering &
Python Nested if statements Technology
Syntax:
Output:
amity
university
gwalior
Inside Else Block
Amity School of Engineering &
Technology
Nested Loops: Python programming language allows to use one loop inside
another loop. Following section shows few examples to illustrate the concept.
Syntax:
while expression:
while expression:
statement(s)
statement(s)
A final note on loop nesting is that we can put any type of loop inside of any other
type of loop. For example a for loop can be inside a while loop or vice versa.
Amity School of Engineering &
Technology
Output:
?????
Amity School of Engineering &
Technology
Loop Control Statements: Loop control statements change execution from its
normal sequence. When execution leaves a scope, all automatic objects that were
created in that scope are destroyed. Python supports the following control
statements.
Continue Statement: It returns the control to the beginning of the loop.
Output:?
Amity School of Engineering &
Technology
Break Statement: It brings control out of the loop
for letter in ‘amityuniversitygwalior':
Output:
Current Letter : e
Amity School of Engineering &
Technology
Pass Statement: We use pass statement to write empty loops. Pass is also
used for empty control statement, function and classes.
# An empty loop
for letter in ‘amity':
pass
Output:
Exercise: How to print a list in reverse order (from last to first item) using while
and for in loops.
Amity School of Engineering &
Technology
While Loop:
In python, while loop is used to execute a block of statements repeatedly until a
given a condition is satisfied. And when the condition becomes false, the line
immediately after the loop in program is executed.
Syntax :
while expression:
statement(s)
All the statements indented by the same number of character spaces after a
programming construct are considered to be part of a single block of code. Python
uses indentation as its method of grouping statements.
Example:
if condition:
# execute these statements
else:
# execute these statements
while condition:
# execute these statements
else:
# execute these statements
Amity School of Engineering &
Technology
Example: Output:
#Python program to illustrate
# combining else with while
count = 0
while (count < 3):
count = count + 1
print("Hello amity")
else:
print("In Else Block")
Single statement while block: Just like the if block, if the while block consists
of a single statement the we can declare the entire loop in a single line as
shown below:
Note: It is suggested not to use this
# Python program to illustrate
type of loops as it is a never ending
# Single statement while block
infinite loop where the condition is
count = 0
always true and you have to
while (count == 0): print("Hello amity")
forcefully terminate the compiler.
Amity School of Engineering &
Technology