DEV Community

Cover image for Python3 Programming - Exercise 10 - Conditions
Michael Otu
Michael Otu

Posted on • Edited on

Python3 Programming - Exercise 10 - Conditions

Conditions

In the previous exercise, Exercise 9 (Logical and relational operators) , we discussed relational and logical operators. In this exercise we shall make use Truth values.

This exercise is about decision making, conditions.

When we want to display certain output or take input or even terminate the program based on a certain condition, in Python, we use the if, elif and else , statements which makes it easier to do some comparison.

If statement

This is the structure of an if statement.

if <condition>:
    # some code
Enter fullscreen mode Exit fullscreen mode

This starts with the if keyword, followed by a condition to evaluate then a colon, : . On a newline, we indent beyond the if keyword and add the code to execute when the condition is True .

Example

# A simple program to check if a driver is driving above
# the speed limit

MAX_SPEED = 120  # this is a constant, the speed limit

# get the drivers speed
drivers_speed = float(input('Enter Vehicle speed: '))

# check if the driver is driver over speeding
if drivers_speed >= MAX_SPEED:
    print("Please slow down, spare your family the cost and grief.")
Enter fullscreen mode Exit fullscreen mode

Note

Nothing happens when the drivers speed is below speed limit

Else statement

Perhaps we want to alert the user to do something when the condition fails or evaluates to False, then one must add an else statement.

if <condition>:
    # some code
else:
    # do something else
Enter fullscreen mode Exit fullscreen mode

Example

# A simple program to check is a driver is driving
# above the speed limit

MAX_SPEED = 120
drivers_speed = float(input('Enter Vehicle speed: '))

if drivers_speed >= MAX_SPEED:
    print("Please slow down, spare your family the cost and grief.")
else:
    print("waw, a very responsible being... You are one of a kind")
Enter fullscreen mode Exit fullscreen mode

Note

Python uses indentation for structuring or creating block codes. Consider the above example on if else statement, all the code in the indentation below the if statement is the body or block of the if . Those that are outside the if' s indentation forms another block of code. So make proper use of the indentations and use it correctly.

Elif statement

Let's assume that the if statement failed. The we would either execute the else block or we would want to check for another condition after the if statement had failed. We may want to check for another condition before the the else block, then we use the elif which is like, else if but together. Also there are certain instances whereby a condition is neither True nor False but something closer.

Consider when we take an integer input from a user, the input can be less than some constant, greater than that constant or even equal to that constant. Thus an elif statement becomes useful here.

Example

# A program to check the speed limit on the high way
# lets assume that the minimum and maximum speed limit
# is between 40 and 120 km/h.. something like this.

MIN_SPEED = 40
MAX_SPEED = 120

drivers_speed = float(input('Enter speed: '))

# method 1 - using else with a nested if
if drivers_speed < MIN_SPEED:
    print(f"Please drive at least {MIN_SPEED}km/h")
else:
    if drivers_speed > MAX_SPEED:
        print(f"Please drive at most {MAX_SPEED}km/h")
    else:
        print("Rock on man")

# method 2, we use elif
if drivers_speed < MIN_SPEED:
    print(f"Please drive at least {MIN_SPEED}km/h")
elif drivers_speed > MAX_SPEED:
    print(f"Please drive at most {MAX_SPEED}km/h")
else:
    print("Rock on man")
Enter fullscreen mode Exit fullscreen mode

Nested Conditions

We may have a nested if else statements as many as We please

Example

# this code has something to do with the above
# but here we check if the driver has parked
# or is over speeding senselessly.
# we assume the driver to be reversing when the drivers 
# speed is a negative value

PARKED_SPEED = 0
MIN_SPEED = 40
MAX_SPEED = 120
OVER_SPEED = 200

drivers_speed = float(input('Enter speed: '))

if drivers_speed < MIN_SPEED:

    # check if the vehicle is parked on the highway
    if drivers_speed == PARKED_SPEED:
        print("Please don't park on the high way.. It's deadly man.")
    else:
        print(f"Please drive at least {MIN_SPEED}km/h")

elif drivers_speed > MAX_SPEED:

    # check if the vehicle is really light speed
    if drivers_speed > OVER_SPEED:
        print("Please We are not on a racing track, We are over speeding")
    else:
        print(f"Please drive at most {MAX_SPEED}km/h")

else:

    # check if the vehicle is reversing
    if drivers_speed < PARKED_SPEED:
        print("what the heck man, no reversing on the highway")
    else:
        print("Rock on man")

# As We may see, we can have even more nested if and else and
# elifs as much as we can provided they don't make the code
# hard to read
Enter fullscreen mode Exit fullscreen mode

Rewrite the above code with elif

PARKED_SPEED = 0
MIN_SPEED = 40
MAX_SPEED = 120
OVER_SPEED = 200

drivers_speed = float(input('Enter speed: '))

if drivers_speed < PARKED_SPEED:
    print("what the heck man, no reversing on the highway")
elif drivers_speed == PARKED_SPEED:
    print("Please don't park on the high way.. It's deadly man.")
elif drivers_speed < MIN_SPEED:
    print(f"Please drive at least {MIN_SPEED}km/h")
elif drivers_speed > OVER_SPEED:
    print("Please we are not on a racing track, you are over speeding")
elif drivers_speed > MAX_SPEED:
    print(f"Please drive at most {MAX_SPEED}km/h")
else:
    # At this point it is that the driver is driving
    # between MIN_SPEED and MAX_SPEED
    print("Rock on man")
Enter fullscreen mode Exit fullscreen mode

Note

White spaces, such as, spaces, newlines and indentation only makes our code pretty to read but has no effect on the code we write.

Compound statements

Remember relational and logical operators? We have made use of relational, what about logical?

Example

# this code is the same as above but just serving some
# different concept. we know it is bad to park or reverse
# on the high way. So we can evaluate these two together,
# the maximum speed limit and the over speeding limit are all
# the same. we can either use a relational or a logical,
# either would do. for the relational, over speeding limit 
# is greater than the maximum speed thus we may check for 
# maximum speed

PARKED_SPEED = 0
MIN_SPEED  = 40
MAX_SPEED  = 120
OVER_SPEED = 200

drivers_speed = float(input('Enter speed: '))

# we use `<=` to compound reversing and parked
# we use `or` to compound the reversing or parked and 
# the minimum speed
if (drivers_speed <= PARKED_SPEED) or (drivers_speed < MIN_SPEED):
    print(f"Please drive at least {MIN_SPEED}km/h")
elif drivers_speed > MAX_SPEED:
    print(f"Please drive below {MAX_SPEED}km/h")
else:
    print("Rock on man")

Enter fullscreen mode Exit fullscreen mode

Note

# consider, when given
MAX_SPEED  = 120
OVER_SPEED = 200
drivers_speed = 210

# if We would like to catch OVER_SPEED, We check for 
# OVER_SPEED first else it won't be caught

if drivers_speed > OVER_SPEED:
    print("Over speed")
elif drivers_speed > MAX_SPEED:
    print("Max speed")
else:
    print("Rock on")

# unlike this example, OVER_SPEED would never be reached
# because OVER_SPEED is greater than MAX_SPEED
# even when drivers speed is 3000km/h
if drivers_speed > MAX_SPEED:
    print("Max speed")
elif drivers_speed > OVER_SPEED:
    print("Over speed")
else:
    print("Rock on")

Enter fullscreen mode Exit fullscreen mode

Practicals

  • Write a program that checks if a given integer input is a multiple of 2, 3 or both 2 and 3.
  # the code should behave this way

  # input = 4
  # output = 4 is a multitple of 2

  # input = 6
  # output = 6 is a multitple of 2 and 3

  # input = 9
  # output = 9 is a multitple of 3

  # input = 18
  # output = 18 is a multitple of 2 and 3

  # input = 5
  # output = 5 is not a multiple of 2 or 3
Enter fullscreen mode Exit fullscreen mode

Summary

  • if else and elif statement are used to create conditional statements.
  • use : to create a block, followed by a consistent indentation
  • the body of the if block is reached only if the condition evaluates to True
  • We may have compound conditions in the if and else statement

Top comments (0)