Python Conditionals: if, elif, else

Question Click to View Answer

What does the following code print to the console?

if 2 == 2:
    print("ice cream is tasty!")

"ice cream is tasty!" is printed.

The code block in an if statement is executed if the boolean condition associated with the if statement evaluates to True. In this example, the boolean condition (2 == 2) evaluates to True and the code block is executed.

What does the following code print to the console?

if "cat" == "dog":
    print("prrrr")
else:
    print("ruff")

"ruff" is printed.

The boolean condition ("cat" == "dog") evaluates to false, so the code block associated with the if is skipped and the code block associated with the else is executed.

What does the following code print to the console?

if 5 > 10:
    print("fan")
elif 8 != 9:
    print("glass")
else:
    print("cream")

"glass" is printed.

If the boolean condition associated with the if is False and the boolean condition associated with the elif is True, then the code associated with the elif will be executed.

What does the following code print to the console?

name = "maria"
if name == "melissa":
    print("usa")
elif name == "mary":
    print("ireland")
else:
    print("colombia")

"colombia" is printed.

If the boolean conditions associated with the if and elif are False, then the code associated with the else will be executed.

What does the following code print to the console?

if 88 > 100:
    print("cardio")

Nothing is printed.

The boolean condition (88 > 100) associated with the if statement evaluates to False, so the code block associated with the if statement is not executed. There are not elif or else statements, so nothing gets printed.

What does the following code print to the console?

hair_color = "blue"
if 3 > 2:
    if hair_color == "black":
        print("You rock!")
    else:
        print("Boring")

"Boring" is printed.

This is an example of a nested if statement. The boolean condition for the exterior if statement (3 > 2) evaluates to True so we enter the inner if statement.

The boolean condition for the inner if statement (hair_color == "black") evaluates to False, so the code block associated with the inner else statement is executed.

What does the following code print to the console?

if True:
    print(101)
else:
    print(202)

101 is printed to the console.

In previous examples, we've used boolean expressions that evaluate to True or False, but we can also use True and False directly.

What does the following code print to the console?

if False:
    print("Nissan")
elif True:
    print("Ford")
elif True:
    print("BMW")
else:
    print("Audi")

"Ford" is printed.

The first elif that is True will be executed when multiple elif statements are True.

What does the following code print to the console?

if 1:
    print("1 is truthy!")
else:
    print("???")

"1 is truthy!" is printed.

1 does not equal True, but it's considered True in a boolean context. Values that are considered True in a boolean context are called "truthy" and values that are considered False in a boolean context are called "falsy".

What does the following code print to the console?

if 0:
    print("huh?")
else:
    print("0 is falsy!")

0 is falsy is printed.

0 is considered falsy in a boolean context. There are other values that are considered falsy in a boolean context and we'll cover those in future quizzes.