'if' 'else' 'elif' Python 3

If



So, as you can see we’ve got the output printed ‘You are old enough to vote’ and once we’ve got the age in the numeric format, we can actually use the greater than, and the equal, as we’ve used here, to actually check to see whether it is greater than or equal to 18, and we’re doing that using if statement, with a condition to check. So, if the condition is true, in this case the age was greater than, or equal to 18. Then, we’re printing the message that you’re old enough to vote. If the age is less than 18, nothing happens. It actually would go into the next line.

Else

Now, we can put in something called an else.



So, how the program works is, it comes to and looks for the ‘if’ condition, and if that is true than that line got executed, not the next line with the else condition but if the age entered is not greater than or equal to 18, we get the second part. So, basically if the first condition got false than it comes to the second condition.

Elif


Now, sometimes you might need more than 2 cases, so what Python does it lets us test condition by letting us the ‘elif’ which is an abbreviation for ‘else if’.






So, the indentation level is very important to actually getting an idea, of what’s actually working because the first ‘if’ is completely different to the second ‘if’.

In this we had used many ‘if’, ‘else’ and ‘elif’. If the first condition got true then it doesn’t go to the second and if the first condition got false then it goes to the second condition.


Comments

Popular posts from this blog

String Slicing and Picking sub-string in Python3

Integers and Float in Python3

Variable in Python3