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

bookOperatori di Confronto

I confronti permettono al tuo programma di porre domande sì/no sui valori:

  • Sono uguali?
  • Questo è maggiore?
  • Questo numero rientra in un intervallo?

Un confronto restituisce un valore booleano (True o False) ed è la base della logica if/while.

Fondamentali

Python fornisce sei operatori di confronto (==, !=, <, <=, >, >=) per verificare uguaglianza e ordinamento tra valori; ogni confronto restituisce True o False.

Uguaglianza ==

Verifica se due valori sono uguali.

1234567
saved_pin = 1234 entered_pin = 1234 print(saved_pin == entered_pin) # True → user entered the correct PIN stored_email = "support@codefinity.com" input_email = "Support@codefinity.com" print(stored_email == input_email) # False → case matters in string comparison
copy
Note
Nota

= assegna un valore a una variabile, mentre == confronta due valori.

Disuguaglianza !=

Verifica se due valori sono diversi.

1234567
user_id_1 = 105 user_id_2 = 203 print(user_id_1 != user_id_2) # True → users have different IDs username_1 = "alex" username_2 = "alex" print(username_1 != username_2) # False → usernames match
copy

Maggiore di >

Vero se il valore a sinistra è strettamente maggiore di quello a destra.

123456789
# Comparing delivery times in minutes estimated_time = 7 actual_time = 9 print(estimated_time > actual_time) # False → delivery took longer than expected # Comparing two product ratings rating_product_a = 12 rating_product_b = 3 print(rating_product_a > rating_product_b) # True → product A has a higher rating
copy

Minore di <

Vero se il valore a sinistra è strettamente inferiore a quello a destra.

123456789
# Comparing user's age with the minimum required age user_age = 17 min_age = 18 print(user_age < min_age) # True → user is too young to access the service # Comparing names alphabetically first_name = "Alice" second_name = "Bob" print(first_name < second_name) # True → "Alice" comes before "Bob" alphabetically
copy

Maggiore o uguale >=

Vero se il valore a sinistra è maggiore o uguale a quello a destra.

123456789
# Checking if a student reached the passing score student_score = 7 passing_score = 7 print(student_score >= passing_score) # True → student passed the test # Comparing two package weights before shipping package_weight = 4 min_weight_required = 9 print(package_weight >= min_weight_required) # False → package is too light
copy

Minore o uguale <=

Restituisce True se il valore a sinistra è minore o uguale a quello a destra.

123456789
# Checking if an order total qualifies for a discount limit order_total = 10 discount_limit = 9 print(order_total <= discount_limit) # False → total exceeds the discount limit # Verifying if a student arrived on time (in minutes) arrival_time = 5 deadline_time = 5 print(arrival_time <= deadline_time) # True → student arrived right on time
copy

Confronti concatenati

Python consente di scrivere intervalli in modo naturale: 0 < x < 10 significa "x è maggiore di 0 e minore di 10". Internamente si comporta come (0 < x) and (x < 10).

1234567
# Checking if the temperature is within a comfortable range temperature = 7 print(0 < temperature < 10) # True → temperature is within the cool range # Checking if a user's rating fits the top-tier range user_rating = 7 print(5 <= user_rating <= 7) # True → rating is within the premium bracket
copy

Questa sintassi è leggibile e evita di ripetere x.

Confronto tra stringhe

I confronti tra stringhe sono case-sensitive e lessicografici (carattere per carattere secondo l'ordine Unicode).

123456789
# Comparing user input with stored data saved_password = "Apple" typed_password = "apple" print(saved_password == typed_password) # False → passwords are case-sensitive # Sorting items alphabetically first_item = "apple" second_item = "banana" print(first_item < second_item) # True → "apple" comes before "banana" alphabetically
copy

Per confronti senza distinzione tra maiuscole e minuscole, normalizzare prima entrambi i lati.

12345
# Comparing email addresses entered in different cases email_stored = "Support@Codefinity.com" email_input = "support@codefinity.COM" print(email_stored.lower() == email_input.lower()) # True → emails match, case ignored
copy

1. Completa gli spazi con True o False:

2. Quale singola espressione verifica correttamente che x sia compreso tra 1 e 5 inclusi (utilizzando il chaining)?

3. Quale confronto tra stringhe restituisce True?

question-icon

Completa gli spazi con True o False:

5 == 5
3 < 2

9 >= 9

"A" == "a"

0 < 7 <= 7

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

question mark

Quale singola espressione verifica correttamente che x sia compreso tra 1 e 5 inclusi (utilizzando il chaining)?

Select the correct answer

question mark

Quale confronto tra stringhe restituisce True?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 2

Chieda ad AI

expand

Chieda ad AI

ChatGPT

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

Awesome!

Completion rate improved to 3.45

bookOperatori di Confronto

Scorri per mostrare il menu

I confronti permettono al tuo programma di porre domande sì/no sui valori:

  • Sono uguali?
  • Questo è maggiore?
  • Questo numero rientra in un intervallo?

Un confronto restituisce un valore booleano (True o False) ed è la base della logica if/while.

Fondamentali

Python fornisce sei operatori di confronto (==, !=, <, <=, >, >=) per verificare uguaglianza e ordinamento tra valori; ogni confronto restituisce True o False.

Uguaglianza ==

Verifica se due valori sono uguali.

1234567
saved_pin = 1234 entered_pin = 1234 print(saved_pin == entered_pin) # True → user entered the correct PIN stored_email = "support@codefinity.com" input_email = "Support@codefinity.com" print(stored_email == input_email) # False → case matters in string comparison
copy
Note
Nota

= assegna un valore a una variabile, mentre == confronta due valori.

Disuguaglianza !=

Verifica se due valori sono diversi.

1234567
user_id_1 = 105 user_id_2 = 203 print(user_id_1 != user_id_2) # True → users have different IDs username_1 = "alex" username_2 = "alex" print(username_1 != username_2) # False → usernames match
copy

Maggiore di >

Vero se il valore a sinistra è strettamente maggiore di quello a destra.

123456789
# Comparing delivery times in minutes estimated_time = 7 actual_time = 9 print(estimated_time > actual_time) # False → delivery took longer than expected # Comparing two product ratings rating_product_a = 12 rating_product_b = 3 print(rating_product_a > rating_product_b) # True → product A has a higher rating
copy

Minore di <

Vero se il valore a sinistra è strettamente inferiore a quello a destra.

123456789
# Comparing user's age with the minimum required age user_age = 17 min_age = 18 print(user_age < min_age) # True → user is too young to access the service # Comparing names alphabetically first_name = "Alice" second_name = "Bob" print(first_name < second_name) # True → "Alice" comes before "Bob" alphabetically
copy

Maggiore o uguale >=

Vero se il valore a sinistra è maggiore o uguale a quello a destra.

123456789
# Checking if a student reached the passing score student_score = 7 passing_score = 7 print(student_score >= passing_score) # True → student passed the test # Comparing two package weights before shipping package_weight = 4 min_weight_required = 9 print(package_weight >= min_weight_required) # False → package is too light
copy

Minore o uguale <=

Restituisce True se il valore a sinistra è minore o uguale a quello a destra.

123456789
# Checking if an order total qualifies for a discount limit order_total = 10 discount_limit = 9 print(order_total <= discount_limit) # False → total exceeds the discount limit # Verifying if a student arrived on time (in minutes) arrival_time = 5 deadline_time = 5 print(arrival_time <= deadline_time) # True → student arrived right on time
copy

Confronti concatenati

Python consente di scrivere intervalli in modo naturale: 0 < x < 10 significa "x è maggiore di 0 e minore di 10". Internamente si comporta come (0 < x) and (x < 10).

1234567
# Checking if the temperature is within a comfortable range temperature = 7 print(0 < temperature < 10) # True → temperature is within the cool range # Checking if a user's rating fits the top-tier range user_rating = 7 print(5 <= user_rating <= 7) # True → rating is within the premium bracket
copy

Questa sintassi è leggibile e evita di ripetere x.

Confronto tra stringhe

I confronti tra stringhe sono case-sensitive e lessicografici (carattere per carattere secondo l'ordine Unicode).

123456789
# Comparing user input with stored data saved_password = "Apple" typed_password = "apple" print(saved_password == typed_password) # False → passwords are case-sensitive # Sorting items alphabetically first_item = "apple" second_item = "banana" print(first_item < second_item) # True → "apple" comes before "banana" alphabetically
copy

Per confronti senza distinzione tra maiuscole e minuscole, normalizzare prima entrambi i lati.

12345
# Comparing email addresses entered in different cases email_stored = "Support@Codefinity.com" email_input = "support@codefinity.COM" print(email_stored.lower() == email_input.lower()) # True → emails match, case ignored
copy

1. Completa gli spazi con True o False:

2. Quale singola espressione verifica correttamente che x sia compreso tra 1 e 5 inclusi (utilizzando il chaining)?

3. Quale confronto tra stringhe restituisce True?

question-icon

Completa gli spazi con True o False:

5 == 5
3 < 2

9 >= 9

"A" == "a"

0 < 7 <= 7

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

question mark

Quale singola espressione verifica correttamente che x sia compreso tra 1 e 5 inclusi (utilizzando il chaining)?

Select the correct answer

question mark

Quale confronto tra stringhe restituisce True?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 2
some-alt