7894110371ControlStructures
7894110371ControlStructures
Module III
Conditional Execution(Selection
statements)
Control Structures
• The Python programs encountered so far are
very simple.
• These follow the sequencing construct .
• Python also supports the other two
constructs, viz. selection, and looping.
• These constructs enable Python to solve real-
life problems effectively.
One-way selection statement
• It needs to check conditions and change the behaviour of the
program accordingly
• Conditional statements give us this ability
– Eg. If x>0:
print(“positive”)
else:
print(“negative”)
Chained conditionals or Multi-way selection
statement
• Sometimes there are more than two possibilities and we
need more than two branches
• One way to express a computation like this is to use
chained conditionals using if…elif…else statement
• Syntax:
if condition 1:
Body of if
elif condition 2:
Body of elif
else:
Body of else
Chained conditionals
• Eg:
if x>0:
print(“positive”)
elif x<0:
print(“negative”)
else:
print(“zero”)
Nested conditionals
• One conditionals can be nested within another
• Syntax
if condition 1:
statement 1
else:
if condition 2:
statement 2
else:
condition 3
Nested conditionals
• Eg:
if x=0:
print(“zero”)
else:
if x>0:
print(“positive”)
else:
print(“negative”)
Sample Programs
1.Write a python program to find the area and
Perimeter of a circle
\b Backspace
\n Newline
\t Horizontal tab
\’ Single quotation mark
1.while…. statement
2.for…. statement
Conditional Iteration: The While loop
• Conditional iteration requires that a condition be tested within
the loop to determine whether the loop should continue.
Eg:
i=1
while(i<=10):
print(i ,end=“ “)
i=i+1
print(“End”)
Output
1 2 3 4 5 6 7 8 9 10
Definite iteration: The For loop
• Repeats a set of statements over a group of values
• Group of values can be a range of integers ,specified with the
range function
• range() function dictates the number of iterations
• range(k) when used with for causes the loop to iterate k
times.
• Syntax:
For variable in group of Values:
statements
Example
Eg.To print first 10 natural numbers
for x in range(1,11):
print(x)
for x in range(1,6):
print( x, “Square is”, x**x)
for i in range(5):
print("Hello")
Range function
• Specifies a range of integers
• Range(start,stop)
– The integers between start(inclusive) and stop(exclusive)
– It can also accept a third value specifying the change between values
Range(start,stop,step)
Eg.
>>>for x in range(1,6):
print (x)
>>>for x in range(5,0,-1):
print( x)
o/p
5
4
3
2
1
Nested loops
• It is possible to have a loop inside another loop.
• We can have a for loop inside a while loop or inside another
for loop.
• The same is possible for while loops too.
• The enclosing loop is called the outer loop, and the other loop
is called the inner loop.
• The inner loop will be executed once for each iteration of the
outer loop:
Example
for i in range(1,5): #This is the outer loop
for j in range(1,5): #This is the inner loop .
print(j, end=" ")
print(“\n”)
Output
1234
1234
1234
1234
The number is 1
2 is even
Outside the loop
Example 2
Example
i=0
while i<10:
num=int(input())
if num==5:
break
else:
print(num)
i++
print(“Finished the loop”)
Continue statement
• The continue statement is used to bypass the
remainder of the current iteration through a
loop.
• The loop does not terminate when a continue
statement is encountered.
• Rather, the remaining loop statements are
skipped and the control proceeds directly to
the next pass through the loop
Example
for num in range(1,5):
if num%2==0:
print(num,"is even")
continue
print("The number is",num)
print("Outside the loop")
Output
The number is 1
2 is even
The number is 3
4 is even
Outside the loop
Print statement
• Syntax:
print( value(s), sep= ‘ ‘, end = ‘\n’, file=file, flush=flush)
• Parameters:
– value(s) : Any value, and as many as you like. Will be converted to string
before printed
Eg:
type( ),max( ),min( ),int( ),str( )
>>>int(‘60’)
60
>>>str(60)
‘60’
>>>max(10,45)
45
>>>min( 67,12)
12
Mathematical functions
• Some of the mathematical functions are defined in the module
“math”
• Module
– Module is a file that contains a collection of related function
– Before we use a module,we have to import it
>>>import math
>>>root=math.sqrt(16)
>>>print(root)
4
>>>print(math.pi)
3.141592653589793
>>>print(math.pow(3,2))
9
Mathematical functions
• Two ways to import module
1)import math
-we get a module object named math
-the math object contains constants like pi and functions like
sqrt( ),sin ( ),exp( ) etc
-the module object along with dot operator is used to access a
function or variables
Eg: math.sqrt()
2)from math import pi
-This will import an object from a module
-Now we can access ‘pi’ directly
>>>print(pi)
3.14….
-to import everything from the module we can use
from math import *
Modules