Python statements if,,elif, else. How does these work.

Statements If, elif, else in Python.



Learn what statements if, elif,else are, how and when to use them.

 

What is if,elif, else in Python

 If, elif, else statements allow to execute code based on decision making.


How does if, elif,else works.

Statements if,elif,else execute particular part of code if condition is made.
It works this way:
 if x is equal to y:
    do something
otherwise if x equal to i:
        do that 
otherwise do this.

Let's learn if, elif, else statements deeply with examples now.

On first example we check if x i bigger than 5 or queal to 5 or bigger than five.  The code to do that would look like below:
x = 3

if x < 5:
print(' x is less than 0')
elif x == 5:
print(' x is equal to 5')
else:
print(' x is bigger than 5')
__________________________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

x is less than 0

Now lets check if some element is in a list.
animals = [ 'cow', 'sheep', 'horse']

x = 'sheep'

if x in animals:
print(x, 'is in animals')
__________________________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

sheep is in animals
We have assigned x to 'sheep' then we're telling Python to check if x with value sheep is in a animals list.
if x in animals:
If statement executes code in the block if condition is met in this situation if  with assigned 'sheep' value is in a list.: 
print(x, 'is in animals list')
Now we can expand the code to do something if there's no element in a list we're looking for so the program will tell if element no in a list.

animals = [ 'cow', 'sheep', 'horse']
y = 'chicken'

if y in animals:
print(y, 'is in animals')
else:
print(y, 'is not in animals list')
__________________________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

chicken is not in animals list

Let's make code more complicated with if, elif, else statements.

animals = [ 'cow', 'sheep', 'horse', 'eagle']

for element in animals:
if element == 'cow':
print('Cow does: muuu;')
elif element == 'sheep':
print('sheep does beeee.')
elif element == 'horse':
print('horse does yeeaaa.')
else:
print(element, 'I do not know what',element,'does.')
__________________________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

Cow does: muuu.
sheep does beeee.
horse does yeeaaa.
eagle I do not know what eagle does.
In animals list we have: cow,sheep,horse and eagle.
I assume your familiar already with range function (if not you can learn it here: ). We told Python to iterate over the animals list and check if is equal to our conditions. With if,elif  statements we've defined what to do with each element in a list. We made an else: statement for an animal we don't know so we can't say what it sound it does. In human language it is:
for each element in a animals list:
if element is cow:
print('Cow does: muuu.')
otherwise if element is sheep:
print('sheep does beeee.')
otherwise:
print(element, 'I don not know what', element, 'does.')
__________________________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com



Comments