-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython if-else.py
217 lines (117 loc) · 2.94 KB
/
python if-else.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#########################################
# #
# #
# Python If - Else #
# #
# #
#########################################
# python if - else statements are logical conditions ...
# Equals: a == b
# Not Equals: a != b
# Less than: a < b
# Less than or equal to: a <= b
# Greater than: a > b
# Greater than or equal to: a >= b
a = 10
b = 5
if b < a :
print('yes!!... a is greater')
# it will print o/p yes!!. a is greater
# we can put the statement without else or with else..
a = 10
b = 5
if b < a :
print('yes!!... a is greater')
else:
print('no! b is greater)
# note : intedent is very important or else it will occur error
# intendent 4 space or press tab button 1 time
### elif
# The elif keyword is pythons way of saying "if the previous conditions were not true, then try this condition".
a = 50
b = 50
if a > b :
print('a is greater ')
elif a == b :
print('a is equal to b ')
else:
print('nothing')
# if the condition is true it will stop first if or it will go elif
# if elif is also false it will go ... else (finally)
# let's see a else conditon statement
a = 50
b = 1
if a < b :
print('b is greater ')
elif a == b :
print(' a and b are equal')
else:
print(' a is greater ')
# we can put the if-else statement with variable string data type
# for e.g.,
a = 'rock'
b = 'helo'
if a == b :
print('yes')
else :
print('no')
# the o/p is no
# we can check anything with the if statement or we can print anything ....
l = [ 1 , 2 , 3 , 4 ,5 ]
if 5 in l :
print('yes')
else:
print('no')
# it will print yes.
# short -hand if statement
# it will be executed in one line
a = 100
b = 50
if a > b: print("a is greater than b")
# for else
a = 10
b = 5
print('yes') if a > b else print('no')
# the o/p will be yes ..
# for elif
a = 330
b = 330
print("A") if a > b else print("=") if a == b else print("B")
# we can give like this also
# we can give compare not only two value we can compare many value
# for e.g.,
a = 50
b = 100
c = 200
if a < b and c > b :
print('yes, it is true')
else:
print('nope')
# we can use and , or operators here
########### NESTED - IF ###########################
# You can have if statements inside if statements, this is called nested if statements.
x = 41
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
if x > 30 :
print('and also above 30')
if x > 40 :
print('and also above 40')
else:
print("but not above 20.")
# it will be print if the above statement is true , it will be print also that statement .
# the o/p will be printed as
# Above ten,
# and also above 20
# and also above 30
# and also above 40
#################################################3
# pass statement
a = 10
b = 20
if b > a :
pass
# the o/p will be nothing can print and it won't give any error.
###############################################################################