Flow control

Conditionals, We can make certain amount of the code to be executed only upon a certain condition met using the if statement. If the condition is not met, then we can execute the code following the else statement. If the first condition is not met, we can set the next condition for the code to be executed using the elif statement.

Input:

# source_code/appendix_c_python/example09_if_else_elif.py
x = 10
if x == 10:
        print 'The variable x is equal to 10.'

if x > 20:
        print 'The variable x is greater than 20.'
else:
        print 'The variable x is not greater than 20.'

if x > 10:
        print 'The variable x is greater than 10.'
elif x > 5:
        print 'The variable x is not greater than 10, but greater ' +
'than 5.' else: print 'The variable x is not greater than 5 or 10.'

Output:

$ python example09_if_else_elif.py 
The variable x is equal to 10.
The variable x is not greater than 20.
The variable x is not greater than 10, but greater than 5.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset