CH 3 Unit 1 Conditional Statement, Looping and Control Statement
CH 3 Unit 1 Conditional Statement, Looping and Control Statement
Conditional Statement
S.No Statement & Description
.
if statements
1
An if statement consists of a boolean expression followed by one or
more statements.
if...else statements
nested if statements
var=100
print("Good bye!")
Syntax
The syntax of the if...else statement is −
if expression:
statement(s)
else:
statement(s)
Example
#!/usr/bin/python3
if amount<1000:
discount= amount*0.05
print("Discount",discount)
else:
discount= amount*0.10
print("Discount",discount)
print("Net payable:",amount-discount)
Output
In the above example, discount is calculated on the input amount. Rate of
discount is 5%, if the amount is less than 1000, and 10% if it is above
10000. When the above code is executed, it produces the following result −
Enter amount: 600
Discount 30.0
Net payable: 570.0
Similar to the else, the elif statement is optional. However, unlike else, for
which there can be at the most one statement, there can be an arbitrary
number of elif statements following an if.
syntax
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
Example
#!/usr/bin/python3
if amount<1000:
discount= amount*0.05
print("Discount",discount)
elif amount<5000:
discount= amount*0.10
print("Discount",discount)
else:
discount= amount*0.15
print("Discount",discount)
print("Net payable:",amount-discount)
Syntax
The syntax of a while loop in Python programming language is −
while expression:
statement(s)
count=0
while(count <9):
count= count +1
print("Good bye!")
Output
When the above code is executed, it produces the following result −
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!
The Infinite Loop
var=1
print("Good bye!")
count= count +1
else:
Output
When the above code is executed, it produces the following result −
0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5
Syntax
For iterating_var in sequence:
statements(s)
Example
>>list(range(5))
[0,1,2,3,4]
Example
range() generates an iterator to progress integers starting with 0 upto n-1.
To obtain a list object of the sequence, it is typecasted to list(). Now this list
can be iterated using the for statement.
print(var)
Output
This will produce the following output.
0
1
2
3
4
print()
fruits=['banana','apple','mango']
print("Good bye!")
Output
When the above code is executed, it produces the following result −
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
if num%2==0:
break
else:
Output
When the above code is executed, it produces the following result −
the list does not contain even number
Nested loops
Syntax
For iterating_var in sequence:
statements(s)
statements(s)
while expression:
while expression:
statement(s)
statement(s)
Example
The following program uses a nested-for loop to display multiplication tables
from 1-10.
#!/usr/bin/python3
import sys
for i in range(1,11):
for j in range(1,11):
k=i*j
print(k,end=' ')
print()
The print() function inner loop has end=' ' which appends a space instead
of default newline. Hence, the numbers will appear in one row.
Output
When the above code is executed, it produces the following result −
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Run on IDE
Output:
Current Letter : g
Current Letter : k
Current Letter : f
Current Letter : o
Current Letter : r
Current Letter : g
Current Letter : k
2)Break Statement
It brings control out of the loop
for letter in 'geeksforgeeks':
if letter == 'e' or letter == 's':
break
print ('Current Letter :', letter)
Run on IDE
Output:
Current Letter : g
3)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 'geeksforgeeks':
pass
print ('Last Letter :', letter)
Run on IDE
Output:
Last Letter : s