(Python) Conditional statement


Control Structures

Do you want to learn how to create powerful, "intelligent", versatile and useful programs?

Then you need to learn the three main forms of program execution control. According to the theory of computer systems, a good programming language should provide the implementation of three forms of control over the execution of programs.

1 - SEQUENCE:  execution of sequential statements - we have already met with this in earlier courses. All our previous programs are some sequence of statements.

2 - SELECT:
     if such and such a case,
              then do it
     otherwise do that.
3 - REPEAT:
UNTIL (as long as such and such cases — do it).

The second form – CHOICE between different ways of doing things makes programs more "intelligent" and also makes computers extremely efficient.

Let's start with a simple example. 
You must enter two real numbers from the keyboard and determine the largest of them.
Explore this example.

Conditional statement (if)

In the problem of finding the maximum number of two, we met a new operator that began with the word if. This operator is called CONDITIONAL operator.

The word if is translated from English as "if", and the word else - as "otherwise". If the condition after the word if is true (true), then all commands (operators) after the colon on a new line and located before the word else are executed. If the condition is false (false), then the commands after the word else:. 
are executed
Unlike other programming languages, indentation (shifts) statements relative to the left border are important in Python. Indentation affects program performance. If you look closely, the word if  and the word else start at the same level, and the commands that are executed are shifted to the right relative to this level the same distance (recommended to use 4 spaces).
 
GENERAL VIEW OF THE CONDITIONAL STATEMENT:
if condition: # title with condition
    # "if" block — statements that are executed
    # if the condition in the header is true
else:
    # block "otherwise" — statements that are executed
    # if the condition in the header is false

You have to remember!
1. if-else -  is one statement!
2. After the word else , the condition is never set (the condition is set only after the word if).
     Block "otherwise" is executed when the main condition specified after the word if   is false, i.e. not executed.
3. The statements to be executed in each branch (if or else) are written with the same indentation of 4 spaces. Indentation required! 
4. Condition is an expression that can be said to be true (i.e., met) or false (i.e., not met).
The condition uses signs of logical relations
  > , <                 more, less,
  >=, <=              greater than or equal, less than or equal,
  ==                     equals,
  !=                     not equal.

Incomplete conditional statement

If in the "otherwise" block you don’t have to do anything (for example: “if there is ice cream on sale, buy ice cream”, and if not ...), then the entire block “otherwise” you can omit and use an abbreviated (incomplete) form of the conditional operator:
if condition:
   ... # what to do if condition is true
The operation of choosing the maximum of two values ​​is used very often, so Python has a built-in function max that can be called in this way
M = max(A, B)
There is also a similar function for finding the minimum value of two or more values ​​- min(). 

When choosing from two values ​​in Python, you can use another form of the conditional operator, which works like the full form of the conditional operator.
M = a if a > b else b
If you need to do more than one  if the condition is met, then all actions are written one under the other at the same shift level:
if a > b:
  temp = a
  a = b
  b = temp
In this program, if \(a>b\), then we swap the values ​​of the variables. The temp variable is an auxiliary one.
Notice the same shifts from the left edge of all three operators. This tells the compiler that all three statements are executed provided that a>b.
Another subtlety of the Python language is the   multiple assignment operator, which facilitates the exchange of two variables. It can be written like this:
a, b = b, a