Conditionals

Conditionals are used to identify and execute actions based on if-else cases.


If-else statements

If, elif, and else statements are used with Boolean values to direct programs. Example:


Note that an elif statement can be read as "else if", which means that if the previous if or elif statement evaluates to False, we'll evaluate the current elif statement next.

If statements must always be used in the if-elif-else order, and the program will evaluate each of these statements in that order. However, we don't always need to include an elif or else statement--we can write if-elif or if-else statements. This depends on what we want the function to actually do.


If-else expression

We can write shorter code by using if-else expressions to directly return what we want.



Of course, in the above example, we could also just return the Boolean value of the n%2==0 expression: