{"id":"conditionals-booleans","slug":"true-and-false","title":"True and False","text":"A condition has a value, just like a calculation. A comparison is either true or false, and Python has two values for that: `True` and `False`. They are called **Boolean values**, after the mathematician George Boole, and their type is `bool`:\n\n```pycon\n>>> 5 > 3\nTrue\n>>> 5 == 3\nFalse\n>>> type(5 > 3)\n<class 'bool'>\n```\n\nCode whose value is `True` or `False`, such as a comparison, is called a **Boolean expression**. Every condition you've written so far is one: the `if` runs its block when the value is `True` and skips it when it's `False`.\n\n### A condition in a variable\n\nThe value of a comparison can be stored in a variable like any other value, and the variable can then be the condition of an `if`:\n\n```python\nage = int(input(\"Age: \"))\nis_adult = age >= 18\nprint(is_adult)\nif is_adult:\n    print(\"You can vote.\")\n```\n\n```input\n20\n```\n\n```output\nAge: 20\nTrue\nYou can vote.\n```\n\n`is_adult = age >= 18` works out the comparison first and stores its value, `True`, in `is_adult`. Then `if is_adult:` reads almost like English. A good name for a Boolean variable answers a yes-or-no question: `is_adult`, `has_ticket`, `game_over`.\n\n### Writing True and False yourself\n\nYou can also write `True` and `False` straight into the code. A setting at the top of a program is a typical use:\n\n```python\nshow_tips = True\nprint(\"Welcome!\")\nif show_tips:\n    print(\"Tip: the Tab key indents a line.\")\n```\n\n```output\nWelcome!\nTip: the Tab key indents a line.\n```\n\nChange the first line to `show_tips = False`, and the tip disappears. Both values start with a capital letter. In small letters, `true` is just a name that Python doesn't know:\n\n```python\nshow_tips = true\n```\n\n```crash\nTraceback (most recent call last):\n  File \"main.py\", line 1, in <module>\n    show_tips = true\n                ^^^^\nNameError: name 'true' is not defined. Did you mean: 'True'?\n```\n","task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"a = 10\nb = 3\nprint(a > b)\nprint(a == b * 3)\nprint(a != 10)\nbig = a >= 10\nprint(big)\n","solution":"a = 10\nb = 3\nprint(a > b)\nprint(a == b * 3)\nprint(a != 10)\nbig = a >= 10\nprint(big)\n","hints":["Each comparison prints `True` or `False`.","`b * 3` is 9, which isn't 10. `a >= 10` is true when `a` is exactly 10."],"tests":[{"name":"Output","stdin":"","expect":"True\nFalse\nFalse\nTrue","hidden":false}],"require":[],"forbid":[],"lines":["True","False","False","True"],"more":[{"task":"Fill in the blank so the program finds out whether a number is even. If the user types `14`, the run looks like this:\n\n```\nNumber: 14\nEven: True\nIt splits into two equal halves.\n```\n","kind":"fill","seed":"number = int(input(\"Number: \"))\nis_even = number % 2 [[==]] 0\nprint(f\"Even: {is_even}\")\nif is_even:\n    print(\"It splits into two equal halves.\")\n","solution":"number = int(input(\"Number: \"))\nis_even = number % 2 == 0\nprint(f\"Even: {is_even}\")\nif is_even:\n    print(\"It splits into two equal halves.\")\n","hints":["A number is even when the remainder of dividing it by 2 is zero.","The comparison \"equal to\" is `==`, so `is_even` holds `True` or `False`."],"tests":[{"name":"Test 1","stdin":"14\n","expect":"Number: 14\nEven: True\nIt splits into two equal halves.","hidden":false},{"name":"Test 2","stdin":"7\n","expect":"Number: 7\nEven: False","hidden":true}],"require":[],"forbid":[]},{"task":"A shop ships orders of 50 or more for free. Write a program that asks for the order total, stores whether the order ships for free in a Boolean variable, prints that value and, when it's `True`, a message. If the user types `62.5`, the run looks like this:\n\n```\nOrder total: 62.5\nFree shipping: True\nYour order ships for free!\n```\n\nUse the Boolean variable itself as the condition of the `if`.\n","kind":"code","seed":"total = float(input(\"Order total: \"))\n","solution":"total = float(input(\"Order total: \"))\nfree_shipping = total >= 50\nprint(f\"Free shipping: {free_shipping}\")\nif free_shipping:\n    print(\"Your order ships for free!\")\n","hints":["Store the comparison in a variable: `free_shipping = total >= 50`.","Print it with an f-string, then write `if free_shipping:` with the message in its block."],"tests":[{"name":"Test 1","stdin":"62.5\n","expect":"Order total: 62.5\nFree shipping: True\nYour order ships for free!","hidden":false},{"name":"Test 2","stdin":"49.99\n","expect":"Order total: 49.99\nFree shipping: False","hidden":true},{"name":"Test 3","stdin":"50\n","expect":"Order total: 50\nFree shipping: True\nYour order ships for free!","hidden":true}],"require":[{"pattern":"\\bif\\s+\\w+\\s*:","message":"Store the comparison in a variable and use that variable alone as the condition of the `if`."}],"forbid":[]}]}