Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Identità vs Uguaglianza | Booleani e Confronti
Tipi di dati in Python

bookIdentità vs Uguaglianza

Scopri la differenza tra identità e uguaglianza in Python. L'identità verifica se due variabili si riferiscono allo stesso oggetto in memoria, mentre l'uguaglianza confronta se due oggetti possiedono lo stesso valore. Comprendere questa distinzione è fondamentale per scrivere condizionali, validazioni e logiche di elaborazione dati accurate.

Che cos'è is?

  • is verifica l'identità dell'oggetto, ovvero se due variabili puntano allo stesso oggetto in memoria;
  • == verifica l'uguaglianza dei valori, ovvero se due oggetti hanno gli stessi contenuti.
12345678
# Comparing two shopping carts in an online store cart_today = ["milk", "bread"] cart_yesterday = ["milk", "bread"] shared_cart = cart_today print(cart_today == cart_yesterday) # True → same items print(cart_today is cart_yesterday) # False → two separate cart objects print(cart_today is shared_cart) # True → both refer to the same cart
copy

Quando usare is e quando evitarlo

  • Utilizzare is per il confronto con singleton come None;
12345
# Checking if the user has entered their phone number user_phone = None if user_phone is None: print("No phone number provided yet")
copy
  • is not è il controllo di identità negato;
12345
# Checking if the user's age is stored in the system user_age = 64 if user_age is not None: print(f"User age is recorded: {user_age}")
copy
  • Evitare l'uso di is per verificare l'uguaglianza tra numeri o stringhe. A causa del caching/interning interno, l'identità può sembrare "funzionare" a volte, ma non è affidabile tra diverse esecuzioni e ambienti; utilizzare invece ==;
123456789101112
# Comparing user IDs and usernames in a system user_id_a = 256 user_id_b = 256 print(user_id_a == user_id_b) # True → same user ID value print(user_id_a is user_id_b) # May appear True, but identity check is unreliable for numbers username_a = "hello" username_b = "he" + "llo" print(username_a == username_b) # True → same text print(username_a is username_b) # Avoid using 'is' for string comparison (implementation detail)
copy
  • Per i booleani, preferire i controlli di veridicità.
12345
# Checking if dark mode is enabled in user settings dark_mode_enabled = True if dark_mode_enabled: # clearer than: if dark_mode_enabled is True print("Dark mode is ON")
copy

1. Completa gli spazi con is oppure ==:

2. Qual è il modo corretto per verificare l'assenza di "valore"?

3. Quale affermazione è raccomandata?

question-icon

Completa gli spazi con is oppure ==:

Use to check if two variables point to the same object.
Use
to check if two values have the same contents.

Click or drag`n`drop items and fill in the blanks

question mark

Qual è il modo corretto per verificare l'assenza di "valore"?

Select the correct answer

question mark

Quale affermazione è raccomandata?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 3

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you explain more about when to use `is` versus `==` in real-world scenarios?

What are some common mistakes people make with `is` and `==` in Python?

Could you provide more examples of identity and equality checks?

Awesome!

Completion rate improved to 3.45

bookIdentità vs Uguaglianza

Scorri per mostrare il menu

Scopri la differenza tra identità e uguaglianza in Python. L'identità verifica se due variabili si riferiscono allo stesso oggetto in memoria, mentre l'uguaglianza confronta se due oggetti possiedono lo stesso valore. Comprendere questa distinzione è fondamentale per scrivere condizionali, validazioni e logiche di elaborazione dati accurate.

Che cos'è is?

  • is verifica l'identità dell'oggetto, ovvero se due variabili puntano allo stesso oggetto in memoria;
  • == verifica l'uguaglianza dei valori, ovvero se due oggetti hanno gli stessi contenuti.
12345678
# Comparing two shopping carts in an online store cart_today = ["milk", "bread"] cart_yesterday = ["milk", "bread"] shared_cart = cart_today print(cart_today == cart_yesterday) # True → same items print(cart_today is cart_yesterday) # False → two separate cart objects print(cart_today is shared_cart) # True → both refer to the same cart
copy

Quando usare is e quando evitarlo

  • Utilizzare is per il confronto con singleton come None;
12345
# Checking if the user has entered their phone number user_phone = None if user_phone is None: print("No phone number provided yet")
copy
  • is not è il controllo di identità negato;
12345
# Checking if the user's age is stored in the system user_age = 64 if user_age is not None: print(f"User age is recorded: {user_age}")
copy
  • Evitare l'uso di is per verificare l'uguaglianza tra numeri o stringhe. A causa del caching/interning interno, l'identità può sembrare "funzionare" a volte, ma non è affidabile tra diverse esecuzioni e ambienti; utilizzare invece ==;
123456789101112
# Comparing user IDs and usernames in a system user_id_a = 256 user_id_b = 256 print(user_id_a == user_id_b) # True → same user ID value print(user_id_a is user_id_b) # May appear True, but identity check is unreliable for numbers username_a = "hello" username_b = "he" + "llo" print(username_a == username_b) # True → same text print(username_a is username_b) # Avoid using 'is' for string comparison (implementation detail)
copy
  • Per i booleani, preferire i controlli di veridicità.
12345
# Checking if dark mode is enabled in user settings dark_mode_enabled = True if dark_mode_enabled: # clearer than: if dark_mode_enabled is True print("Dark mode is ON")
copy

1. Completa gli spazi con is oppure ==:

2. Qual è il modo corretto per verificare l'assenza di "valore"?

3. Quale affermazione è raccomandata?

question-icon

Completa gli spazi con is oppure ==:

Use to check if two variables point to the same object.
Use
to check if two values have the same contents.

Click or drag`n`drop items and fill in the blanks

question mark

Qual è il modo corretto per verificare l'assenza di "valore"?

Select the correct answer

question mark

Quale affermazione è raccomandata?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 3
some-alt