Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Kombinera Villkor | Villkorssatser
Introduktion till Python

Svep för att visa menyn

book
Kombinera Villkor

Genom att bygga vidare på din förståelse av booleans, kommer vi nu att utforska hur man kombinerar flera villkor i Python. Denna färdighet gör att dina program kan fatta ännu mer nyanserade beslut genom att kontrollera flera kriterier samtidigt. Se när Alex kombinerar flera villkor för att fatta bättre beslut under livsmedelsbutikens verksamhet:

Förståelse av kombinerade villkor

I Python kan du kombinera villkor med logiska operatorer som and, or och not. Dessa operatorer låter dig skapa sammansatta villkor som utvärderar flera booleska uttryck.

  • and : Returnerar True om båda villkoren är True ;

  • or : Returnerar True om minst ett villkor är True ;

  • not : Returnerar True om villkoret är False (och vice versa).

Exempelapplikation

Låt oss kombinera villkor för att kontrollera om en vara är både en färskvara OCH har hög lagerstatus med hjälp av and-operatorn:

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

Nu, låt oss kombinera villkor för att kontrollera om en vara är antingen en säsongsvara ELLER om det är en helgdag vara med hjälp av or operatorn:

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

Slutligen, låt oss kombinera villkor för att kontrollera om en vara INTE behöver omprissättning med hjälp av not operatorn:

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
Uppgift

Swipe to start coding

Utvärdera om en artikel är rabatterad eller lågt i lager för att avgöra dess berättigande för kampanj.

  • Definiera en boolesk variabel movingProduct som är True om artikeln är antingen rabatterad eller lågt i lager, med hjälp av logiska operatorer.
  • Skapa en boolesk variabel promotion som är True om artikeln är inte rabatterad och tillräckligt i lager.
  • Skriv ut meddelandet: Is the item eligible for promotion? <promotion>.

Utmatningskrav

  • Skriv ut om artikeln är berättigad för kampanj: Is the item eligible for promotion? <promotion>.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 2
Vi beklagar att något gick fel. Vad hände?

Fråga AI

expand
ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

book
Kombinera Villkor

Genom att bygga vidare på din förståelse av booleans, kommer vi nu att utforska hur man kombinerar flera villkor i Python. Denna färdighet gör att dina program kan fatta ännu mer nyanserade beslut genom att kontrollera flera kriterier samtidigt. Se när Alex kombinerar flera villkor för att fatta bättre beslut under livsmedelsbutikens verksamhet:

Förståelse av kombinerade villkor

I Python kan du kombinera villkor med logiska operatorer som and, or och not. Dessa operatorer låter dig skapa sammansatta villkor som utvärderar flera booleska uttryck.

  • and : Returnerar True om båda villkoren är True ;

  • or : Returnerar True om minst ett villkor är True ;

  • not : Returnerar True om villkoret är False (och vice versa).

Exempelapplikation

Låt oss kombinera villkor för att kontrollera om en vara är både en färskvara OCH har hög lagerstatus med hjälp av and-operatorn:

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

Nu, låt oss kombinera villkor för att kontrollera om en vara är antingen en säsongsvara ELLER om det är en helgdag vara med hjälp av or operatorn:

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

Slutligen, låt oss kombinera villkor för att kontrollera om en vara INTE behöver omprissättning med hjälp av not operatorn:

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
Uppgift

Swipe to start coding

Utvärdera om en artikel är rabatterad eller lågt i lager för att avgöra dess berättigande för kampanj.

  • Definiera en boolesk variabel movingProduct som är True om artikeln är antingen rabatterad eller lågt i lager, med hjälp av logiska operatorer.
  • Skapa en boolesk variabel promotion som är True om artikeln är inte rabatterad och tillräckligt i lager.
  • Skriv ut meddelandet: Is the item eligible for promotion? <promotion>.

Utmatningskrav

  • Skriv ut om artikeln är berättigad för kampanj: Is the item eligible for promotion? <promotion>.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 2
Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Vi beklagar att något gick fel. Vad hände?
some-alt