Decision Control Statements - Python
Decision Control Statements - Python
Conditional Statements:
if (test expression):
statement block 1
else:
statement block 2
Example:
x=8
if (x==10):
print(x+1)
else:
print(x-1)
Example:
Exercise – 2: Write a program that prompts the user to input any number from 1 to 7 and then
displays the corresponding day of the week.
NOTE: If we have complex test expression, then we should appropriately use logical
operators (AND, OR , NOT). In Python, the following expression is invalid:
Exercise – 4: Write a program that prompts the user to enter any number and then print its
interval to be one of these:
Iterative Statements:
statement block X
while (conditional expression):
statement block
statement block Y
Example:
i=0
while(i<10)
print(i)
i+=1
In this example, value of i is printed from 0 to 9 because the condition checked within while()
remains true until i is less than 10 – counting from 0 to 9, we can say that the while loop has
executed 10 times. Or, we say there are 10 iterations of while loop in this program.
However, if the condition within while() were i<=10 – then the loop would have
executed one more time printing the value of i from 0 to 10 – and then, we would say while
loop has executed 11 times. Or, there are 11 iterations of while loop in this program.
Exercise – 5: Write a program that prompts the user to enter a number and then calculates the
sum of its digits.
NOTE: This program requires us to be very careful with the data types.
statement block X
for loop_variable in sequence:
statement block
statement block Y
We can iterate using ‘for’ loop over a sequence of numbers, such as present in list, sets,
tuples, or we can make use range() function to allow ‘for’ loop to execute for a pre-declared
number of times.
Range() – is a built-in function to iterate over a sequence! It can take any of the following
three forms:
i) range(end) – here ‘end’ parameter indicates that the loop_variable should execute
from 0 till (end-1). An example:
for i in range(10):
print(i)
This loop will print values of i from 0 to 9 (end-1), i.e. incrementing value of i by
1 in each iteration.
ii) range(beg, end) – here ‘beg’ indicates the start value of loop_variable and ‘end’
indicates that loop_variable should execute till (end-1). An example:
for i in range(1, 10):
print(i)
This loop will print values of i from 1 (start value) to 9 (end-1), i.e. incrementing
value of i by 1 in each iteration.
iii) range(beg, end, step) - here, ‘beg’ and ‘end’ have same interpretation as above
(i.e. incrementing the loop control variable stops at end - 1), ‘step’ indicates the
increment value in loop_variable. An example:
1
3
5
7
9
iv) Printing list elements and strings using range function - List and string characters
can be accessed using subscript operator, [] - to print list or string elements, we
will find the length of list or string using len() function. and use range function
with the length of list/string. An example:
list1 = [1,2,3,4,5,6,7,8]
for i in range(len(list1)):
print(list1[i])
Exercise – 6: Write a program to find average of first ‘n’ natural numbers (that start from 1).
For example – say ‘n’ is 5, then the program should compute sum as: 1+2+3+4+5 = 15, and
compute average as 15/5 = 3.0
Exercise – 7: Write a program to compute exponent, i.e. program that replicates the exponent
operator **.
*
**
***
****
*****
*
**
***
****
*****
NOTE: Listed below are few more useful statements that can appear within the body of the
loop only:
Scenario 1:
while (conditional expression):
…..
if condition:
break
…..
Transfers control out of while
…..
….. loop
Scenario 2:
for conditional expression:
…..
if condition:
break
…..
Transfers control out of for
…..
loop
…..
Scenario 3:
2. Continue statement – when compiler encounters this statement, then rest of the
statements in the loop are skipped and the control is unconditionally transferred to
loop-continuation part of nearest loop.
Scenario 1:
while (conditional expression):
…..
if condition:
continue
…..
….. Transfers control to the conditional
….. expression of while loop
Scenario 2:
Scenario 3: