Python's Conditional Statements

PythonPythonBeginner
Practice Now

Introduction

In this experiment, we will explore how to use if statements in Python. if statements allow you to execute code only if a certain condition is met. This is a fundamental concept in programming that allows you to control the flow of your program based on different conditions.

Achievements

  • if Statements
  • Ternary Conditional Operator

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/conditional_statements -.-> lab-82{{"`Python's Conditional Statements`"}} python/standard_libraries -.-> lab-82{{"`Python's Conditional Statements`"}} python/build_in_functions -.-> lab-82{{"`Python's Conditional Statements`"}} end

if Statements

Open up a new Python interpreter.

python3

Let's start by using a simple if statement to check if a number is positive or negative. Type the following code:

number = -5
if number < 0:
    print("The number is negative.")

Run the code and observe the output. The message "The number is negative." should be printed to the screen because the value of number is less than 0.

Else Clause

Now, let's add an else clause to our if statement. This will allow us to specify code to be executed if the condition in the if statement is not met. Modify the code as follows:

number = 5
if number < 0:
    print("The number is negative.")
else:
    print("The number is positive.")

Run the code and observe the output. This time, the message "The number is positive." should be printed to the screen because the value of number is not less than 0.

Elif Clause

We can also use an elif clause to check for multiple conditions. For example, let's check if a number is positive, negative, or zero. Modify the code as follows:

number = 0
if number < 0:
    print("The number is negative.")
elif number > 0:
    print("The number is positive.")
else:
    print("The number is zero.")

Run the code and observe the output. This time, the message "The number is zero." should be printed to the screen because the value of number is equal to 0.

Check for Conditions

You can also use if statements to check for conditions in other types, such as strings or lists. For example, let's check if a string is empty or not. Modify the code as follows:

string = "hello"
if not string:
    print("The string is empty.")
else:
    print("The string is not empty.")

Run the code and observe the output. The message "The string is not empty." should be printed to the screen because the value of string is not an empty string.

Ternary Conditional Operator

Python also has a ternary conditional operator, which allows you to specify a condition and two values, one for if the condition is true and one for if the condition is false. The syntax for the ternary conditional operator is:

value_if_true if condition else value_if_false

For example, let's use the ternary operator to assign a value to a variable based on whether a number is positive or negative:

number = 5
result = "positive" if number > 0 else "negative"
print(result)

Run the code and observe the output. The message "positive" should be printed to the screen because the value of number is greater than 0.

Note that the ternary operator can be used as a shorthand way to write an if-else statement, but it is not recommended to use it for more complex conditions or code blocks. In those cases, it is generally easier to read and understand an if-else statement.

Summary

In this lab, we learned how to use if statements in Python to control the flow of our program based on different conditions. We saw how to use if statements with an else clause to execute code if a condition is met or not met, and how to use elif clauses to check for multiple conditions. We also saw how to use if statements with other types such as strings and lists.

Other Python Tutorials you may like