Conditionals take an expression, which is code that evaluates to determine a value, and checks if it is
False. If it’s
True, we can tell our program to do one thing — we can even account for
False to do another.
As we write more complex programs, conditionals allow us to address multiple scenarios and make our programs more robust.
if statement is used to determine the execution of code based on the evaluation of a Boolean expression.
- If the
ifstatement expression evaluates to
True, then the indented code following the statement is executed.
- If the expression evaluates to
Falsethen the indented code following the if statement is skipped and the program executes the next line of code which is indented at the same level as the if statement.
test_value = 100if test_value > 1:print("This code is executed!")if test_value > 1000:print("This code is NOT executed!")print("Program continues at this point.")
else statement provides alternate code to execute if the expression in an if statement evaluates to
The indented code for the
if statement is executed if the expression evaluates to
True. The indented code immediately following the
else is executed if and only if the expression evaluates to
To mark the end of the else block, the code must be unindented to the same level as the starting if line.
test_grade = 61if test_grade > 60:print("You passed.")else:print("You failed.")# Output: You passed.
elif statement allows for continued checks to be performed after an initial
if statement. An
elif statement differs from the else statement because another expression is provided to be checked, just as with the initial
If the expression is
True, the indented code following the
elif is executed. If the expression evaluates to False, the code can continue to an optional
Multiple elif statements can be used following an initial
if to perform a series of checks. Once an
elif expression evaluates to
True, no further
elif statements are executed.
pet_type = "fish"if pet_type == "dog":print("You have a dog.")elif pet_type == "cat":print("You have a cat.")elif pet_type == "fish":# This is performedprint("You have a fish")else:print("Not sure!")
What do you think will happen if
pH is changed to 7?
- Anonymous contributorAnonymous contributor3077 total contributions
- Anonymous contributor