1. Introduzione agli operatori logici
# ============================================================
# ============================================================
# Gli operatori principali:
# and → entrambe le condizioni devono essere vere
# or → almeno una condizione deve essere vera
# not → inverte il valore di una condizione booleana
# ============================================================
# 2. Operatore AND — entrambe le condizioni devono essere vere
# ============================================================
# Esempio:
price = 5
stock = 20
if price < 10 and stock > 0:
print("The product is affordable AND available.")
# ============================================================
# 3. Operatore OR — almeno una condizione vera
# ============================================================
# Esempio:
age = 16
has_parent = True
if age >= 18 or has_parent:
print("User can enter.")
# ============================================================
# 4. Operatore NOT — inverte una condizione
# ============================================================
# Esempio:
is_expired = False
if not is_expired:
print("Product is still valid.")
# ============================================================
# 5. Combinazione di AND e OR
# ============================================================
# Uso di parentesi per chiarezza (importantissimo!)
# Esempio:
price = 3.5
stock = 50
category = "Produce"
if (price < 5 and stock > 20) or category == "Produce":
print("Special discount available!")
# ============================================================
# 6. Precedenza degli operatori
# ============================================================
# NOT ha priorità più alta
# AND viene dopo
# OR è l’ultimo
# Esempio:
x = True or False and False
print(x) # True, perché AND viene prima
# ============================================================
# 7. Condizioni multiple con range
# ============================================================
# Controllare se un valore è dentro un intervallo con AND
# Esempio:
temp = 22
if temp > 18 and temp < 30:
print("Comfortable temperature.")
# ============================================================
# 8. Controllare range usando una forma più leggibile
# ============================================================
# Esempio:
if 18 < temp < 30:
print("Same result, more Pythonic!")
# ============================================================
# 9. Combinazione di condizioni su stringhe
# ============================================================
# Esempio:
product = "milk"
if product.startswith("m") and len(product) < 5:
print("Product qualifies.")
# ============================================================
# 10. Combinazione di condizioni con liste
# ============================================================
# Esempio:
cart = ["milk", "bread", "eggs"]
if "milk" in cart and "eggs" in cart:
print("Breakfast items complete!")
# ============================================================
# 11. Condizioni su dizionari
# ============================================================
# Esempio:
stock = {"milk": 10, "eggs": 0}
if stock["milk"] > 0 and stock["eggs"] == 0:
print("Milk available but eggs out of stock.")
# ============================================================
# 12. Usare OR per gestire valori di fallback
# ============================================================
# Esempio:
user_input = ""
username = user_input or "Guest"
print(username)
# ============================================================
# 13. Combinazione di NOT con AND/OR
# ============================================================
# Esempio:
available = False
price = 4.0
if not available or price > 10:
print("Cannot purchase.")
# ============================================================
# 14. Condizioni annidate vs condizioni combinate
# ============================================================
# Esempio annidato:
if price < 5:
if stock > 10:
print("OK")
Stessa logica, molto più pulita:
if price < 5 and stock > 10:
print("OK")
# ============================================================
# 15. Condizioni con più operatori logici (caso reale)
# ============================================================
# Esempio:
age = 25
has_id = True
banned = False
if has_id and (age >= 18) and not banned:
print("Access granted.")