Python supports conditional statements i.e. if, elif, else. Comparison operators and Boolean operators written in the previous section can be used in if-elif-else statements.
An “if statement” is written by using the if keyword.
Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose.
The elif keyword is Python’s way of saying “if the previous conditions were not true, then try this condition”.
The else keyword catches anything which isn’t caught by the preceding conditions.
if Statement Example:
name = Rabia
if name == ‘Rabia’:
print(‘Hi, Rabia.’)
if-else Statement Example
name = ‘Sajal’
if name == ‘Rabia’:
print(‘Hi, Rabia.’)
else:
print(‘Hello, stranger.’)
if-elif-else Statement Example:
name = ‘Sajal’
age = 22
if name == ‘Rabia’:
print(‘Hi, Rabia.’)
elif age < 12:
print(‘You are not Rabia, kiddo.’)
else:
print(‘You are neither Rabia nor a little kid.’)