DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Making Decisions in Python
Suleiman Ibrahim
Suleiman Ibrahim

Posted on

Making Decisions in Python

As humans and an intelligent species, we always make decisions before taking some actions. These decisions can either be based on knowledge or choice. It is true that every human has the ability to make decisions, but what about the computer? Is it yet intelligent enough to make decisions for itself? We will find out from this article.

Table of Contents

Prerequisite

Have the latest version of Python installed on your computer

Making Decisions In Python

Python allows us to write programs that are executed only if some conditions are true. This paradigm of programming are sometimes called control flows. They control the flow of execution from one part of the program to another. They decide which part of our programs should be executed and which part should not. This is the basis on which the computer makes decisions.

Python provides for us mainly three constructs for making decisions, which are:

  • The if statement
  • The if … else statement
  • The if … elif … else statement

The "if" Statement

The basic syntax for the if statement is:

if this is true:
do this

Two important things to note from this syntax are the colon : and the indentation. Unlike other languages like Java and JavaScript that use curly braces to wrap a block of code, Python uses indentation instead. You will see the indentation syntax more often when you start dealing with functions and methods in python.

For your code to run properly, the colon and the indentation must be properly written. The recommended way to indent in Python is to use 4 space indentation according to PEP8 style guide. If the statement to execute when an expression is true is not indented, it will still execute, but irrespective of whether the boolean expression is true or not.

command = 'Greet'

if command == 'Greet':
   print('Hello, World!')


## output
# Hello, World!
Enter fullscreen mode Exit fullscreen mode

The program above checks for the value of command == Greet and prints ”Hello, World!” to the console if it evaluates to true. This condition is known as a Boolean expression. An expression that evaluates to either true or false. Also, note the == operator that was used for the boolean expression. This is called the comparison operator and it’s different from the assignment operator =.

Below are the comparison operators Python uses to evaluate a boolean expression:

Operator Meaning
== is equal to
!= is not equal to
> is strictly greater than
< is strictly less than
>= is greater than or equal to
<= is less than or equal to

These operators return either True or False when used and they all accept two arguments, to the left, and to the right.

Let’s now modify the program above to see what will happen if the boolean expression is not true:

command = 'Greet'
# when the boolean expression is not true

if command == 'greet':
   print('Hello, World!')
print('Bye')


## output
# Bye
Enter fullscreen mode Exit fullscreen mode

The code inside the if block was not executed because Greet is not equal to greet. They both have different Unicode values. The if statement is the basic way our programs can make decisions by themselves.

The "if … else" Statement

The syntax for the if … else statement is:

if <this is true>:
<do this>
else:
<then do this>

This syntax is similar to the if statement, but the code in the else block is executed only when the conditions in the if block evaluate to false. The else block is never executed when the if block evaluates to true.

greet = False
if greet:
   print('Hello, World!')
else:
   print('The system is unable to greet you')
print('Bye')


## output
# The system is unable to greet you
# Bye

Enter fullscreen mode Exit fullscreen mode

The variable greet is used to hold a boolean value, which is what a boolean expression will return when executed. But greet is false, which means the if block won’t get executed, leaving the else block to get executed. And the last line of the code that prints ”Bye” gets executed no matter what.

Nested "if" statement

The nested if statements allow you to nest an if statement inside another if statement. This is useful when you want to make decisions if a particular decision is true or not.

i_am_human = True
height = 6
if i_am_human:
   if height >= 6:
       print('I am a tall human')
   else:
       print('I am an average height human')
else:
   print('I am not a human')


## output
# I am a tall human
Enter fullscreen mode Exit fullscreen mode

The output of the program is based on the fact that i_am_human is true and height is 6 or above. If i_am_human was false, then the program won’t bother to check for the inner conditions, but rather jump to the else block and output ” I am not a human”.

The "if … elif … else" Statement

The if … elif … else statements solve the problem when there exists more than one condition to be tested. In a more complex program, if a condition fails to be true, the system doesn’t quit immediately. Some other conditions need to be tested before quitting.
The syntax for the if … elif … else statements is:

If <this is true>:
<do this>
elif <the above is not true>:
<do this>
else:
<do this if none of the above is true>

The elif is short for else if. You can have as many elif as desired in your programs. The last else is optional. You can choose to add it or leave it out of your programs. Let's see how this works:

age = 19
if age == 17:
   print('I am 17 years old')
elif age == 18:
   print('I am 18 years old')
elif age == 19:
   print('I am 19 years old')
else:
   print('Age not found')


## output
# I am 19 years old
Enter fullscreen mode Exit fullscreen mode

Suppose you change the value of age to 18 as follows:

age = 18
Enter fullscreen mode Exit fullscreen mode

Then the output is returned:

I am 18 years old
Enter fullscreen mode Exit fullscreen mode

But what if the age is not among the ages to be compared. This is where the else block is executed. When the age from the example above is set to 21, then none of the conditions are satisfied. Only then, the else block is executed.

age = 21
Enter fullscreen mode Exit fullscreen mode

The output will be:

Age not found
Enter fullscreen mode Exit fullscreen mode

Conclusion

The advent of control structures in programming that allows programmers to write programs that makes decision is big turn around as programmers don't have to programs that only run in sequence anymore. Python took advantage of this structure and implemented it in using selection statements like "if", "if ... else", and "if ... elif ... else".
This article gives a brief overview of all you need to know about the basics of selection statements in Python and how to apply them if your everyday programs.
To know more about selection statements and Python as a whole, I recommend you check out the Python documentation tutorial.

Feel free to drop your thoughts and suggestions in the discussion box. I will be available to attend to them. And also, if you have any questions, you can as well drop them in the discussion box.

Top comments (2)

Collapse
 
lweiner profile image
Lukas Weiner

I think I would appreciate it if you would tell the people that the age example is not that ideal, because you could just check if the age is in an array or you could even check if the age exists and then make a format string out of it. I think that would be a better solution for that problem. Other than that I think you post this quite useful and I generally like it.

Collapse
 
princeibs profile image
Suleiman Ibrahim

Thanks for the review @lweiner . I'll make possible corrections on the next update.

Stop sifting through your feed.

Find the content you want to see.

Change your feed algorithm by adjusting your experience level and give weights to the tags you follow.