if statement in python
Indentation
Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose. if statement without identation will raise an error, in python identation for if elif else statement is (TAB) i.e four whitespaces.
if elif statements in python
elif means else if .
a = 200
b = 203
if(a>b):
print('The value of a is greater than b')
elif(b>a):
print('The value of b is greater than a')
The value of b is greater than a
if elif else statements in python
a = 100
b = 100
if(a>b):
print('The value of a is greater than b')
elif(a<b):
print('The value of a is lesser than b')
else:
print('The value of a is equal to b')
The value of a is equal to b
You can also use else without elif and can transform it into a if else statement.
Relational Operators
- \== means equals
** >=** means greater than or equals to
** <=** means less than or equal to
\> means greater than
< means less than
Logical Operators
**AND ** - Returns True if both the conditions are met .
**OR ** - Returns True if any one of the conditions is met.
NOT - Inverts true to false and false to true.
AND OPERATOR
The and keyword is a logical operator and is used to combine conditional statements.
a = 40
b = 32
c = 39
if (a>b) and (a>c) :
print('Value of a is greater than b and c ')
else :
print('a is not the greatest number among a,b and c')
Value of a is greater than b and c
OR OPERATOR
a = 30
b = 32
c = 30
if (a>b) or (a<c) :
print('Value of a is either greater than b or less than c')
else:
print('Condition not satisfied')
Condition not satisfied
Nested if statements
Using if statements inside if statements is called nested if statements.
a = 30
b = 291
c = 32
if (a<b):
print('a is less than b')
if (a<c):
print('a is less than c')
else:
print(a)
a is less than b
a is less than c
using IN and IS
using in
A = None
if ( A is None ):
print('yes')
else:
print('a is not none')
yes
using is
a = [1,2,3,333,34,69]
if (333 in a):
print('yes')
else:
print('333 is not in a ')
# METHOD - 2
print(5555 in a)
print(3 in a)
yes
False
True