{"id":"variables-integers","slug":"integers","title":"Numbers and strings","text":"Variables can hold numbers as well as strings. A whole number, such as `7`, `-40` or `1000000`, is called an **integer**. You write it without quotes:\n\n```python\nyear = 2026\nprint(year)\nprint(year + 1)\n```\n\n```output\n2026\n2027\n```\n\n### \"25\" is not 25\n\nWith quotes, `\"25\"` is a string: two characters that happen to be digits. A locker number and a door code may look alike when printed, but Python treats them differently:\n\n```python\nlockers = 25\ndoor_code = \"25\"\nprint(lockers)\nprint(door_code)\nprint(lockers + lockers)\nprint(door_code + door_code)\n```\n\n```output\n25\n25\n50\n2525\n```\n\nFor numbers, `+` adds. For strings, it joins. What an operator does depends on the **type** of the values it works on: whether they are integers, strings or something else. Python's short name for the integer type is `int`, and for the string type it's `str`.\n\n### A look in the interpreter\n\nPython also has an **interactive interpreter**: you type one line after its `>>>` prompt, and Python runs it and shows the value right away. It shows a string with its quotes, so you can tell the string `'34'` from the number `34`:\n\n```pycon\n>>> 3 + 4\n7\n>>> \"3\" + \"4\"\n'34'\n```\n\n### Not every operation works for every type\n\nTaking a number away from a string makes no sense, and Python says so:\n\n```python\nwidth = \"12\"\nprint(width - 2)\n```\n\n```crash\nTraceback (most recent call last):\n  File \"main.py\", line 2, in <module>\n    print(width - 2)\n          ~~~~~~^~~\nTypeError: unsupported operand type(s) for -: 'str' and 'int'\n```\n\nA `TypeError` means that an operation got values of a type it can't work with. The last line says that `-` doesn't work on a `str` and an `int` (the values an operator works on are its **operands**). The `~` and `^` marks show the calculation that failed. The fix here is to drop the quotes, so that `width` holds a number.\n\nKeep in mind that what `input` gives you is always a string, even when the user types digits. The next module shows how to turn it into a number.\n","task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"x = 7\ny = \"7\"\nprint(x + x)\nprint(y + y)\nprint(x + 3)\n","solution":"x = 7\ny = \"7\"\nprint(x + x)\nprint(y + y)\nprint(x + 3)\n","hints":["`x` holds the integer 7, and `y` holds a string, because of the quotes.","`+` adds two integers but joins two strings: `\"7\" + \"7\"` is the string `77`."],"tests":[{"name":"Output","stdin":"","expect":"14\n77\n10","hidden":false}],"require":[],"forbid":[],"lines":["14","77","10"],"more":[{"task":"The program should print the number of fruits in the basket, 12, but it prints 57. Fix it.\n\n```\n12\n```\n","kind":"code","seed":"apples = \"5\"\npears = \"7\"\nprint(apples + pears)\n","solution":"apples = 5\npears = 7\nprint(apples + pears)\n","hints":["The quotes make both values strings, and `+` joins strings.","Remove the quotes around the numbers, so `+` adds them."],"tests":[{"name":"Output","stdin":"","expect":"12","hidden":false}],"require":[],"forbid":[{"pattern":"\\b12\\b","raw":true,"message":"Let Python add the numbers: fix the variables instead of printing 12 directly."}]},{"task":"At the zoo, a ticket costs 12 for an adult and 7 for a child. The variables hold the number of adults and children in a family. Add two lines that print the number of tickets and then the total price, both calculated from the variables:\n\n```\n5\n50\n```\n","kind":"code","seed":"adults = 3\nchildren = 2\n","solution":"adults = 3\nchildren = 2\nprint(adults + children)\nprint(adults * 12 + children * 7)\n","hints":["The number of tickets is the number of adults plus the number of children.","The total price is the adults times 12 plus the children times 7. Multiplication comes before addition, so no parentheses are needed.","`print(adults * 12 + children * 7)`"],"tests":[{"name":"Output","stdin":"","expect":"5\n50","hidden":false}],"require":[{"pattern":"\\badults\\b","min":3,"message":"Calculate both results with the variable `adults`."},{"pattern":"\\bchildren\\b","min":3,"message":"Calculate both results with the variable `children`."}],"forbid":[{"pattern":"\\b50\\b","raw":true,"message":"Let Python calculate the total instead of typing 50."}]}]}