Python if, elif, else and ternary operators

if statement

Executes the code written 'if block' if specified condition is true.
#prints zero when input number is 0
int_var = int(input("Please enter an integer: "));                          
if int_var == 0:                                                                
    print("zero");                                  
  

if else statement

Executes if block when condition is true, otherwise else block will be executed.
int_var = int(input("Please enter an integer: "));                           
                                                                                
if int_var % 2 == 0:                                                            
    print("even");    # prints even when number is even and not zero                                   
else:                                                                           
    print('odd');    # prints odd when number is not 0 and not even 

elif statement

Used to provide the additional conditions along with if.
int_var = int(input("Please enter an integer: "));                              
if int_var == 0:                                                                
    print("zero")   # prints zero when number is 0                                                         
elif int_var % 2 == 0:                                                          
    print("even");   # prints even when number is even and not zero                                   
else:                                                                           
    print('odd');    # prints odd when number is not 0 and not even                                                                              
  

Nested if statements

if block can have another if block or if else block. else and elif blocks also can have same.
int_var = int(input("Please enter an integer: "))                               
                                                                                
if int_var % 2 == 0:                                                            
    if int_var == 0:                                                            
        print("zero");                                                          
    else:                                                                       
        print("even");     # prints even when number is even and not zero                                   
else:                                                                           
    print('odd')    # prints odd when number is not 0 and not even   
Output:
$ python test_ifelse.py 
Please enter an integer: 0
zero
$ python test_ifelse.py 
Please enter an integer: 2
even

Getting input in python

input method is used to get input from user as string format.
str_var = input("Please enter an integer: ");
Suppose if we need to get input from user in different format, we type cast it to required format.
int_var = int(input("Please enter an integer: ")); 
This above statement converts input string into integer format.
Suppose input string is not compatible to convert required format, throws interpreter error like below,
$ python test_ifelse.py
Please enter an integer: sfdsf
Traceback (most recent call last):
  File "test_ifelse.py", line 1, in 
    int_var = int(input("Please enter an integer: "))  
  File "", line 1, in 
NameError: name 'sfdsf' is not defined

Ternary Operators

This is similar like if else statements, Ternary operator is nothing but conditional expressions which is used to assign expression result based on condition.
condition_is_true if condition else condition_is_false
#!/usr/bin/python                                                               
                                                                                
a = 10                                                                          
result = "True" if a <= 10 else "False"                                         
                                                                                
print result 
Output:
$ python ternary_test.py
True
Another way to use ternary operators
(if_condition_is_false, if_condition_is_true)[condition]
#!/usr/bin/python                                                               
                                                                                
a = 10                                                                          
#result = "True" if a <= 10 else "False"                                        
# Alternate way
result = ("False", "True")[a<=10]                                                
                                                                                
print result   
Output:
$ python ternary_test.py
True

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^