Binary Decisions

if and else statements


if (boolean condition):
    # if's code block

    code here
else:
    # else's code block

# end of the conditionals

code 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 1

num = 100

if num < 0 and 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 2

word = '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.
Output:
The letter o exists.

Last updated