## Wednesday, August 14, 2019

The selective structure If else in Python

## 1 - The selective structure If

This structure is one of the most important in programming. The idea is to say that if such a condition is satisfied then, execute an instruction or a block of instructions that otherwise execute other instructions ...
Take the following example:

we give the age of a person. If the age is greater than or equal to 18, we will display the  message: 'You are an adult!' If no, we display the message 'you are a minor'

#### Example:

``````age = 17
if (age >= 18):
print ("You're major!")
else:
print ("You are a minor!")
# This example display the message : 'You are a minor!'``````

## 2 - Elif instruction

The elif statement is generally used when the exception has 2 or more cases to distinguish. In our example above the exception is age <18 which corresponds to the minor case. But the minor case involves both cases:

1. Childhood  age  < 14
2. Adolescence  14  < age  < 18

The else statement selects the opposite condition, which is age <18, and therefore can not distinguish between the two childhood and adolescence cases. So to solve this problem, we use the elif statement:

#### Example

``````
if (age>= 18):
print ("You are major!")
elif (age < 15):
print("You are too small!")
else:
print("You are a teenager!")
``````

Younes Derfoufi

## Python Exercises With Solutions

1. Python Arithmetic Exercises
2. Equations & System Of Equations
3. Polynomials and Functions
4. Python Arithmetic Exercises
5. Numpy, Matplolib, Sympy , Scipy - Exercises
6. Python Arithmetic Exercises

Younes Derfoufi

## Sample Text 