Learn Python like a Professional! Take You from 0 to Hero
In this Python lecture, we will learn about the if, elif & else.
"It either returns True or False according to the condition".
if statement
x = 2 y = 1
if x > y : print('x is greater than y') # indentation means this statement belongs to the if block
In above example the if condition x>y is true.
So, the print() statement belongs to if block is executed.
# What is the less than operator is used if x < y : print('x is greater than y') print('Outside the if block')
In above example the if condition x<y is false.
So, the print statements inside the if block is not executed.
And the print function outside the if block is executed.
if & else statement
x = 4 y = 5
if x > y : print('x is greater than y') else: print('x is not greater than y')
In the above example, it will check the if condition x > y.
Since the condition is false. So, the print statements inside the if block is skipped and it will jump to else statement.
Then the next else block is executed and it will display print message "x is not greater than y".
if, elif & else statement
x = 1
if x==1 : print('x is equal to 1') elif x==2 : print('x is equal to 2') elif x==3 : print('x is equal to 3') else: print('x is the number other than 1,2,3')
In the above example, the if condition x==1 is true.
So, the print() statement belongs to if block is executed
And skipped the other elif and else statement.
x = 2
if x==1 : print('x is equal to 1') elif x==2 : print('x is equal to 2') elif x==3 : print('x is equal to 3') else: print('x is the number other than 1,2,3')
In the above example, it will check the if condition x==2.
Since it is false. So, the if block is not executed and it will jump to elif.
Since elif condition x==2 is true.
So, elif block is executed and it will display print message "x is equal to 2".
x = 3
if x==1 :
print('x is equal to 1')
elif x==2 :
print('x is equal to 2')
elif x==3 :
print('x is equal to 3')
else:
print('x is the number other than 1,2,3')
In the above, it will check the if condition x ==3.
Since it is false. So, the if block is not executed and it will jump to next elif condition.
Since elif condition x==2 is also false. So, elif block is not executed and it will jump to next elif condition.
Since elif condition x==3 is true. So, elif block is executed and it will display print message "x is equal to 3".
x = 5
if x==1 :
print('x is equal to 1')
elif x==2 :
print('x is equal to 2')
elif x==3 :
print('x is equal to 3')
else:
print('x is the number other than 1,2,3')
In the above example, it will check all condition one by one.
Since all the conditions are false. So, it will jump to else condition.
And display the print message "x is not equal to 1,2,3".
EDITS ARE WELCOMED!!Since all the conditions are false. So, it will jump to else condition.
And display the print message "x is not equal to 1,2,3".
In the next Blog, we will discuss for loop
https://sngurukuls247.blogspot.com/2018/09/python-ninja-bootcamp-17-for-loop.html
......................................................................................................................................
Instagram-
https://www.instagram.com/python.india/
View the Jupyter Notebook for this lecture
Download the Jupyter Notebook for this lecture
https://sngurukuls247.blogspot.com/2018/09/python-ninja-bootcamp-17-for-loop.html
......................................................................................................................................
Follow the link below to access Free Python Lectures-
https://www.youtube.com/channel/UCENc9qI7_r8KMf6-_1R1xnwInstagram-
https://www.instagram.com/python.india/
View the Jupyter Notebook for this lecture
Feel free contact me on-
Email - sn.gurukul24.7uk@gmail.com
No comments:
Post a Comment