top of page
  • Writer's pictureGianluca Turcatel

Python Conditionals



Conditionals, as the name suggests, allow the conditional execution of a statement or series of statements based on the values of a specific expression. Conditionals include if, elif and else statement.


If statement

if condition:

do something

##Create a small script that check if you are an adult
age = 20

if age > 18:
    print('You are an adult')

: You are an adult

The use of the if statement is straight forward. We specify a condition after the if statement. If the condition is met, all the instructions inside the if statement are executed. In the case above we may want to print something in the case the condition is not met --> the else statement helps us with that.


If/else statements

if condition:

do something

else:

do something else

##Create a small script that check if you are an adult - complete script
age = 20

if age > 18:
    print('You are an adult')
else:
    print('You are under age')

: You are an adult
--------------------------------------------------------------------------
age = 10    #Lower the value of the age variable

if age > 18:
    print('You are an adult')
else:
    print('You are under age')

: You are under age

Implementing the else statement gives us more flexibility, but we are still limited to testing 2 scenarios. If we want to test several conditions we use the elif statements.


If/elif/else statements

if condition1:

do action1

elif condition2:

do action2

elif condition3:

do action3

.

.

else:

do something else


##Create a script that check if we are driving slow, at normal speed, fast or very fast
speed = 5

if speed < 20:
    print('You are driving slow')
elif speed < 35:
    print('You are driving at normal speed')
elif speed < 60:
    print('You are driving fast')
else:
    print('You are driving really fast')

: You are driving slow
--------------------------------------------------------------------------
speed = 50   # Change the value of the variable speed

if speed < 20:
    print('You are driving slow')
elif speed < 35:
    print('You are driving at normal speed')
elif speed < 60:
    print('You are driving fast')
else:
    print('You are driving really fast')

: You are driving fast

With the implementation of elif statements we can generate complex scripts that can handle all possible scenarios. Something to keep in mind is the order on which the conditions are arranged will have a crucial impact on the functionality of the script. For instance in the example below, the script will fail to execute properly.

speed = 10

if speed < 60:
    print('You are driving fast')
elif speed < 35:
    print('You are driving at normal speed')
elif speed < 20:
    print('You are driving slow')
else:
    print('You are driving really fast') 

: You are driving fast

This happened because Python determined that the condition of the if statement was met, executed the operation associated with the if statement and ignored the rest of the script. Thus, keep in mind that when working with conditionals be mindful of the sequence of the conditions being tested and make sure the proposed sequence is logically correct.


Before ending this short article on conditionals I want you know that conditional can be arranged in one single line. The example below explains what I mean with that

BMI = 22

if BMI < 18.5: print('Underweight')
elif BMI < 25: print('Normal weight'); print('Healthy!')
else: print('Overweight')

: Normal weight 
: Healthy!

Note that I got rid of the indentation, and the statement of each conditional is arranged on the same line of the conditional it is associated to. Multiple statements can be executed with this one-single-line approach but need to be separated by ; (check the elif statement in the example).


Follow me on Twitter and Facebook to stay updated.


16 views0 comments

Recent Posts

See All

Comments


bottom of page