Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Combining Conditions | Conditional Statements
Introduction to Python Video Course
course content

Course Content

Introduction to Python Video Course

Introduction to Python Video Course

1. Getting Started
2. Variables and Types
3. Conditional Statements
4. Other Data Types
5. Loops
6. Functions

Combining Conditions

Building on your understanding of booleans, we will now explore how to combine multiple conditions in Python. This skill allows your programs to make even more nuanced decisions by checking multiple criteria simultaneously. Watch as Alex combines multiple conditions to make better decisions during grocery store operations:

Understanding Combined Conditions

In Python, you can combine conditions using logical operators such as and, or, and not. These operators allow you to create compound conditions that evaluate multiple Boolean expressions.

  • and: Returns True if both conditions are True;
  • or: Returns True if at least one condition is True;
  • not: Returns True if the condition is False (and vice versa).

Example Application

Let's combine conditions to check if an item is both a perishable good AND high in stock using the and operator:

12345678910111213
# Define the perishable and stock status conditions is_perishable = True item_quantity = 110 perishable_highStockRisk = 100 # Using the (and) operator to combine two conditions # The first condition (`is_perishable`) checks if the item is perishable # The second condition (`item_quantity >= perishable_highStockRisk`) checks if the item is high in stock # The `consider_discount` variable will become `True` only if both conditions are `True` consider_discount = is_perishable and (item_quantity >= perishable_highStockRisk) # Print the result print("Is the item perishable and high in stock?", consider_discount)
copy

Now, let's combine conditions to check if an item is either a seasonal item OR if it is a holiday item using the or operator:

12345678910
# Define the seasonal and holiday status conditions seasonal_item = False holiday_item = True # Combine the conditions to check if the item is seasonal or discounted # (`temporary_stock`) will become `True` if either condition `seasonal_item` OR `holiday_item` is `True` temporary_stock = seasonal_item or holiday_item # Print the result print("Is this a seasonal or holiday item?", temporary_stock)
copy

Finally, let's combine conditions to check if an item does NOT need repricing using the not operator:

12345678
# Define the item status condition is_perishable = True # Use the `not` operator to check if the item is NOT perishable long_shelf_life = not is_perishable # Print the result print("Does the item need to be sold quickly?", long_shelf_life)
copy

Task

Now it's your turn to practice combining conditions. In this challenge, you will check if an item is eligible for a promotion based on its stock status and whether or not it has been discounted already.

Combine the conditions to check if an item is either discounted or low in stock, and then use the result to check if the item is not eligible for promotion. Fill in the appropriate operators to complete the code.

  1. Create a boolean variable that becomes True if the item is either discounted or low in stock.
  2. Use the result of movingProduct to check if the item is not eligible for promotion, implying that only non-discounted and sufficiently stocked items are eligible for the promotion.
  3. Output whether the item is eligible for a promotion based on the evaluated conditions using the promotion variable.

Task

Now it's your turn to practice combining conditions. In this challenge, you will check if an item is eligible for a promotion based on its stock status and whether or not it has been discounted already.

Combine the conditions to check if an item is either discounted or low in stock, and then use the result to check if the item is not eligible for promotion. Fill in the appropriate operators to complete the code.

  1. Create a boolean variable that becomes True if the item is either discounted or low in stock.
  2. Use the result of movingProduct to check if the item is not eligible for promotion, implying that only non-discounted and sufficiently stocked items are eligible for the promotion.
  3. Output whether the item is eligible for a promotion based on the evaluated conditions using the promotion variable.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Section 3. Chapter 2
toggle bottom row

Combining Conditions

Building on your understanding of booleans, we will now explore how to combine multiple conditions in Python. This skill allows your programs to make even more nuanced decisions by checking multiple criteria simultaneously. Watch as Alex combines multiple conditions to make better decisions during grocery store operations:

Understanding Combined Conditions

In Python, you can combine conditions using logical operators such as and, or, and not. These operators allow you to create compound conditions that evaluate multiple Boolean expressions.

  • and: Returns True if both conditions are True;
  • or: Returns True if at least one condition is True;
  • not: Returns True if the condition is False (and vice versa).

Example Application

Let's combine conditions to check if an item is both a perishable good AND high in stock using the and operator:

12345678910111213
# Define the perishable and stock status conditions is_perishable = True item_quantity = 110 perishable_highStockRisk = 100 # Using the (and) operator to combine two conditions # The first condition (`is_perishable`) checks if the item is perishable # The second condition (`item_quantity >= perishable_highStockRisk`) checks if the item is high in stock # The `consider_discount` variable will become `True` only if both conditions are `True` consider_discount = is_perishable and (item_quantity >= perishable_highStockRisk) # Print the result print("Is the item perishable and high in stock?", consider_discount)
copy

Now, let's combine conditions to check if an item is either a seasonal item OR if it is a holiday item using the or operator:

12345678910
# Define the seasonal and holiday status conditions seasonal_item = False holiday_item = True # Combine the conditions to check if the item is seasonal or discounted # (`temporary_stock`) will become `True` if either condition `seasonal_item` OR `holiday_item` is `True` temporary_stock = seasonal_item or holiday_item # Print the result print("Is this a seasonal or holiday item?", temporary_stock)
copy

Finally, let's combine conditions to check if an item does NOT need repricing using the not operator:

12345678
# Define the item status condition is_perishable = True # Use the `not` operator to check if the item is NOT perishable long_shelf_life = not is_perishable # Print the result print("Does the item need to be sold quickly?", long_shelf_life)
copy

Task

Now it's your turn to practice combining conditions. In this challenge, you will check if an item is eligible for a promotion based on its stock status and whether or not it has been discounted already.

Combine the conditions to check if an item is either discounted or low in stock, and then use the result to check if the item is not eligible for promotion. Fill in the appropriate operators to complete the code.

  1. Create a boolean variable that becomes True if the item is either discounted or low in stock.
  2. Use the result of movingProduct to check if the item is not eligible for promotion, implying that only non-discounted and sufficiently stocked items are eligible for the promotion.
  3. Output whether the item is eligible for a promotion based on the evaluated conditions using the promotion variable.

Task

Now it's your turn to practice combining conditions. In this challenge, you will check if an item is eligible for a promotion based on its stock status and whether or not it has been discounted already.

Combine the conditions to check if an item is either discounted or low in stock, and then use the result to check if the item is not eligible for promotion. Fill in the appropriate operators to complete the code.

  1. Create a boolean variable that becomes True if the item is either discounted or low in stock.
  2. Use the result of movingProduct to check if the item is not eligible for promotion, implying that only non-discounted and sufficiently stocked items are eligible for the promotion.
  3. Output whether the item is eligible for a promotion based on the evaluated conditions using the promotion variable.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Section 3. Chapter 2
toggle bottom row

Combining Conditions

Building on your understanding of booleans, we will now explore how to combine multiple conditions in Python. This skill allows your programs to make even more nuanced decisions by checking multiple criteria simultaneously. Watch as Alex combines multiple conditions to make better decisions during grocery store operations:

Understanding Combined Conditions

In Python, you can combine conditions using logical operators such as and, or, and not. These operators allow you to create compound conditions that evaluate multiple Boolean expressions.

  • and: Returns True if both conditions are True;
  • or: Returns True if at least one condition is True;
  • not: Returns True if the condition is False (and vice versa).

Example Application

Let's combine conditions to check if an item is both a perishable good AND high in stock using the and operator:

12345678910111213
# Define the perishable and stock status conditions is_perishable = True item_quantity = 110 perishable_highStockRisk = 100 # Using the (and) operator to combine two conditions # The first condition (`is_perishable`) checks if the item is perishable # The second condition (`item_quantity >= perishable_highStockRisk`) checks if the item is high in stock # The `consider_discount` variable will become `True` only if both conditions are `True` consider_discount = is_perishable and (item_quantity >= perishable_highStockRisk) # Print the result print("Is the item perishable and high in stock?", consider_discount)
copy

Now, let's combine conditions to check if an item is either a seasonal item OR if it is a holiday item using the or operator:

12345678910
# Define the seasonal and holiday status conditions seasonal_item = False holiday_item = True # Combine the conditions to check if the item is seasonal or discounted # (`temporary_stock`) will become `True` if either condition `seasonal_item` OR `holiday_item` is `True` temporary_stock = seasonal_item or holiday_item # Print the result print("Is this a seasonal or holiday item?", temporary_stock)
copy

Finally, let's combine conditions to check if an item does NOT need repricing using the not operator:

12345678
# Define the item status condition is_perishable = True # Use the `not` operator to check if the item is NOT perishable long_shelf_life = not is_perishable # Print the result print("Does the item need to be sold quickly?", long_shelf_life)
copy

Task

Now it's your turn to practice combining conditions. In this challenge, you will check if an item is eligible for a promotion based on its stock status and whether or not it has been discounted already.

Combine the conditions to check if an item is either discounted or low in stock, and then use the result to check if the item is not eligible for promotion. Fill in the appropriate operators to complete the code.

  1. Create a boolean variable that becomes True if the item is either discounted or low in stock.
  2. Use the result of movingProduct to check if the item is not eligible for promotion, implying that only non-discounted and sufficiently stocked items are eligible for the promotion.
  3. Output whether the item is eligible for a promotion based on the evaluated conditions using the promotion variable.

Task

Now it's your turn to practice combining conditions. In this challenge, you will check if an item is eligible for a promotion based on its stock status and whether or not it has been discounted already.

Combine the conditions to check if an item is either discounted or low in stock, and then use the result to check if the item is not eligible for promotion. Fill in the appropriate operators to complete the code.

  1. Create a boolean variable that becomes True if the item is either discounted or low in stock.
  2. Use the result of movingProduct to check if the item is not eligible for promotion, implying that only non-discounted and sufficiently stocked items are eligible for the promotion.
  3. Output whether the item is eligible for a promotion based on the evaluated conditions using the promotion variable.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Building on your understanding of booleans, we will now explore how to combine multiple conditions in Python. This skill allows your programs to make even more nuanced decisions by checking multiple criteria simultaneously. Watch as Alex combines multiple conditions to make better decisions during grocery store operations:

Understanding Combined Conditions

In Python, you can combine conditions using logical operators such as and, or, and not. These operators allow you to create compound conditions that evaluate multiple Boolean expressions.

  • and: Returns True if both conditions are True;
  • or: Returns True if at least one condition is True;
  • not: Returns True if the condition is False (and vice versa).

Example Application

Let's combine conditions to check if an item is both a perishable good AND high in stock using the and operator:

12345678910111213
# Define the perishable and stock status conditions is_perishable = True item_quantity = 110 perishable_highStockRisk = 100 # Using the (and) operator to combine two conditions # The first condition (`is_perishable`) checks if the item is perishable # The second condition (`item_quantity >= perishable_highStockRisk`) checks if the item is high in stock # The `consider_discount` variable will become `True` only if both conditions are `True` consider_discount = is_perishable and (item_quantity >= perishable_highStockRisk) # Print the result print("Is the item perishable and high in stock?", consider_discount)
copy

Now, let's combine conditions to check if an item is either a seasonal item OR if it is a holiday item using the or operator:

12345678910
# Define the seasonal and holiday status conditions seasonal_item = False holiday_item = True # Combine the conditions to check if the item is seasonal or discounted # (`temporary_stock`) will become `True` if either condition `seasonal_item` OR `holiday_item` is `True` temporary_stock = seasonal_item or holiday_item # Print the result print("Is this a seasonal or holiday item?", temporary_stock)
copy

Finally, let's combine conditions to check if an item does NOT need repricing using the not operator:

12345678
# Define the item status condition is_perishable = True # Use the `not` operator to check if the item is NOT perishable long_shelf_life = not is_perishable # Print the result print("Does the item need to be sold quickly?", long_shelf_life)
copy

Task

Now it's your turn to practice combining conditions. In this challenge, you will check if an item is eligible for a promotion based on its stock status and whether or not it has been discounted already.

Combine the conditions to check if an item is either discounted or low in stock, and then use the result to check if the item is not eligible for promotion. Fill in the appropriate operators to complete the code.

  1. Create a boolean variable that becomes True if the item is either discounted or low in stock.
  2. Use the result of movingProduct to check if the item is not eligible for promotion, implying that only non-discounted and sufficiently stocked items are eligible for the promotion.
  3. Output whether the item is eligible for a promotion based on the evaluated conditions using the promotion variable.

Switch to desktop for real-world practiceContinue from where you are using one of the options below
Section 3. Chapter 2
Switch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt