# Conditional operator. If then otherwise. Python programming for beginners

All previously considered programs had a linear structure: all instructions were executed sequentially one after the other, each written instruction is necessarily executed.

Such actions of the executor (of a Python program) can be called Sequential Actions and are described by successive lines of the program. It is worth adding, however, that in Python programs, indentation is important, so all operators that are part of a sequence of actions must have the same indentation.

Of course, sequential actions alone are not sufficient, so when writing algorithms, branching is also used. A branch is also called a conditional instruction - a fork in the path of program execution. Our program may take one path or the other. Which way is chosen depends on how the condition is checked.

Comparison statements
As a rule, the result of calculating one of the following comparison operators is used as a checked condition:

< Less - the condition is true if the first operand is less than the second operand.

> More - the condition is true if the first operand is greater than the second one.

<= Less than or equal.

>= More or equal.

== Equals. The condition is true if two operands are equal.

!= Inequality. The condition is true if two operands are unequal.

For example, the condition (x * x < 1000) means "the value of x * x is less than 1000", and the condition (2 * x != y) means "the doubled value of variable x is not equal to the value of variable y".

Comparison operators in Python can be combined into chains (unlike most other programming languages, where you have to use logical chords / logical operators for this), for example, a == b == c or 1 <= x <= 10.

Programming Help for beginners

Instruction block 1 will be executed if Condition is true. If Condition is false, Instruction Block 2 will be executed.

A conditional instruction may be missing the word else and the following block. Such an instruction is called an incomplete branching. For example, if a number x is given and we want to replace it with an absolute value of x, this can be done as follows:

1
2
3
if x < 0:
x = -x
print(x)
In this example, the value -x will be assigned to x, but only if x<0. But the print(x) instruction will be executed all the time, regardless of the condition being checked.

Let me remind you again (since this is very important in Python) that Python uses indentation to separate the block of instructions that belong to an if or else instruction. All instructions that belong to the same block must be indented equally, that is, the same number of spaces at the beginning of the line. It is recommended to indent 4 spaces and not to use the tab character as an indent.

This is one of the essential differences between Python syntax and that of most languages in which blocks are delimited by special words, e.g. nz... kz in Kumir, begin... end in Pascal or {figure} brackets in C.

Boolean operators
Sometimes it is necessary to check more than one condition at a time. For instance, if you want to check if a given number is even, you can use the condition (n % 2 == 0) (the residue from dividing n by 2 is 0) and if you want to check if the two given integers n and m are even, you should check both conditions: n % 2 == 0 and m % 2 == 0, which is done by combining them using the and (logical AND) operator: n % 2 == 0 and m % 2 == 0.

In Python, there are standard logical operators: logical AND, logical OR, logical negation (NOT).

The logical AND is a binary operator (that is, an operator with two operands: left and right) and has the form and. The and operator returns True if and only if both its operands are True.

A logical OR is a binary operator and returns True if and only if at least one operand is True. The logical OR operator is in the form of or.

A logical NOT (negation) is a unary (that is, with a single operand) operator and has the form not followed by a single operand. Logical NOT returns True if the operand equals False and vice versa.

Example. Check if at least one of the numbers a or b ends in 0:

if a % 10 == 0 or b % 10 == 0:

Let's check that the number a is positive and b is non-negative:

if a > 0 and not (b < 0):

Or we can write (b >= 0) instead of not (b < 0).