We make decisions in our daily lives based on various factors, such as whether or not we are hungry. We choose to eat when we are hungry, but when we are not, we decide not to eat. In our daily lives, decision-making takes place in this manner.

Similarly, conditional statements are used in Python programming when making decisions are required. Programming requires conditional statements because they let you base decisions on specific conditions in your code. Depending on whether a condition is true or false, they are used to execute various blocks of code.

There are mainly three conditional statements in Python

  1. If
  2. Elif 
  3. Else.

Here is an example: let’s say we have a variable called temperature. We set it to 10.

If the temperature is below 5, we may want to display a message to the user.

We use an if statement. After “if,” we add a condition which is a boolean expression, an expression that produces a boolean value.

Let me explain. Here is the critical part that many beginners miss: when you use an if statement, you should permanently terminate your statement with a colon (:).

Our cursor becomes indented when we press ENTER. There are four white spaces in this instance. By using these indentations, the Python interpreter will know what statements to run if this condition is true, which makes this indentation important.

Here we want to print a message like “It’s Cold” we can also print another message,Wear Warm clothes.”

So we can have as many statements as we want here as long as they are indented; they belong to this if block. When we finish, we should remove the indentation to indicate the end of this if block.

So here, we can add a print statement with a message like “I’m Done.” This statement will always be executed whether this condition is true or not.

Alright, now let’s run this program, so because the temperature is not lower than 5, we see the dawn message whether our condition is true or not, as you can see.

If we change the temperature to 4 and run the program one more time, we see the first two messages and the dawn message.

If you want to execute both print statements if the condition is “true,” you must be careful and precise with your indentation. Any unintentional removal of the indentation on the fourth line may cause the program to malfunction and fail to produce the desired results. As a result, when dealing with an indentation in your code, you must always be careful.

elif

Now, what if you wish to have multiple conditions? You can evaluate numerous conditions in your code using the “elif,” which is short of “else if.” The “elif” statement enables you to define additional conditions to check in case the preceding “if” statement’s condition is not satisfied. With this, you can quickly examine multiple conditions and perform various actions based on the true condition.

Here we can add another condition with another expression. For example, if the temperature is greater than 40. Print a different message, “It’s Hot,” and save the changes.

Now, if you look, all these lines are indented consistently. You can have as many “elif” statements as you want.

else

You can also have an “else” statement. So if none of the previous conditions are true, then what you have in the else block will be executed.

Once again, we add “else” with a colon (:). We can add the message “It’s hot” and save the changes. In this case, we set the temperature to 10, so none of these two conditions will be true, and we will see “It’s Normal.” 

Ternary Operator

An “if, else” statement can be written in Python using the ternary operator in only one line of code. It offers a clear and readable way to represent a straightforward conditional expression. This is a technique for writing cleaner code. 

Let’s say we’re building an application for some organization, and we want to check to see if the person who’s applying for this organization program is Eligible or Not.

So, we start by defining a variable called age and setting it to 18.

Age = 18

Now, if the age is greater than or equal to 18, we want to printEligible”; else, we want to printNot Eligible.”

This code looks the same as below. Run the program and make sure it works.

There is nothing wrong with this code, but I want to show you a cleaner way to achieve the same result. Instead of having a print statement here, we can define a variable like a “message” and set it to the string “Eligible” and “Not Eligible,” which is the first step.

Then we will print this “message.”

Now when you have an “if, else” statement with this structure, you assign a value to a variable. You can rewrite this more simply. 

This is how it works: all we have to do over these few lines is to assign a value to this message variable.

We start with the “message” we set it to “Eligible” if the “age” is greater than or equal to 18; else, we set it to “Not Eligible.” This is how the code looks.

So, what we have on line 6 is equivalent to these four lines of code.

Delete these lines of code from 2 – 5, save the changes, and run the program. You can see this person is “Eligible.”

The program returns “Not Eligible” if we run it with “age” set to 14.

What we have here is called a ternary operator.

Logical Operations in Python

Python’s logical operations allow you to determine whether an expression is true and then draw conclusions from the results. In Python, we have three logical operators, and we use these operators to model more complex conditions, so these operators are:

  1. and 
  2. or
  3. not.

Let’s see a real-world example of using these operators. So imagine we’re building an application for processing loans, so we need two variables:

high_income = True

good_credit = True.

Now here’s the condition we want to implement.

If the applicant has a good income and credit score, they are Eligible for the loan.

So “if good_income and good_credit,” we add the colon (:) and print “Eligible.” Take a look below.

Now note that here I have yet to compare the value of this variable with true. That is one of the issues I see in many beginner’s codes that must be more varied and professional because “good_income” is a boolean. It’s either true or false. We don’t need to compare true with true.

So if this condition is true and this second condition is true, then we will print “Eligible” in the terminal. 

So save the changes and run the program. This person is “Eligible.” 

However, if one of these conditions is false, we will not see Eligible in the terminal. So let’s add an “else” statement and print “Not Eligible,” and run the program we see “Not Eligible.”

So this is how the “and” operator works with an operator. If both conditions are true, the result will be true.

Or 

In contrast with the “or” operator, the result will be true if at least one of the conditions is true.

So if we replace “and” with “or” here, we should see “Eligible” in the terminal. These are the “and” and “or” operators.

Not

The “not” operator is used to negate a condition. Take a look at an example of the “not” operator. I will define another variable, “student,” and set it to true.

Let’s say the person is “Eligible” if they are not a student. The “not” operator basically inverses the value of a boolean. In this case, the student is true. The result will be false when we apply the “not” operator. So, in this case, our condition will be false, and that’s why this print statement will not be executed.

So save and run the program you will see they are “Not Eligible.”

If the student is false, when we apply the “not” operator, our condition will be true, and we’ll see it as “Eligible.”

With this operator, we can model even more complex conditions.

Here is an example

A person can be “Eligible” if they have either good income or good credit, and they should not be a student. Let me show you how to implement this condition.

If (good_income or good_credit) we want at least one of these conditions to be true, we put these in parentheses(). We want to separate these from the other condition, which is not a student. Now the result of this should be true, which means at least one of these conditions should be true

After that, we’ll add “and not a student” and finally : (colon). So with these operators, you can model all kinds of real-world scenarios.

This is how the code looks.

Short-circuit evaluation

Short-circuiting enables the immediate determination of a boolean expression result, ignoring any additional evaluation if the outcome can be determined from the first portion. 

Like before, a person is Eligible for a loan if they have a good income and credit and are not a student. Now one thing you need to know about these boolean operators is that they are “short-circuit.” 

What do we mean by that?

When a Python interpreter wants to evaluate this expression, it starts from the first argument. If this is true, it continues the evaluation to see if the second argument is also true, so it continues the evaluation to the end of this expression.

However, the evaluation stops once one of these arguments is false.

Let me explain what I mean if we change good_income to false. When the Python interpreter sees this expression, it knows that high_income is false, so it doesn’t matter what comes after the result of this entire expression will always be false because at least one of the arguments or operands is false.

We call this short-circuiting, just like the short-circuit concept we have in electronics. 

So the evaluation stops as soon as one of these arguments evaluates to false.

The “or operator” uses a similar idea. If we switch these “and” operators to “or.” We know that at least one of the arguments must be true for the “or operator” to work.

As soon as we discover an argument that validates it as true, the evaluation, therefore, comes to an end. When the Python interpreter evaluates this expression in this situation, it discovers that good_income is false and proceeds with the evaluation hoping that the next argument will be true.

Here good_credit is true, so evaluation stops, and the result of this entire expression will be true. In Python, logical operators are “short-circuit.”

Chaining Comparison Operators

Now, I’m going to show you how to chain comparison operators. This is a very powerful technique for writing clean code. Python allows you to execute multiple comparisons in a single line by chaining comparison operators. By chaining comparison operators, you can check various conditions and determine whether a value satisfies a set of requirements.

Here is an example

Let’s say we want to implement a rule that says the age should be between 22 and 55.

Here’s how we can implement it, we define a variable like “age” and set it to 24. Now if the age is greater than or equal to 22 and less than 55, then we print “Eligible.”

This is how it looks.

Here’s a question: how do we write this rule in math?

We can write it like this

If <= 22 age < 55:

The age should be between 22 and 55. This is how we write this rule in math.

We can write the same expression in Python. Lines 4 and 3 are exactly equivalent, but as you can see, line four is cleaner and easier to read.

So let’s eliminate line 3, which we call the chaining comparison operator.