#7 Else Statement

Previous post: 

Next post: https://codinghelp.site/knowledgebase/python/8-comments/

The “ELIF” Statement

The else statement in python is used with IF statements and commonly alongside an ELIF statement. The ELSE statement will always get ran if the IF or ELIF(s) statement(s) condition(s) aren’t met.

“ELIF” Statement Syntax

The ELSE statement has no expressions (see if & else expressions) meaning, it will get ran if the “IF” or “ELIF” statement(s) expression(s) aren’t met. The statement must be indented.

Examples

Here, we are setting the value of apples variable to 10. After that, we’re running an if statement to see if apple’s variable value is equal to 10, in this case, it isn’t. Then we run an ELSE statement. The ELSE statement will always get ran unless there is an IF or ELIF statement in which expression(s) have not been met. The result of this code would be “Apples are not equal to 10”.

Exercise

Write a script that asks for a number, then check if that number is smaller than 5 if yes, print “The number is smaller than 5”. After that make a else statement which will print “The number is not smaller than 5”.

Answer key

 

image