Conditional statements in python 3

by Alex
Conditional statements in python 3

The program can be executed gradually: line by line, expression by expression from beginning to end, without missing a single line of code. But this rarely happens in reality. It is often necessary to introduce variation, so that when a certain condition is met, one thing will happen, and when it is not met, another will happen. This is how conditional operators are used.


Conditional operators in Python 3 are sometimes called branching operators. They are designed so that the program can choose which instruction to follow when a certain value of a given variable is encountered. Conditional statements consist of a header and a body. The header is the construct itself and what’s next to it (not in curly brackets in other programming languages or before a colon in Python). The body is what is written after the colon. In order for the program to understand that the code after it is nested, you must indent it with four spaces. Usually they are placed automatically, if the user is working in a specially created programming environment.

Note that using the Tab key to indent code is highly undesirable.

There are many situations in which a conditional operator could be used. These include, for example:

  • Determination of whether the student passed the exam. If the number of correct answers is 10 out of 15 – passed, if less – did not pass;
  • Finding out the size of the discount. If you bought 10 items – 10% discount, if less – no discount.

Operator if

From English, the name of this operator is translated as “if”. That is, it sets a condition. After this operator the expression itself is written. If it is true, the program proceeds to the execution of the instruction specified in the specified operator. An expression is considered true when it:

  • is not equal to zero;
  • is not empty;
  • is logical

For clarity, let’s look at an example of the use of an if – condition in Python 3:

if 3:
    print("Hello 3")

The entry in front of the user will appear on the screen: Hello 3. Let’s complicate the code:

a = 6
if a == 6:
    print("Hello 2")

The computer will display: Hello 2. You can also specify the condition with a formula or a conditional expression:

a = 5
if a > 2:
    print("Hello 3")

The variable is 5, the condition is that it is greater than 2. It is true, so we will see in front of us: Hello 3. An even more complicated variant with multiple variables:

a = 0
b = 34
c = 10
if c < 100:
    a = b + c

Because c is really less than 100, a will become 44, and that is the number that will be displayed when this code is executed. If c had originally been greater than 100, e.g., 110, then 0 would have been displayed, the variable a would simply not change at all.

The else statement

Sometimes a program needs to tell you what to do if a condition is false. To do this you set up a new set of instructions and use the if – else construct. By the way, keep in mind that there can be no logical expression in else. It is also impossible that both branches (if and else) will be executed. Let’s consider an example:

a = 7
if a > 5:

When this code is executed, the computer will print: Yes. This is because 7 is actually greater than the number 5 specified in the condition. Let’s see what happens if the condition is false. Let’s take the same code, but change the variable a.

a = 3
if a > 5:

Obviously the word “No” will appear in front of the user, since 3 is not greater than 5, but less than that. Let’s complicate the code:

product1 = 30
product2 = 23
if product1 + product2 > 70 :
    print("70 rubles not enough")
    print("Money enough, all paid")

In this case the programmer will see the following entry: Money is enough, everything is paid, because 30 + 23 = 53, and this is less than 70.

The elif operator and the if – elif – else construct

In general, elif is roughly deciphered as else + if. In order to be able to realize a program that would choose among several alternatives, the above construction is used. Let’s consider a code with several conditions:

if balance < 0:
    print("Balance is below zero, put money in your account so you don't get fined")
elif balance == 0:
    print("Balance is zero, deposit money into account soon")
    print("Your balance is above zero, all is well")

So, the program can consider the three conditions. And if the balance variable is 150, the user will see the message: “Your balance is above zero, all is well. The elif operator allows you to simplify the code. Make it easier to read. Allows you to avoid writing multiple if conditions in Python.

Ternary operator

Sometimes a so-called ternary operator is used to shorten a record, it makes the code fit into a single line. Let’s look at an example of how you can write a Python condition on one line:

is_happy = True
state = "happy" if is_happy else "not happy

To avoid writing out the if statement in several lines, we’ll just use a condensed version.

The switch-construction – case

This instruction is not used in Python. Instead, you can use constructions with multiple elifs. For example, a programmer wants to write code so that the program will give a grade for a test passed by students. In this case there can be several grades. In this case the code can be written in this way:

if grade >= 50:
    print("Grade 5+")
elif grade >= 45:
    print("Grade 5")
elif grade >= 35:
    print("Grade 4")
elif grade >= 25:
    print("Grade 3")
    print("Test failed.")

In this case, the value of grade will be compared to all the conditions in a row. However, when the condition becomes true, the code will stop and the grade will be displayed. For example, if grade is 30, the user will see the message “Grade 3”, and if the variable is below 25, the user will see “Failed test”. If a novice programmer has a good command of conditional constructions, this makes it much easier to write programs and to label their actions more precisely. To complicate things, you can create whole combinations of operators and combine them using loops.

Related Posts