Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Combinación de Condiciones | Sentencias Condicionales
Introducción a Python

Desliza para mostrar el menú

book
Combinación de Condiciones

Basándonos en tu comprensión de los booleanos, ahora exploraremos cómo combinar múltiples condiciones en Python. Esta habilidad permite que tus programas tomen decisiones aún más matizadas al verificar varios criterios simultáneamente. Observa cómo Alex combina múltiples condiciones para tomar mejores decisiones durante las operaciones en la tienda de comestibles:

Comprensión de condiciones combinadas

En Python, puedes combinar condiciones utilizando operadores lógicos como and, or y not. Estos operadores te permiten crear condiciones compuestas que evalúan múltiples expresiones booleanas.

  • and: Devuelve True si ambas condiciones son True;

  • or: Devuelve True si al menos una condición es True;

  • not: Devuelve True si la condición es False (y viceversa).

Ejemplo de aplicación

Combinemos condiciones para verificar si un artículo es un producto perecedero Y tiene un alto stock utilizando el operador 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)
copy

Ahora, combine condiciones para verificar si un artículo es de temporada O si es un artículo festivo utilizando el operador 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)
copy

Finalmente, combinemos condiciones para verificar si un artículo NO necesita cambio de precio utilizando el operador 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)
copy
Tarea

Swipe to start coding

Evaluar si un artículo está en descuento o con bajo stock para determinar su elegibilidad para promoción.

  • Definir una variable booleana movingProduct que sea True si el artículo está en descuento o con bajo stock, utilizando operadores lógicos.
  • Crear una variable booleana promotion que sea True si el artículo no está en descuento y tiene suficiente stock.
  • Imprimir el mensaje: Is the item eligible for promotion? <promotion>.

Requisitos de salida

  • Imprimir si el artículo es elegible para promoción: Is the item eligible for promotion? <promotion>.

Solución

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 2

Pregunte a AI

expand
ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

book
Combinación de Condiciones

Basándonos en tu comprensión de los booleanos, ahora exploraremos cómo combinar múltiples condiciones en Python. Esta habilidad permite que tus programas tomen decisiones aún más matizadas al verificar varios criterios simultáneamente. Observa cómo Alex combina múltiples condiciones para tomar mejores decisiones durante las operaciones en la tienda de comestibles:

Comprensión de condiciones combinadas

En Python, puedes combinar condiciones utilizando operadores lógicos como and, or y not. Estos operadores te permiten crear condiciones compuestas que evalúan múltiples expresiones booleanas.

  • and: Devuelve True si ambas condiciones son True;

  • or: Devuelve True si al menos una condición es True;

  • not: Devuelve True si la condición es False (y viceversa).

Ejemplo de aplicación

Combinemos condiciones para verificar si un artículo es un producto perecedero Y tiene un alto stock utilizando el operador 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)
copy

Ahora, combine condiciones para verificar si un artículo es de temporada O si es un artículo festivo utilizando el operador 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)
copy

Finalmente, combinemos condiciones para verificar si un artículo NO necesita cambio de precio utilizando el operador 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)
copy
Tarea

Swipe to start coding

Evaluar si un artículo está en descuento o con bajo stock para determinar su elegibilidad para promoción.

  • Definir una variable booleana movingProduct que sea True si el artículo está en descuento o con bajo stock, utilizando operadores lógicos.
  • Crear una variable booleana promotion que sea True si el artículo no está en descuento y tiene suficiente stock.
  • Imprimir el mensaje: Is the item eligible for promotion? <promotion>.

Requisitos de salida

  • Imprimir si el artículo es elegible para promoción: Is the item eligible for promotion? <promotion>.

Solución

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 2
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Lamentamos que algo salió mal. ¿Qué pasó?
some-alt