Unit 4 Conditional Statements and Looping Statements
Unit 4 Conditional Statements and Looping Statements
------------------------------------------------------------
Unit 4:
1. if Statement :
Before going to discuss about if statement syntax and examples, we
need to know about an important concept known as indentation.
if condition: (Note: In Python any where we are using colon(:) means we are defining block)
.......statement 1
.......statement 2 (These statements are said to be same indentation)
.......statement 3
statement 4 (This statement is not under if statement)
Example
In [1]: if 10<20:
print('10 is less than 20')
print('End of Program')
10 is less than 20
End of Program
if 10<20:
print('10 is less than 20')
print('End of Program')
Enter Name:Uppu
Hello upendra Good Morning
How are you!!!
if - else Statement:
Syntax
if condition:
........ Action 1
else:
........ Action 2
if condition is true then Action-1 will be executed otherwise Action-2 will be executed.
Example
In [6]: name = input('Enter Name : ')
if name == 'Uppu':
print('Hello Upendra! Good Morning')
else:
print('Hello Guest! Good MOrning')
print('How are you?')
if-elif-else Statement:
Syntax
if condition1:
........ Action-1
elif condition2:
........ Action-2
elif condition3:
........ Action-3
elif condition4:
........ Action-4
........ ...
else:
........ Default Action
Enter Number:454
The number 454 is not in between 1 to 100
2. Iterative Statements
If we want to execute a group of statements multiple times then we should go for Iterative statements.
Python supports 2 types of iterative statements.
1. for loop
2. while loop
1. for loop:
If we want to execute some action for every element present in some sequence (it may be string or
collection) then we should go for for loop.
Syntax:
for x in sequence:
....... body
I
n
d
i
a
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Q 4: To display numbers from 0 to 10
In [23]: for i in range(0,11):
print(i)
0
1
2
3
4
5
6
7
8
9
10
1
3
5
7
9
11
13
15
17
19
10
9
8
7
6
5
4
3
2
1
Enter List:45,78,96
The Sum= 219
while loop:
If we want to execute a group of statements iteratively until some condition false,then we should go for
while loop.
Syntax:
while condition:
........ body
1
2
3
4
5
6
7
8
9
10
Enter number:10
The sum of first 10 numbers is : 55
Enter password:upendra
Enter password:rohit
Enter password:jayesh
Enter password:Python@1234
Thanks for confirmation
Infinite Loops
Some times a loop can execute infinite number of times without stopping also.
Example
i = 1
while True: # The body of this while loop keep on execuing because condition is always true
......print('Hello', i) # This program never going to terminates
......i=i+1
In [ ]:
Nested Loops
Sometimes we can take a loop inside another loop,which are also known as nested loops.
Upendra
Jayesh
Jayesh
Upendra
Jayesh
Jayesh
Upendra
Jayesh
Jayesh
i = 0 j = 0
i = 0 j = 1
i = 0 j = 2
i = 0 j = 3
i = 1 j = 0
i = 1 j = 1
i = 1 j = 2
i = 1 j = 3
i = 2 j = 0
i = 2 j = 1
i = 2 j = 2
i = 2 j = 3
i = 3 j = 0
i = 3 j = 1
i = 3 j = 2
i = 3 j = 3
Alternative way:
In [42]: n = int(input("Enter number of rows:"))
for i in range(1,n+1):
print("* " * i)
Transfer Statements
1. break:
We can use break statement inside loops to break loop execution based on some condition
1. continue:
We can use continue statement to skip current iteration and continue next iteration
0
1
2
3
4
5
6
processing is enough..plz break
In [45]: cart=[10,20,600,60,70]
for item in cart:
if item>500:
print("To place this order insurence must be required")
break
print(item)
10
20
To place this order insurence must be required
cart=[10,20,600,60,70]
for item in cart:
if item>500:
print("To place this order insurence must be required")
continue
print(item)
10
20
To place this order insurence must be required
60
70
In [49]: cart=[10,20,500,700,50,60]
for item in cart:
if item >= 500:
print("We cannot process this item :",item)
continue
print(item)
10
20
We cannot process this item : 500
We cannot process this item : 700
50
60
1
3
5
7
9
In [50]: numbers=[10,20,0,5,0,30]
for n in numbers:
if n==0:
print("Hey how we can divide with zero..just skipping")
continue
print("100/{} = {}".format(n,100/n))
100/10 = 10.0
100/20 = 5.0
Hey how we can divide with zero..just skipping
100/5 = 20.0
Hey how we can divide with zero..just skipping
100/30 = 3.3333333333333335
Questions:
Q 1. What is the difference between for loop and while loop in Python?
We can use loops to repeat code execution
Repeat code for every item in sequence ==>for loop
Repeat code as long as condition is true ==>while loop ### Q 2. How to exit from the loop?
by using break statement ### Q 3. How to skip some iterations inside loop?
by using continue statement.
pass statement:
pass is a keyword in Python.
In our programming syntactically if block is required which won't do anything then we can define that
empty block with pass keyword. ### use case of pass:
Sometimes in the parent class we have to declare a function with empty body and child class
responsible to provide proper implementation. Such type of empty body we can define by using pass
keyword. (It is something like abstract method in java).
0
9
18
27
36
45
54
63
72
81
90
99
In [56]: math.log(100,5)
2.8613531161467867
Out[56]:
Lets Make a game: Guessing Game
In [13]: import random # imported to generate Random Numbers
if count>Number_of_Try:
print(f" the number is {x} ")
print(" Better luck Next time! ")
In [ ]:
Assignment 2:
1. For all these questions, Please use loops and avoide using
ready made function/methods as much as possible.
2. write a Python Program to find the factorial of a number entered by the user.
3. Write a python program that prints the following pyramid on the screen. The number of the lines must
be obtained from the user as input.
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
4. Write a program to find those numbers which are divisible by 7 and multiple of 5, between 1500 and
2000 (both included).
5. Write a python program to construct the following pattern, using a nested for loop.
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
6. Write a code mimicking KBC (Kaun Banega Karodpati). Let user enter his/her details (name, age, etc.)
and ask minimum 5 questions. Tell user the amount he/she has won and inform that the amount will be
deposited in given bank account.
7. Frame Your own question and solve using tools/techniques learned in this unit.
In [ ]: