Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Jämförelseoperatorer | Booleska Värden och Jämförelser
Datatyper i Python

bookJämförelseoperatorer

Comparisons let your program ask yes/no questions about values:

  • Are these equal?
  • Is one bigger?
  • Is a value inside a range?

A comparison returns a Boolean (True / False) and powers if/while.

Equality ==

Checks whether two values are the same.

1234567
saved_pin = 1234 entered_pin = 1234 print(saved_pin == entered_pin) # True stored_email = "support@codefinity.com" input_email = "Support@codefinity.com" print(stored_email == input_email) # False
copy
Note
Note

= - assigns, == - compares.

Inequality !=

True when values differ.

1234567
user_id_1 = 105 user_id_2 = 203 print(user_id_1 != user_id_2) # True username_1 = "alex" username_2 = "alex" print(username_1 != username_2) # False
copy

Greater Than >

1234567
estimated = 7 actual = 9 print(estimated > actual) # False rating_a = 12 rating_b = 3 print(rating_a > rating_b) # True
copy

Less Than <

12345
user_age = 17 min_age = 18 print(user_age < min_age) # True print("Alice" < "Bob") # True
copy

Greater or Equal >=

123
student_score = 7 passing = 7 print(student_score >= passing) # True
copy

Less or Equal <=

123
order_total = 10 limit = 9 print(order_total <= limit) # False
copy

Chained Comparisons

Python supports natural ranges: 0 < x < 10 works like (0 < x) and (x < 10).

12345
temperature = 7 print(0 < temperature < 10) # True user_rating = 7 print(5 <= user_rating <= 7) # True
copy

Comparing Strings

String comparisons are case-sensitive and lexicographic.

12345
saved_password = "Apple" typed_password = "apple" print(saved_password == typed_password) # False print("apple" < "banana") # True
copy

For case-insensitive matching:

123
email_stored = "Support@Codefinity.com" email_input = "support@codefinity.COM" print(email_stored.lower() == email_input.lower()) # True
copy
question mark

Which single expression correctly checks that x is between 1 and 5 inclusive (using chaining)?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 2

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

bookJämförelseoperatorer

Svep för att visa menyn

Comparisons let your program ask yes/no questions about values:

  • Are these equal?
  • Is one bigger?
  • Is a value inside a range?

A comparison returns a Boolean (True / False) and powers if/while.

Equality ==

Checks whether two values are the same.

1234567
saved_pin = 1234 entered_pin = 1234 print(saved_pin == entered_pin) # True stored_email = "support@codefinity.com" input_email = "Support@codefinity.com" print(stored_email == input_email) # False
copy
Note
Note

= - assigns, == - compares.

Inequality !=

True when values differ.

1234567
user_id_1 = 105 user_id_2 = 203 print(user_id_1 != user_id_2) # True username_1 = "alex" username_2 = "alex" print(username_1 != username_2) # False
copy

Greater Than >

1234567
estimated = 7 actual = 9 print(estimated > actual) # False rating_a = 12 rating_b = 3 print(rating_a > rating_b) # True
copy

Less Than <

12345
user_age = 17 min_age = 18 print(user_age < min_age) # True print("Alice" < "Bob") # True
copy

Greater or Equal >=

123
student_score = 7 passing = 7 print(student_score >= passing) # True
copy

Less or Equal <=

123
order_total = 10 limit = 9 print(order_total <= limit) # False
copy

Chained Comparisons

Python supports natural ranges: 0 < x < 10 works like (0 < x) and (x < 10).

12345
temperature = 7 print(0 < temperature < 10) # True user_rating = 7 print(5 <= user_rating <= 7) # True
copy

Comparing Strings

String comparisons are case-sensitive and lexicographic.

12345
saved_password = "Apple" typed_password = "apple" print(saved_password == typed_password) # False print("apple" < "banana") # True
copy

For case-insensitive matching:

123
email_stored = "Support@Codefinity.com" email_input = "support@codefinity.COM" print(email_stored.lower() == email_input.lower()) # True
copy
question mark

Which single expression correctly checks that x is between 1 and 5 inclusive (using chaining)?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 2
some-alt