{"id":"conditionals-comparisons","slug":"comparisons","title":"Comparison operators","text":"A condition usually compares two values. These are Python's **comparison operators**:\n\n| Operator | Means | Example |\n|:-:|---|---|\n| `==` | equal to | `a == b` |\n| `!=` | not equal to | `a != b` |\n| `<` | less than | `a < b` |\n| `<=` | less than or equal to | `a <= b` |\n| `>` | greater than | `a > b` |\n| `>=` | greater than or equal to | `a >= b` |\n\nThe difference between `<` and `<=` matters at the edge: `18 < 18` is false, but `18 <= 18` is true.\n\nA program can have several `if` statements one after another. This guessing game has three:\n\n```python\nsecret = 7\nguess = int(input(\"Guess a number from 1 to 10: \"))\nif guess < secret:\n    print(\"Too small.\")\nif guess > secret:\n    print(\"Too big.\")\nif guess == secret:\n    print(\"Right! Well done.\")\n```\n\n```input\n4\n```\n\n```output\nGuess a number from 1 to 10: 4\nToo small.\n```\n\nPython checks each `if` on its own, in order. A number can't be smaller than 7, bigger than 7 and equal to 7 at the same time, so exactly one of the three messages is printed.\n\n### = is not ==\n\nA single `=` stores a value in a variable. Two, `==`, compare two values. Mixing them up is one of the most common mistakes, and in a condition Python catches it:\n\n```python\nguess = 7\nif guess = 7:\n    print(\"Right!\")\n```\n\n```crash\n  File \"main.py\", line 2\n    if guess = 7:\n       ^^^^^^^^^\nSyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?\n```\n\nPython suggests `==` or `:=`. The second is a rarer operator that this course doesn't need: `==` is the one you want.\n\n### Comparing strings\n\n`==` and `!=` work for strings too. Two strings are equal only when they are exactly the same, character by character, capitals and spaces included:\n\n```python\nanswer = input(\"Do you like Python? \")\nif answer == \"yes\":\n    print(\"Great!\")\nif answer != \"yes\":\n    print(\"Give it a few more weeks.\")\n```\n\n```input\nYes\n```\n\n```output\nDo you like Python? Yes\nGive it a few more weeks.\n```\n\n`Yes` with a capital Y is not the same string as `yes`. `!=` is handy for doing something whenever a value is anything but one particular value.\n","task":"Fill in the blank so the program unlocks only with the PIN `4321`. If the user types `4321`, the run looks like this:\n\n```\nPIN: 4321\nUnlocked\n```\n","kind":"fill","seed":"code = input(\"PIN: \")\nif code [[==]] \"4321\":\n    print(\"Unlocked\")\n","solution":"code = input(\"PIN: \")\nif code == \"4321\":\n    print(\"Unlocked\")\n","hints":["The condition should be true when the two strings are the same.","The operator for \"equal to\" is two equals signs: `==`."],"tests":[{"name":"Test 1","stdin":"4321\n","expect":"PIN: 4321\nUnlocked","hidden":false},{"name":"Test 2","stdin":"1234\n","expect":"PIN: 1234","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Write a program that asks for two whole numbers and tells which one is bigger, with one of these three messages:\n\n- `The first number is bigger.`\n- `The second number is bigger.`\n- `The numbers are equal.`\n\nIf the user types `5` and `8`, the run looks like this:\n\n```\nFirst: 5\nSecond: 8\nThe second number is bigger.\n```\n","kind":"code","seed":"first = int(input(\"First: \"))\nsecond = int(input(\"Second: \"))\n","solution":"first = int(input(\"First: \"))\nsecond = int(input(\"Second: \"))\nif first > second:\n    print(\"The first number is bigger.\")\nif second > first:\n    print(\"The second number is bigger.\")\nif first == second:\n    print(\"The numbers are equal.\")\n","hints":["Write three separate `if` statements, one for each message.","The conditions are `first > second`, `second > first` and `first == second`."],"tests":[{"name":"Test 1","stdin":"5\n8\n","expect":"First: 5\nSecond: 8\nThe second number is bigger.","hidden":false},{"name":"Test 2","stdin":"9\n3\n","expect":"First: 9\nSecond: 3\nThe first number is bigger.","hidden":true},{"name":"Test 3","stdin":"4\n4\n","expect":"First: 4\nSecond: 4\nThe numbers are equal.","hidden":true}],"require":[],"forbid":[]},{"task":"At the museum, a ticket costs 5 for visitors under 12 and 10 for everyone 12 or older. Write a program that asks for the visitor's age and prints the price. If the user types `12`, the run looks like this:\n\n```\nAge: 12\nTicket: 10 euros\n```\n\nMake sure that a visitor who is exactly 12 pays 10.\n","kind":"code","seed":"age = int(input(\"Age: \"))\n","solution":"age = int(input(\"Age: \"))\nif age < 12:\n    print(\"Ticket: 5 euros\")\nif age >= 12:\n    print(\"Ticket: 10 euros\")\n","hints":["You need two `if` statements, one for each price.","\"Under 12\" is `age < 12`. \"12 or older\" includes 12 itself, so it's `age >= 12`."],"tests":[{"name":"Test 1","stdin":"12\n","expect":"Age: 12\nTicket: 10 euros","hidden":false},{"name":"Test 2","stdin":"11\n","expect":"Age: 11\nTicket: 5 euros","hidden":true},{"name":"Test 3","stdin":"40\n","expect":"Age: 40\nTicket: 10 euros","hidden":true},{"name":"Test 4","stdin":"3\n","expect":"Age: 3\nTicket: 5 euros","hidden":true}],"require":[],"forbid":[]}]}