Python conditional statement or if else
In python, if the values are not zero or not-null then it is assumed as true. Whereas if the value is null or zero then it is said to be False.
if-else is the decision-making statement which consists of conditions happening while the implementation of the program and specifying execution takes place according to the conditions.
Following are the types of decision-making statements:
1.if
Here if the statement will compare values and return boolean which is followed by one or more statements respectively.
Example:
Num1 = 100
if Num1:
print "Num1 is non zero value "
print var1
Num2 = 0
if Num2:
print "Num2 is zero "
print var2
print " above Num2 is zero"
2. if…else
Here the if is continued by else statement. If the if statement will be true then the statement executes and if not true then else condition will execute.
Example:
#!/usr/bin/python
num1 = 5
if num1:
print "I am if block statement"
print num1
else:
print "I am else block statement"
print num1
num2 = 0
if num2:
print "I am if block statement "
print num2
else:
print "I am else block statement"
print num2
print "Happy coding "
3. nested if
When we use if or else if statement within the if or else if statement then it is called as nested if because we are using if and else if inside another conditional statement.
Example:
#!/usr/bin/python
num1 = 75
if num1 < 200:
print "Expression value is less than 200"
if num1 == 150:
print "Which is 150"
elif num1 == 100:
print "Which is 100"
elif num1 == 75:
print "Which is 50"
elif num1 < 50:
print "Expression value is less than 50"
else:
print "Could not find true expression"
print "happy coding!"
