{"id":"more-features-conditional-expressions","slug":"conditional-expressions","title":"Conditional expressions","text":"An `if` statement chooses which code runs. Often, though, the only thing that changes is a value:\n\n```python\ntemperature = 25\nif temperature > 20:\n    print(\"warm\")\nelse:\n    print(\"cold\")\n```\n\n```output\nwarm\n```\n\nA **conditional expression** chooses between two values in one line. It reads almost like English: `\"warm\" if temperature > 20 else \"cold\"` is `\"warm\"` when the condition is true, and `\"cold\"` otherwise. Since it's an expression, it goes anywhere a value can go:\n\n```python\ntemperature = 25\nprint(\"warm\" if temperature > 20 else \"cold\")\nlabel = \"even\" if temperature % 2 == 0 else \"odd\"\nprint(label)\n```\n\n```output\nwarm\nodd\n```\n\nThe order is: the value for true, then `if` and the condition, then `else` and the value for false. (Some languages call this the ternary operator, because it has three parts.)\n\n### Where it shines\n\nSmall choices inside a longer line, such as a word that changes with a number, are where a conditional expression saves the most. Without it, each line would need an `if` statement of four lines:\n\n```python\nfor count in [0, 1, 5]:\n    print(f\"{count} {'file' if count == 1 else 'files'} found\")\n```\n\n```output\n0 files found\n1 file found\n5 files found\n```\n\nIt works in a `return` too:\n\n```python\ndef larger(a, b):\n    return a if a > b else b\n\n\nprint(larger(3, 8))\nprint(larger(10, -2))\n```\n\n```output\n8\n10\n```\n\n### The else is not optional\n\nAn expression must always have a value, so the `else` part is required. Without it, Python can't read the line:\n\n```python\nscore = 7\nprint(\"passed\" if score >= 5)\n```\n\n```crash\n  File \"main.py\", line 2\n    print(\"passed\" if score >= 5)\n          ^^^^^^^^^^^^^^^^^^^^^^\nSyntaxError: expected 'else' after 'if' expression\n```\n\nUse a conditional expression to choose a value. When the two cases do different things, or there are more than two cases, an ordinary `if`, `elif` and `else` is easier to read.\n","task":"Fill in the blanks so the program sets `status` to `\"adult\"` for an age of 18 or more and to `\"minor\"` otherwise. For example:\n\n```\nAge: 20\nStatus: adult\n```\n","kind":"fill","seed":"age = int(input(\"Age: \"))\nstatus = \"adult\" [[if]] age >= 18 [[else]] \"minor\"\nprint(\"Status:\", status)\n","solution":"age = int(input(\"Age: \"))\nstatus = \"adult\" if age >= 18 else \"minor\"\nprint(\"Status:\", status)\n","hints":["The value for true comes first, then the keyword that starts the condition.","The pattern is `value if condition else other_value`: type `if` and `else` into the blanks."],"tests":[{"name":"Test 1","stdin":"20\n","expect":"Age: 20\nStatus: adult","hidden":false},{"name":"Test 2","stdin":"17\n","expect":"Age: 17\nStatus: minor","hidden":true},{"name":"Test 3","stdin":"18\n","expect":"Age: 18\nStatus: adult","hidden":true}],"require":[],"forbid":[],"more":[{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"x = 7\nprint(\"odd\" if x % 2 == 1 else \"even\")\ny = 10 if x > 5 else 0\nprint(y)\nprint(x * 2 if x < 5 else x - 2)\n","solution":"x = 7\nprint(\"odd\" if x % 2 == 1 else \"even\")\ny = 10 if x > 5 else 0\nprint(y)\nprint(x * 2 if x < 5 else x - 2)\n","hints":["In each expression, check the condition first. It decides which of the two values the whole expression is.","7 is odd and greater than 5, but not less than 5."],"tests":[{"name":"Output","stdin":"","expect":"odd\n10\n5","hidden":false}],"require":[],"forbid":[],"lines":["odd","10","5"]},{"task":"Write a function `plural(count, word)` that returns the count and the word, with an `s` added to the word unless the count is exactly 1: `plural(1, \"apple\")` returns `\"1 apple\"`, and `plural(3, \"apple\")` returns `\"3 apples\"`. Choose between the two with a conditional expression, not an `if` statement.\n","kind":"code","seed":"def plural(count, word):\n    pass\n\n\nif __name__ == \"__main__\":\n    print(plural(1, \"apple\"))\n    print(plural(3, \"apple\"))\n","solution":"def plural(count, word):\n    ending = \"\" if count == 1 else \"s\"\n    return f\"{count} {word}{ending}\"\n\n\nif __name__ == \"__main__\":\n    print(plural(1, \"apple\"))\n    print(plural(3, \"apple\"))\n","hints":["The only thing that changes is the ending of the word, `\"\"` or `\"s\"`.","Choose it with `\"\" if count == 1 else \"s\"`, and build the result with an f-string."],"tests":[{"name":"plural(1, \"apple\")","stdin":"","call":"plural(1, \"apple\")\n","expect":"'1 apple'","hidden":false,"returns":true},{"name":"plural(3, \"apple\")","stdin":"","call":"plural(3, \"apple\")\n","expect":"'3 apples'","hidden":false,"returns":true},{"name":"plural(0, \"cat\")","stdin":"","call":"plural(0, \"cat\")\n","expect":"'0 cats'","hidden":true,"returns":true},{"name":"plural(1, \"bus stop\")","stdin":"","call":"plural(1, \"bus stop\")\n","expect":"'1 bus stop'","hidden":true,"returns":true},{"name":"plural(12, \"egg\")","stdin":"","call":"plural(12, \"egg\")\n","expect":"'12 eggs'","hidden":true,"returns":true}],"require":[{"pattern":"\\bif\\b[^\\n]*\\belse\\b","message":"Choose with a conditional expression, `value if condition else other_value`."}],"forbid":[{"pattern":"^\\s*else\\s*[:]","flags":"m","message":"Use a conditional expression instead of an `if` and `else` statement."}]}]}