Prev: - 'while' loop | Next: Break, continue, pass
if condition-1:
block-1
elif condition-2:
block-2
...........................
elif condition-N:
block-N
else:
block_else
# Python allows concise forms of composite conditions
x = int(input())
if 0 < x < 10: # instead of: 0<x and x<10
print('x in the (0,10) interval')
else:
print('x out of the (0,10) interval')
# When implementing multiple if branches: use elif - avoid if-else nesting
grade = int(input())
if 18.5 <= grade <= 20:
perform = 'Excellent'
elif 15 <= grade < 18.5:
perform = 'Very Good'
elif 12.5 <= grade < 15:
perform = 'Good'
elif 10 <= grade < 12.5:
perform = 'Accept'
else:
perform = 'Fail'
print(perform)
import random
a = random.randint(1,3)
if a == 2:
print('Equal')
elif a < 2:
print('Less than 2')
else:
print('Greater than 2')
print('End of if control, a =',a)
# Note the use of docstring enclosed in triple quotes
menu = '''
Please make a choice:
1. spam
2. chorizo
3. salchichón
4. jamon iberico de belotta
'''
print(menu)
c = input('Your choice: ')
if c=='1':
print('spam: €0.25')
elif c=='2':
print('chorizo: €2.45')
elif c=='3':
print('salchichón = €1.99')
elif c=='4':
print('jamon iberico de belotta = €100.10')
else:
print('Bad choice')
"True_expression if Condition else False_expression"
x=False
a=1 if x else 2
a
a = 'Alpha'
b='1'
print(a) if b else 'X'
. Free learning material
. See full copyright and disclaimer notice