Lesson 1. Intro to Conditional Statements


Conditional Statements - Earth analytics bootcamp course module

Welcome to the first lesson in the Conditional Statements module. This tutorial walks you through implementing another key strategy for writing DRY (i.e. Do Not Repeat Yourself) code in Python: conditional statements.

In this lesson, you will learn about the structure of conditional statements in Python and how you can use them to write DRY code by only executing code when certain conditions have been met.

Learning Objectives

After completing this lesson, you will be able to:

  • Describe the syntax for conditional statements in Python
  • Explain how conditional statements can be used to write DRY code

What You Need

Be sure that you have completed the lesson on Intro to DRY Code.

Conditional Statements

In the lesson introducing DRY code, you learned that conditional statements provide you with the ability to control when or how code is executed.

This can be very useful for checking whether a certain condition exists before the code begins to execute, as you may want to only execute certain code lines when certain conditions are met.

For example, conditional statements can be used to check that a certain variable or file exists before code is executed, or to execute more code if some criteria is met, such as a calculation resulting in a specific value.

Structure of Conditional Statements

The structure of a conditional statement is defined through the use of if and else.

if condition:
    print("condition is true")
else:
    print("condition not true")

When the condition following the if is met, then a certain code will execute. When that condition is not met, then the code following the else will execute.

Review these simple examples to see how the if and else are used to control which code lines are executed.

# create a variable x with the value of 0
x = 0

# compare the value of x to 10 and print a message depending on the outcome of the comparison
if x < 10:
    print("x has a value of", x, "which is less than 10.")
    
else:
    print("x has a value of", x, "which is not less than 10.")    
x has a value of 0 which is less than 10.
# create a variable x with the value of 100
x = 100

# compare the value of x to 10 and print a message depending on the outcome of the comparison
if x < 10:
    print("x has a value of", x, "which is less than 10.")
    
else:
    print("x has a value of", x, "which is not less than 10.") 
x has a value of 100 which is not less than 10.

You can also write conditional statements to identify keywords within a text string, such as the name of a variable.

# check if the text string "precip" in contained within the text string "avg_monthly_precip"
if "precip" in "avg_monthly_precip":
    print("This file contains the keyword: precip.")
    
else:
    print("This file does not contain the keyword: precip")
This file contains the keyword: precip.
# check if the text string "precip" in contained within the text string "avg_monthly_temp"
if "precip" in "avg_monthly_temp":
    print("This file contains the keyword: precip.")
    
else:
    print("This file does not contain the keyword: precip.")
This file does not contain the keyword: precip.

Optional Challenge 1

Test your Python skills to:

  1. Write a conditional statement that will print a message depending on whether following condition is met: the value of x is equal to 35.
    • Set x equal to 25, and print a message stating whether x is equal to 35.
    • Note that the comparison operator for equal to is ==.
  2. How does the the comparison operator for equal to differ from using the = to assign values to variable names?
x has a value of 25 which is not equal to 35.

Optional Challenge 2

Test your Python skills to:

  1. Write a conditional statement that will add a value of 15 to a variable x if the following condition is met: the value of x is NOT equal to 35.
    • Set x equal to 25 and use the assignment operator to add 15 when x is not equal to 35.
    • Note that the comparison operator for not equal to is !=.
    • For both the if and else, print the final value of x.
40

Leave a Comment