if else in Pythpn
if...else Statement in Pythpn
The if .. else statement provides control to check the true block as well as the false block. Following is the syntax of ‘if..else’ statement.
if..else statement thus provides two possibilities and the condition determines which BLOCK is to be executed.
Example : #Program to check if the accepted number odd or even
a = int(input("Enter any number :"))
print (a, " is an even number")
print (a, " is an odd number")
Enter any number :56
56 is an even number
Enter any number :67
67 is an odd number
An alternate method to rewrite the above program is also available in Python. The complete if..else can also written as:
variable = variable1 if condition else variable 2
Example : #Program to check if the accepted number is odd or even (using alternate method of if...else)
a = int (input("Enter any number :"))
x="even" if a%2==0 else "odd"
print (a, " is ",x)
Enter any number :3
3 is odd
Enter any number :22
22 is even