Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Yhdistäminen Ehtoja | Ehtolauseet
Johdanto Python-Ohjelmointiin

Pyyhkäise näyttääksesi valikon

book
Yhdistäminen Ehtoja

Rakentaen ymmärryksesi booleaneista, tutkimme nyt, kuinka yhdistää useita ehtoja Pythonissa. Tämä taito antaa ohjelmillesi mahdollisuuden tehdä vielä hienovaraisempia päätöksiä tarkistamalla useita kriteerejä samanaikaisesti. Katso, kuinka Alex yhdistää useita ehtoja tehdäkseen parempia päätöksiä ruokakaupan toiminnan aikana:

Yhdistettyjen ehtojen ymmärtäminen

Pythonissa voit yhdistää ehtoja käyttämällä loogisia operaattoreita kuten and, or ja not. Nämä operaattorit mahdollistavat yhdistettyjen ehtojen luomisen, jotka arvioivat useita Boolen lausekkeita.

  • and : Palauttaa True , jos molemmat ehdot ovat True ;

  • or : Palauttaa True , jos vähintään yksi ehto on True ;

  • not : Palauttaa True , jos ehto on False (ja päinvastoin).

Esimerkki sovelluksesta

Yhdistetään ehtoja tarkistaaksemme, onko tuote sekä helposti pilaantuva JA varastossa paljon käyttäen and-operaattoria:

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

Nyt yhdistetään ehdot tarkistaaksemme, onko tuote joko kausituote TAI onko se juhlatuote käyttämällä or-operaattoria:

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

Lopuksi yhdistetään ehdot tarkistaaksemme, tarvitseeko tuote EI uudelleenhinnoittelua käyttämällä not-operaattoria:

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
Tehtävä

Swipe to start coding

Arvioi, onko tuote alennuksessa tai vähissä varastossa määrittääksesi sen kelpoisuuden kampanjaan.

  • Määrittele totuusarvomuuttuja movingProduct, joka on True, jos tuote on joko alennuksessa tai vähissä varastossa, käyttämällä loogisia operaattoreita.
  • Luo totuusarvomuuttuja promotion, joka on True, jos tuote ei ole alennuksessa ja riittävästi varastossa.
  • Tulosta viesti: Is the item eligible for promotion? <promotion>.

Tulostusvaatimukset

  • Tulosta, onko tuote kelvollinen kampanjaan: Is the item eligible for promotion? <promotion>.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 2
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?

Kysy tekoälyä

expand
ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

book
Yhdistäminen Ehtoja

Rakentaen ymmärryksesi booleaneista, tutkimme nyt, kuinka yhdistää useita ehtoja Pythonissa. Tämä taito antaa ohjelmillesi mahdollisuuden tehdä vielä hienovaraisempia päätöksiä tarkistamalla useita kriteerejä samanaikaisesti. Katso, kuinka Alex yhdistää useita ehtoja tehdäkseen parempia päätöksiä ruokakaupan toiminnan aikana:

Yhdistettyjen ehtojen ymmärtäminen

Pythonissa voit yhdistää ehtoja käyttämällä loogisia operaattoreita kuten and, or ja not. Nämä operaattorit mahdollistavat yhdistettyjen ehtojen luomisen, jotka arvioivat useita Boolen lausekkeita.

  • and : Palauttaa True , jos molemmat ehdot ovat True ;

  • or : Palauttaa True , jos vähintään yksi ehto on True ;

  • not : Palauttaa True , jos ehto on False (ja päinvastoin).

Esimerkki sovelluksesta

Yhdistetään ehtoja tarkistaaksemme, onko tuote sekä helposti pilaantuva JA varastossa paljon käyttäen and-operaattoria:

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

Nyt yhdistetään ehdot tarkistaaksemme, onko tuote joko kausituote TAI onko se juhlatuote käyttämällä or-operaattoria:

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

Lopuksi yhdistetään ehdot tarkistaaksemme, tarvitseeko tuote EI uudelleenhinnoittelua käyttämällä not-operaattoria:

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
Tehtävä

Swipe to start coding

Arvioi, onko tuote alennuksessa tai vähissä varastossa määrittääksesi sen kelpoisuuden kampanjaan.

  • Määrittele totuusarvomuuttuja movingProduct, joka on True, jos tuote on joko alennuksessa tai vähissä varastossa, käyttämällä loogisia operaattoreita.
  • Luo totuusarvomuuttuja promotion, joka on True, jos tuote ei ole alennuksessa ja riittävästi varastossa.
  • Tulosta viesti: Is the item eligible for promotion? <promotion>.

Tulostusvaatimukset

  • Tulosta, onko tuote kelvollinen kampanjaan: Is the item eligible for promotion? <promotion>.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 2
Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt