Professional Documents
Culture Documents
Day-5 (09-10-2020) (Conditional Statements and Iterations
Day-5 (09-10-2020) (Conditional Statements and Iterations
In [ ]: #If statement: (its used for decision making operations. it runs the code only the condition is True)
#syntax:
if condition/expression:
block of statements
else:
block of statements (its executed only if condition is false)
In [5]: #example:
x=5
y=10
if x > y:
print("x is less than y")
else:
print("x is greater than y")
x ix greater than y
In [7]: a = 10
b = 10
if a < b:
print(" a is less than b")
elif a==b:
print(" a is equal to b")
else:
print("a is greater than b")
#Note: Here we got the wrong result by else block blindly execution.
# to correct the previus error made by "else block" we can use "elif"
a is equal to b
In [27]: #Example:
SavingAmt = 5000
withdrawAmt = int(input("amout to be withdraw:")) #type conversion string to integer values
if withdrawAmt > SavingAmt:
print("insuffient Balance")
elif SavingAmt==withdrawAmt
SavingAmt = SavingAmt-withdrawAmt #int() float() str()
print("Account Balance:"+str(SavingAmt))
#print("Zero Balance")
else:
SavingAmt = SavingAmt-withdrawAmt
print("Account Balance:"+str(SavingAmt)) #Type conversion integer to string
amout to be withdraw:3000
Account Balance:2000
In [ ]: #Nested If:
#Syntax:
if condition #1:
if condition #1.1:
if condition #1.2:
[statements to be execute if 1 and 1.1 and 1.2 conditions are True]
else:
statement to execute if 1 and 1.1. are False
else:
alternate statements to execute
In [36]: #Example:
#time = int(input("Enter the Time")
time = int(input("enter time"))
if time>=6 and time <12:
print("Hello Good Morning")
else:
if (time==12):
print("Noon")
else:
if time > 12 and time <= 17:
print("afternoon")
else:
if time > 17 and time <= 20:
print("Good Evening")
else:
if time > 20 and time <24 or time >=0 and time<6:
print("Good Night")
else:
print("invalid Time!")
enter time21
Good Night
In [43]: #Example:
s = "Python Programming"
print(type(s))
for i in s:
print(i,end=" ")
<class 'str'>
P y t h o n P r o g r a m m i n g
In [42]: li = [10,20,500,1000,30,60,90]
sum = 0
for val in li:
sum = sum+val
print(sum,end=" ")
1710
Out[57]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [55]: #Syntax:
#range(start,stop,step-size)
print(list(range(10,101,5)),end=" ")
[10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]
In [59]: print(list(range(2,20,3)))
In [62]: #Example: to print the list even numbers upto 100 numbers
for i in range(0,100):
if i %2 ==0:
print(i,end=" ")
0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74
76 78 80 82 84 86 88 90 92 94 96 98
In [65]: li = ["abc","xyz","pqr","friday","sunday"]
print(len(li))
for name in range(len(li)):
print(li[name])
5
abc
xyz
pqr
friday
sunday
0
1
2
3
4
5
6
7
8
9
No items are left
jan
feb
mar
april
In [75]: #Ex:
for x in range(10,50):
if x==15:
break
print(x)
else:
print("this is end point")
10
11
12
13
14
In [ ]: #Continue Statement:
Continue function, will terminate the current iteration of the for loop BUT will
continue execution of the remaiming iterations.
In [4]: a,b,c=10,20,30
print(a,b,c)
10 20 30
enter numbers:10 20 30
10 20 30
In [ ]: