What Are If-else, And Elif Statements: Watch Python Make Decisions

Imagine you are a calculator 🔢🔣 throughout the post. Come on there is no loss in imagining this. In fact, it will help you understand conditional statements python if-else-elif and will also enable you to make yourself(I mean a calculator 🔢🔣)😅

To read if-else-elif official documentation click here.

But, wait, before being a calculator let’s look at the previous post’s challenge’s solution.

Previous post’s challenge’s solution

print("\nWelcome to the fun world ladies and gentleman\n")
print("""
⠀⠀⠀⠀⠀⠀⠀⣠⡔⠚⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠑⠢⢄⠀⢀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⢀⠔⠋⠻⢿⣷⣦⣤⣀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣤⣽⣧⣷⠀⠀⠀⠀
⠀⠀⢀⠴⠁⠀⠀⠀⠀⢈⣹⣿⣿⣷⠀⠀⠀⠀⠀⢾⣿⣿⣟⠋⠁⠈⠳⡀⠀⠀
⠀⢠⠊⣀⣤⠴⠶⠖⢛⠻⣿⣿⣭⣀⣀⠀⠀⠀⠀⠀⠉⠻⡿⠳⠖⠶⠶⠟⠓⠲
⣴⢣⡞⡉⢄⠒⡈⠜⡀⢾⣏⠉⠙⠛⠻⠿⢿⣿⣶⣶⣿⠿⢷⡡⢘⡐⢢⠘⡄⢂
⣿⡏⡐⢌⠢⠑⡌⠂⢥⣿⣿⣦⣤⣀⣀⠀⠀⠀⠀⠀⠀⣀⣼⣷⠠⠘⡄⠣⡐⠂..
⡟⣷⠀⢆⠢⡑⣈⢡⡾⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡷⣅⠰⢁...⡡
⡇⠈⠻⣦⣴⣤⠶⠋⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠈⠉⠓⠒⢪
⡇⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡃⠀⠀⠀⠀⢸
⣇⠀⠀⠀⠀⠀⠀⠀⠀⠀⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⢸
⠘⡄⠀⠀⠀⠀⠀⠀⠀⠀⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠀⠀⠀⠀⢀⠏
⠀⠙⣄⠀⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⠀⠀⠀⣠⠏⠀
⠀⠀⠈⠢⡀⠀⠀⠀⠀⠀⠀⠀⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⠀⠀⢀⡴⠁⠀⠀
⠀⠀⠀⠀⠈⠲⢄⠀⠀⠀⠀⠀⠀⠉⠿⣿⣿⣿⣿⣿⠿⠋⠀⣠⠔⠋⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠙⠒⠤⣤⣀⣀⣀⣀⣀⣈⣉⣉⣠⠤⠖⠏⠁
""")

fun_world_dictionary = {'Bumfuzzle': "confused",
                        'Everywhen': "all the time",
                        'Erf': "plot of land",
                        'Obelus': " the symbol used for division in a math problem",
                        'Bumbershoot': "umbrella"}

# I know that it's not quiet funnny(ok not at all funny). But, please forgive me for that

print("Enter any of the words below and press enter to know its meaning in the fun world")
list_of_words = list(fun_world_dictionary.keys())
print(list_of_words, "\n")


word_to_be_searched = input("Enter the word here:- ")
print("searching...\n")
meaning =""

if word_to_be_searched=="Bumfuzzle":
    meaning= fun_world_dictionary['Bumfuzzle']

if word_to_be_searched=="Everywhen":
    meaning= fun_world_dictionary['Everywhen']

if word_to_be_searched=="Erf":
    meaning= fun_world_dictionary['Erf']

if word_to_be_searched=="Obelus":
    meaning= fun_world_dictionary['Obelus']

if word_to_be_searched=="Bumbershoot":
    meaning= fun_world_dictionary['Bumbershoot']


print(f"The meaning of '{word_to_be_searched}' in our world is:- {meaning}\n")

Output:-

Welcome to the fun world ladies and gentleman


⠀⠀⠀⠀⠀⠀⠀⣠⡔⠚⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠑⠢⢄⠀⢀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⢀⠔⠋⠻⢿⣷⣦⣤⣀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣤⣽⣧⣷⠀⠀⠀⠀
⠀⠀⢀⠴⠁⠀⠀⠀⠀⢈⣹⣿⣿⣷⠀⠀⠀⠀⠀⢾⣿⣿⣟⠋⠁⠈⠳⡀⠀⠀
⠀⢠⠊⣀⣤⠴⠶⠖⢛⠻⣿⣿⣭⣀⣀⠀⠀⠀⠀⠀⠉⠻⡿⠳⠖⠶⠶⠟⠓⠲
⣴⢣⡞⡉⢄⠒⡈⠜⡀⢾⣏⠉⠙⠛⠻⠿⢿⣿⣶⣶⣿⠿⢷⡡⢘⡐⢢⠘⡄⢂
⣿⡏⡐⢌⠢⠑⡌⠂⢥⣿⣿⣦⣤⣀⣀⠀⠀⠀⠀⠀⠀⣀⣼⣷⠠⠘⡄⠣⡐⠂..
⡟⣷⠀⢆⠢⡑⣈⢡⡾⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡷⣅⠰⢁...
⡇⠈⠻⣦⣴⣤⠶⠋⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠈⠉⠓⠒⢪
⡇⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡃⠀⠀⠀⠀⢸
⣇⠀⠀⠀⠀⠀⠀⠀⠀⠀⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⢸
⠘⡄⠀⠀⠀⠀⠀⠀⠀⠀⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠀⠀⠀⠀⢀⠏
⠀⠙⣄⠀⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⠀⠀⠀⣠⠏⠀
⠀⠀⠈⠢⡀⠀⠀⠀⠀⠀⠀⠀⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⠀⠀⢀⡴⠁⠀⠀
⠀⠀⠀⠀⠈⠲⢄⠀⠀⠀⠀⠀⠀⠉⠿⣿⣿⣿⣿⣿⠿⠋⠀⣠⠔⠋⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠙⠒⠤⣤⣀⣀⣀⣀⣀⣈⣉⣉⣠⠤⠖⠏⠁

Enter any of the words below and press enter to know its meaning in the fun world
['Bumfuzzle', 'Everywhen', 'Erf', 'Obelus', 'Bumbershoot']

Enter the word here:- Obelus
searching...

The meaning of 'Obelus' in our world is:-  the symbol used for division in a math problem

Steps to solve the problem of the fun world man:-

  • First, we showed our manners and welcomed the one using our dictionary. We use escape sequences to make it look better.
  • Then, we went on to make the dictionary for the fun world.
  • After that, we displayed all the words in the dictionary using the keys function.
  • We created one variable to store the input word. And an empty variable to store its meaning.
  • Next, we used 5 if statements to know which was the word that the user entered. We stored the meaning of the word in the empty variable.
  • Finally, using the f-string we displayed the word and the meaning.

Ok, so calculator 🔢🔣 let’s first see what is an if statement.

If

You first need to understand that we use these conditional statements when a piece of code is to be executed at the satisfaction of condition(s).

For example, you as an intelligent calculator 🔢🔣 want to print a number only if it is positive.

It is super easy to code this in python you just need to use the correct syntax:-

num = 19
if num>=0:
    print(num)

Output:-

19

The syntax of the if statement goes like this:-

if condition to be satisfied:
     statement(s) to be executed if the codition is satisfied

In our case, the condition was that the number should be positive. Zero itself and all the numbers greater than 0 are positive that is why we wrote if num >= 0.

The statement to be executed in our case was to print the number so we gave a print statement inside of the if statement.

To tell python that a statement is inside the if statement we need to give an indentation(space) before the statement.

Now take a look at this code:-

num = 19
if num>=0:
    print(num)
print("This will always be printed")

Output:-

19
This will always be printed

Here, the print("This will always be printed") is outside the if statement whether the condition is satisfied or not this line would be printed.

if statements in python flow chart if-else-elif

If-else

Now, time for an if-else statement.

Say, you(smart calculator 🔢🔣) got some addition in work. You don’t only need to print a number when it’s positive. Instead, you need to print the number and then tell whether it is positive or negative.

num = -19

if num>=0:
    print(f"{num} is a positive number.")
else:
    print(f"{num} is a negative number.")

print("This will always be printed")

Output:-

-19 is a negative number.
This will always be printed

Here, first, python will check if the number is greater than or equal to 0 or not.

  • If it is then it will print the number saying it is positive.
  • If not, like in our case, it will go and execute the code inside the else statement. And as a result, will print the number saying it is negative.

We used this logic because any number 0 or greater than 0 is positive and in that case all the remaining numbers are negative.

if-else statements in python flow chart if-else-elif

If-else-elif

You(smart calculator 🔢🔣) turned out to be so good that you are now appointed to check whether a person is 18, more than 18, or below the age of 18.

And to let them know whether they qualify the age criteria for the driver’s license or not. For this, you’ll need to use an elif statement along with if and else.

age = 19

if age==18:
    print("You're 18 go through the next check to know whether you qualify for license or not.")
elif age > 18:
    print("You are above 18 you do qualify age criteria for license.")
else:
    print(f"You are not yet 18 come after {18-age} years to qualify the age criteria.")

Output:-

You are above 18 you do qualify age criteria for license.

#Output when age = 16
You are not yet 18 come after 2 years to qualify the age criteria.

#Output when age = 18
You're 18 go through the next check to know whether you qualify for license or not.

Here, python will first check whether the age is 18 or not if it is, it will go and execute the if statement.

If not it will check whether the age is greater than 18 or not.

If it is then it will execute the code inside the elif statement.

And if not it will go and execute the else statement.

You could have also written this code the following way:-

age = 19

if age==18:
    print("You're 18 go through the next check to know whether you qualify for license or not.")
if age > 18:
    print("You are above 18 you do qualify age criteria for license.")
else:
    print(f"You are not yet 18 come after {18-age} years to qualify the age criteria.")

Output:-

You are above 18 you do qualify age criteria for license.

#Output when age = 16
You are not yet 18 come after 2 years to qualify the age criteria.

#Output when age = 18
You're 18 go through the next check to know whether you qualify for license or not.

But this code would be inefficient compared to the one we wrote before. Because python,

  • Always checks to see if an ‘if‘ statement is true,
  • Checks ‘elif‘ statements only if each ‘if‘ and ‘elif‘ statement above it is false,
  • And ‘else‘ runs only when the conditions for all attached ‘if‘ and ‘elif‘ statements are false.
if-else-elif statements in python flow chart

Can elif be used without else?

The simplest answer is yes you can use an elif, in fact, multiple elifs without using else.

However, it is not usually done. But you can do this without facing any errors.👍

You just can not use an elif or an else statement without an if statement.

This means if is compulsory.❗

Nested if-else-elif

Ok, so now because of your excellent performance now you have been assigned a bit bigger task. First, you need to identify whether a number is positive or negative and then you need to identify whether it is an integer or a floating-point number.

num = -19

if num>=0:
    if type(num)==float:
        print(f"{num} is a positive floating-point number.")
    elif type(num)==int:
        print(f"{num} is a positive integer.")

else:
    if type(num)==float:
        print(f"{num} is a negative floating-point number.")
    elif type(num)==int:
        print(f"{num} is a negative integer.")

Output:-

-19 is a negative integer.

#Output when num = 19
19 is a positive integer.

#Output when num = 19.0
19.0 is a positive floating-point number.

#Output when num = -19.0
-19.0 is a negative floating-point number.

if-else Comprehension

We all know that python is known for making our work easy and with lesser lines of code. Python has got a way to shorten if-else statements too.

Say for example you want to input two numbers from a user and then want to check which one is greater. You don’t need 5 lines of code for that you can do that in just one line:-

a = int(input("Enter value of a:-\n"))
b = int(input("Enter value of b:-\n"))
print("a is greater than b") if a > b else print("b is greater than a")

Output:-

Enter value of a:-
10
Enter value of b:-
20
b is greater than a

The syntax of the if-else comprehension goes like this:-

"statement(s) to be executed if the codition is satisfied" if "conditon to be satisfied" else "statement(s) to be executed if the codition is not satisfied"

** ” ” these quotations are not a part of syntax they are just used to show the workings clearly.

Conclusion

In this post, you first became a calculator🔢🔣. Then, saw how can you print a number only when it’s positive.

After that, we saw how can you tell your user whether a number is positive or negative.

Next, we saw how can you tell whether someone qualifies in terms of age 👩‍✈️to get a driver’s license or not.

And then we saw whether the number inputted is positive or negative and also whether it is an integer or float.

Finally, we saw a way of using these conditional statements for lazy programmers👩‍💻👨‍💻 like me and you.

Challenge🧗‍♀️

Your challenge for this post obviously is to make a calculator which takes three inputs:-

  • two numbers
  • and one operator.

And as an output should give a number after performing the given operation on the given numbers.

Go fast. I am waiting. Comment your answers below. And remember if you don’t solve this I’ll do a magic spell 🧙‍♀️🧙‍♂️ to actually make you a calculator🔢🔣.

This was it for the post. Comment below suggestions if there and tell me whether you liked it or not. Do consider sharing this with your friends.

See you in the next post till then have a great time.😊

Leave a Reply

This Post Has 2 Comments

  1. Davidpiort

    Xin chao, toi mu?n bi?t gia c?a b?n.

  2. Maitry

    Can you ask the question in English, I cannot understand what you have written?