if (boolean condition):# if's code block code hereelse:# else's code block# end of the conditionalscode continues here
The ifโs code block only execute if its boolean condition is True
The elseโs code block will execute only when the ifโs boolean condition is False
This helps us create two pathways in our code
# Code Example 1num =100if num <0and num >100:print(num, 'is invalid.')else:print(num)# since the if statement's condition is False we get to execute just the print(num) statement
Output:
100
# Code Example 2word ='hello world!'if'o'in word:print('The letter o exists.')else:print('The letter o does not exist.')# examine that the print statement inside the else statement does not execute.