Last Updated:

Conditional constructions in Python

In our article today, we'll talk about things like the if statement, as well as the while and for loop operators. As a result, we will give an idea of these algorithms, and then analyze various simple cases in order to explain to the reader the basic principles of working with the above operators.

Conditional constructions in Python

Let's start with an easy one: a conditional if statement

Surely you have already met him if you worked in Pascal. If so, you probably remember that this statement performs a user-defined set of actions that depends on a specific condition. Let's take a closer look at the use cases:

Using if

The syntax of this operator is quite simple:

if expression:
(instruction1)
(instruction2)

Let's take a closer look: after the if statement, your expression will be located. If it is true (or true), then the actions that were specified by the operator will be performed.

Note that an expression is true when the result is not zero, when the result is not an empty object, or when it is a logical truth. We would like to note that after the expression is always put ":".

And of course, consider the various use cases:

if 1:
print(«hi Name»)

The program will output: hi Name.

x = 5
if x == 5:
print(«hi Jessica»)

The program will output: hi Jessica.

y = 11
if y > 4:
print(«hi Mark»)

The program will output: hi Mark.

lst = [4, 5, 6]
if lst :
print(«hi Ivan»)

The program will output: hi Ivan.

2. Design if – else

In programming, as in everyday life, there are often cases when we need to cooperate with alternative cases. Let's explain it more simply: when we have a true condition, only instructions are followed.

And when the condition is false, completely different instructions are connected. In programming, we use a special construct for this, which carries the if – else command.

if expression:
(instruction 1)
(instruction)
else:
(instruction)
(instruction)

Let's rather look at various examples:

x = 5
if x > 3:
print(«Y»)
else:
print(«B»)

The program will output: Y.

j = 8
if j > 11:
print(«K»)
else:
print(«N»)

The program will output: N.

3. Конструкция if – elif – else

When we have more than one alternative, we use a wonderful construct that looks like if – elif – else.

if expressionA:
statements (block_1)
elif expressionB:
statements (block_2)
elif expressionC:
statements (block_3)
else:
statements (block_4)

Now let's look at a few specific cases:

x = int(input("Enter: "))
if x < 0:
print("Negative")
elif x == 0:
print("zero")
else:
print("Positive")

If you enter a number < zero, the program displays "Negative", if the number = zero - "Zero", if the number > zero - "Positive".

while loop operator

It executes the user-selected set of instructions if the loop condition is true. If you have forgotten how to define the condition of truth, take a look at the point of working with the if statement, because the principle of definition is the same. Now let's look at what the syntax of this operator looks like:

while expression:
instruction
instruction

Separately, we note that the set of instructions that the operator performs is called the body of the loop.

Now let's look at the cases with while:

b = 0
while b < 6:
print(«G»)
b += 1

The letter "G" will be printed by the program six times.

A rather interesting case is an example of an infinite loop:

c = 0
while c == 0:
print(«Р»)

Break and continue statements

 

Also, when working with loops, it is not uncommon to use statements such as break and continue.

Note that break immediately interrupts while.

Now let's look at an example:

z = 0
while z >= 0:
if z == 11:
break
z += 1
print(«M»)

In this code, the loop will end as soon as z reaches 11. Separately, we take out that in the absence of this condition, the work of the cycle would occur indefinitely.

The continue statement, by contrast, returns the loop to work again. Note that the code that comes immediately after this statement is not produced.

And let's look at a specific case for better consolidation.

k = -1
while k < 11:
k += 1
if k >= 9:
continue
print(«N»)

After running the code, the "N" will be printed 9 times, although there will be 12 loop passes.

For loop operator

This statement executes a user-defined set of actions a certain number of times.

And consider the simplest use case:

for i in range(8):
print("Love")
As a result, “Love” will be displayed 8 times.

We would like to draw your attention to the fact that break and continue operators are quietly used inside the body of the loop. They work exactly the same way as with while.