Combinaison de Conditions
En s'appuyant sur votre compréhension des booléens, nous allons maintenant explorer comment combiner plusieurs conditions en Python. Cette compétence permet à vos programmes de prendre des décisions plus nuancées en vérifiant plusieurs critères simultanément. Observez comment Alex combine plusieurs conditions pour prendre de meilleures décisions lors des opérations en magasin d'alimentation :
Comprendre la combinaison de conditions
En Python, vous pouvez combiner des conditions à l'aide d'opérateurs logiques tels que and, or et not. Ces opérateurs permettent de créer des conditions composées qui évaluent plusieurs expressions booléennes.
and: renvoieTruesi les deux conditions sontTrue;or: renvoieTruesi au moins une condition estTrue;not: renvoieTruesi la condition estFalse(et inversement).
Exemple d'application
Combinons des conditions pour vérifier si un article est à la fois un produit périssable ET en stock élevé en utilisant l'opérateur and :
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)
À présent, combinons des conditions pour vérifier si un article est soit un article saisonnier OU s'il s'agit d'un article de fête à l'aide de l'opérateur or :
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)
Enfin, combinons des conditions pour vérifier si un article n’a PAS besoin d’être réévalué en utilisant l’opérateur not :
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)
Swipe to start coding
Évaluer si un article est en promotion ou faible en stock afin de déterminer son éligibilité à une promotion.
- Définir une variable booléenne
movingProductqui estTruesi l'article est soit en promotion, soit faible en stock, en utilisant des opérateurs logiques. - Créer une variable booléenne
promotionqui estTruesi l'article n'est pas en promotion et dispose d'un stock suffisant. - Afficher le message :
Is the item eligible for promotion? <promotion>.
Exigences de sortie
- Afficher si l'article est éligible à une promotion :
Is the item eligible for promotion? <promotion>.
Solution
Merci pour vos commentaires !
single
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Génial!
Completion taux amélioré à 2.17
Combinaison de Conditions
Glissez pour afficher le menu
En s'appuyant sur votre compréhension des booléens, nous allons maintenant explorer comment combiner plusieurs conditions en Python. Cette compétence permet à vos programmes de prendre des décisions plus nuancées en vérifiant plusieurs critères simultanément. Observez comment Alex combine plusieurs conditions pour prendre de meilleures décisions lors des opérations en magasin d'alimentation :
Comprendre la combinaison de conditions
En Python, vous pouvez combiner des conditions à l'aide d'opérateurs logiques tels que and, or et not. Ces opérateurs permettent de créer des conditions composées qui évaluent plusieurs expressions booléennes.
and: renvoieTruesi les deux conditions sontTrue;or: renvoieTruesi au moins une condition estTrue;not: renvoieTruesi la condition estFalse(et inversement).
Exemple d'application
Combinons des conditions pour vérifier si un article est à la fois un produit périssable ET en stock élevé en utilisant l'opérateur and :
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)
À présent, combinons des conditions pour vérifier si un article est soit un article saisonnier OU s'il s'agit d'un article de fête à l'aide de l'opérateur or :
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)
Enfin, combinons des conditions pour vérifier si un article n’a PAS besoin d’être réévalué en utilisant l’opérateur not :
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)
Swipe to start coding
Évaluer si un article est en promotion ou faible en stock afin de déterminer son éligibilité à une promotion.
- Définir une variable booléenne
movingProductqui estTruesi l'article est soit en promotion, soit faible en stock, en utilisant des opérateurs logiques. - Créer une variable booléenne
promotionqui estTruesi l'article n'est pas en promotion et dispose d'un stock suffisant. - Afficher le message :
Is the item eligible for promotion? <promotion>.
Exigences de sortie
- Afficher si l'article est éligible à une promotion :
Is the item eligible for promotion? <promotion>.
Solution
Merci pour vos commentaires !
single