PROMEMORIA — OPERATORI DI APPARTENENZA E CONFRONTI DI TIPO
# ============================================================
# ============================================================
# Questo promemoria riassume:
# - operatori di appartenenza: in / not in
# - verifica dei tipi di dato con type()
# - uso pratico nel contesto di un negozio di alimentari
# ============================================================
# 1. OPERATORI DI APPARTENENZA: in / not in
# ============================================================
# Gli operatori di appartenenza permettono di verificare
# se un valore è contenuto in un oggetto iterabile
# (stringhe, liste, tuple, ecc.).
# Restituiscono sempre un valore booleano: True o False
# ------------------------------------------------------------
# ESEMPIO BASE CON STRINGHE
# ------------------------------------------------------------
item_name = "Strawberries"
# Controllo se una sottostringa è presente
contains_straw = "Straw" in item_name
contains_berry = "Berry" in item_name
print("Is 'Straw' in 'Strawberries'?", contains_straw) # True
print("Is 'Berry' in 'Strawberries'?", contains_berry) # False (case-sensitive)
# ------------------------------------------------------------
# ESEMPIO REALISTICO: DESCRIZIONE PRODOTTO
# ------------------------------------------------------------
product_description = "Fresh organic milk from local farms, pasteurized."
# Verifica presenza parole chiave
is_organic = "organic" in product_description
is_local = "local" in product_description
is_raw = "raw" in product_description
print("Is the product organic?", is_organic)
print("Is the product local?", is_local)
print("Is the product raw?", is_raw)
# ============================================================
# 2. ATTENZIONE A MAIUSCOLE E MINUSCOLE
# ============================================================
# Python è case-sensitive:
# "Imported" è diverso da "imported"
description = "Fresh raw cheese imported from Italy"
contains_imported_upper = "Imported" in description
contains_imported_lower = "imported" in description
print("Contains 'Imported'?", contains_imported_upper) # False
print("Contains 'imported'?", contains_imported_lower) # True
# ============================================================
# 3. CONFRONTI DI TIPO CON type()
# ============================================================
# La funzione type() restituisce il tipo di una variabile.
# Spesso viene usata nei confronti per validare i dati.
# ------------------------------------------------------------
# ESEMPIO BASE
# ------------------------------------------------------------
price = 3.49
count = 20
name = "Almond Milk"
price_is_float = type(price) == float
count_is_int = type(count) == int
name_is_string = type(name) == str
print("Is price a float?", price_is_float)
print("Is count an integer?", count_is_int)
print("Is name a string?", name_is_string)
# ------------------------------------------------------------
# ESEMPIO CON ERRORE INTENZIONALE
# ------------------------------------------------------------
# Prezzo ricevuto come stringa (errore comune)
product_price = "3.49"
correct_price_type = type(product_price) == float
print("Is product_price a float?", correct_price_type) # False
# ============================================================
# 4. COMPITO RIASSUNTO (COME NELLA SCHEDA)
# ============================================================
# Dati di esempio
description = "Raw honey imported from local farms"
price = 4.99
count = 15
# Operatori di appartenenza
contains_raw = "raw" in description
contains_Imported = "Imported" in description
# Confronti di tipo
price_is_float = type(price) == float
count_is_int = type(count) == int
# Output richiesto
print("Contains 'raw':", contains_raw)
print("Contains 'Imported':", contains_Imported)
print("Is price a float?:", price_is_float)
print("Is count an integer?:", count_is_int)
# ============================================================
# 5. CONCETTI CHIAVE DA RICORDARE
# ============================================================
# ✔ in / not in → controllano la presenza di un elemento
# ✔ funzionano su stringhe e strutture iterabili
# ✔ sono case-sensitive
# ✔ type() serve per validare i dati
# ✔ i confronti di tipo restituiscono booleani