Vertailuoperaattorit
Pyyhkäise näyttääksesi valikon
Vertailut mahdollistavat ohjelman esittää kyllä/ei-kysymyksiä arvoista:
- Ovatko nämä samat?
- Onko tämä suurempi?
- Sijoittuuko tämä luku tietylle alueelle?
Vertailu palauttaa Boolean-arvon (True tai False) ja muodostaa if/while-logiikan perustan.
Perusteet
Python tarjoaa kuusi vertailuoperaattoria (==, !=, <, <=, >, >=) arvojen yhtäsuuruuden ja järjestyksen testaamiseen. Jokainen vertailu palauttaa arvon True tai False.
Yhtäsuuruus ==
Tarkistaa, ovatko kaksi arvoa samat.
1234567saved_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
= asettaa arvon muuttujalle, kun taas == vertaa kahta arvoa.
Eriarvoisuus !=
Tarkistaa, ovatko kaksi arvoa erilaisia.
1234567user_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
Suurempi kuin >
Tosi, jos vasemmanpuoleinen arvo on ehdottomasti suurempi kuin oikeanpuoleinen.
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
Vähemmän kuin <
Tosi, jos vasemmanpuoleinen arvo on tiukasti pienempi kuin oikeanpuoleinen.
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
Suurempi tai yhtä suuri >=
Tosi, jos vasemmanpuoleinen arvo on suurempi tai yhtä suuri kuin oikeanpuoleinen.
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
Vähemmän kuin tai yhtä suuri <=
Tosi, jos vasemmanpuoleinen arvo on pienempi tai yhtä suuri kuin oikeanpuoleinen.
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
Ketjutetut vertailut
Pythonissa voit kirjoittaa alueet luonnollisesti:
0 < x < 10 tarkoittaa "x on suurempi kuin 0 ja pienempi kuin 10".
Taustalla tämä toimii kuten (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
Tämä on selkeälukuista ja välttää x:n toistamisen.
Merkkijonojen vertailu
Merkkijonojen vertailu on kirjainkoon huomioivaa ja leksikografista (merkki kerrallaan Unicode-järjestyksessä).
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
Kirjainkoon huomioimattomia vertailuja varten normalisoi molemmat osapuolet ensin.
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
1. Täydennä aukot True tai False:
2. Mikä yksittäinen lauseke tarkistaa oikein, että x on 1:n ja 5:n välillä mukaan lukien (käyttäen ketjutusta)?
3. Mikä merkkijonojen vertailu on tosi?
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme