{"id":"terminology-expressions-types","slug":"expressions-and-types","title":"Expressions, values and types","text":"An **expression** is a piece of code that Python can work out to get a value. `2 + 3` is an expression, and its value is `5`. Working out the value is called **evaluating** the expression. A single number or a variable on its own is an expression too: the value of `score` is whatever the variable holds at that moment.\n\nAn expression can go anywhere Python expects a value: on the right side of `=`, inside the parentheses of `print`, inside the braces of an f-string, or as the condition of an `if`. That's the difference from a statement: a statement does something, and an expression has a value. `total = 5` is a statement, while `total + 5` is an expression.\n\nPython's **interactive interpreter** shows this well. It runs one line at a time, and when you type an expression after its `>>>` prompt, it shows the expression's value. Here is what it prints for a few expressions:\n\n```pycon\n>>> 7 + 8 * 2\n23\n>>> 9 / 4\n2.25\n>>> \"rain\" + \"bow\"\n'rainbow'\n>>> 20 - 6 < 10\nFalse\n```\n\nThe interpreter shows a string in quotes, so you can see that `'rainbow'` is text. The last expression is a comparison, and its value is `False`.\n\n### Step by step\n\nSmall expressions combine into bigger ones, and Python evaluates a big one piece by piece, each time putting a value in place of a part. With `price = 4` and `count = 3`, the condition `price * count + 5 > 20` goes like this:\n\n```\nprice * count + 5 > 20\n4 * 3 + 5 > 20\n12 + 5 > 20\n17 > 20\nFalse\n```\n\nMultiplication comes before addition, as in mathematics, and the comparison comes after the arithmetic.\n\n### Every expression has a type\n\nSince an expression has a value, it also has a type: its **data type**. You met the function `type` in part 1. Give it any expression, and it tells the type of the expression's value:\n\n```python\nprint(type(42))\nprint(type(3.5 * 2))\nprint(type(\"4\" + \"2\"))\nprint(type(42 > 7))\n```\n\n```output\n<class 'int'>\n<class 'float'>\n<class 'str'>\n<class 'bool'>\n```\n\nThese are the four types you know: `int` for whole numbers, `float` for numbers with a decimal point, `str` for strings and `bool` for the truth values `True` and `False`. A comparison is an expression of the type `bool`, which is why it can be the condition of an `if`, or be printed:\n\n```python\nprice = 4\ncount = 3\ntotal = price * count + 5\nprint(f\"Total: {total}, over 20: {total > 20}\")\n```\n\n```output\nTotal: 17, over 20: False\n```\n","task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"a = 7\nb = 2\nprint(a + b * 3)\nprint(a > b * 3)\nprint(str(a) + str(b))\nprint(type(a > b))\n","solution":"a = 7\nb = 2\nprint(a + b * 3)\nprint(a > b * 3)\nprint(str(a) + str(b))\nprint(type(a > b))\n","hints":["Multiplication comes first: `b * 3` is 6, in both the first and the second `print`.","`a > b * 3` is the comparison `7 > 6`, so its value is a Boolean. `str(a) + str(b)` joins two strings.","A comparison has the type `bool`, printed as `<class 'bool'>`."],"tests":[{"name":"Output","stdin":"","expect":"13\nTrue\n72\n<class 'bool'>","hidden":false}],"require":[],"forbid":[],"lines":["13","True","72","<class 'bool'>"],"more":[{"task":"Fill in each blank with a value of the type the comment asks for, so the program prints:\n\n```\n<class 'int'>\n<class 'float'>\n<class 'str'>\n<class 'bool'>\n```\n","kind":"fill","seed":"print(type([[25]]))  # a whole number\nprint(type([[2.5]]))  # a number with a decimal point\nprint(type([[\"25\"]]))  # a piece of text\nprint(type([[True]]))  # a truth value\n","solution":"print(type(25))  # a whole number\nprint(type(2.5))  # a number with a decimal point\nprint(type(\"25\"))  # a piece of text\nprint(type(True))  # a truth value\n","hints":["A whole number has no decimal point (`25`), and a float has one (`2.5`).","Text is a string, so it needs quotes around it, even when it's made of digits.","A truth value is `True` or `False`, with a capital letter and no quotes. A comparison such as `3 > 1` works too."],"tests":[{"name":"Output","stdin":"","expect":"<class 'int'>\n<class 'float'>\n<class 'str'>\n<class 'bool'>","hidden":false}],"require":[],"forbid":[]},{"task":"Write a program that asks for two whole numbers and prints the values of three comparisons about them. Put each comparison inside the braces of an f-string, so Python prints its value.\n\n```\nFirst number: 5\nSecond number: 8\nIs the first smaller? True\nAre they equal? False\nIs their sum over 10? True\n```\n","kind":"code","seed":"first = int(input(\"First number: \"))\nsecond = int(input(\"Second number: \"))\n","solution":"first = int(input(\"First number: \"))\nsecond = int(input(\"Second number: \"))\nprint(f\"Is the first smaller? {first < second}\")\nprint(f\"Are they equal? {first == second}\")\nprint(f\"Is their sum over 10? {first + second > 10}\")\n","hints":["A comparison is an expression with the value `True` or `False`, so it can go inside the braces of an f-string like any other expression.","The three comparisons are `first < second`, `first == second` and `first + second > 10`.","For example: `print(f\"Are they equal? {first == second}\")`."],"tests":[{"name":"Test 1","stdin":"5\n8\n","expect":"First number: 5\nSecond number: 8\nIs the first smaller? True\nAre they equal? False\nIs their sum over 10? True","hidden":false},{"name":"Test 2","stdin":"8\n5\n","expect":"First number: 8\nSecond number: 5\nIs the first smaller? False\nAre they equal? False\nIs their sum over 10? True","hidden":true},{"name":"Test 3","stdin":"4\n4\n","expect":"First number: 4\nSecond number: 4\nIs the first smaller? False\nAre they equal? True\nIs their sum over 10? False","hidden":true},{"name":"Test 4","stdin":"5\n5\n","expect":"First number: 5\nSecond number: 5\nIs the first smaller? False\nAre they equal? True\nIs their sum over 10? False","hidden":true}],"require":[],"forbid":[{"pattern":"\\bif\\b","message":"No `if` is needed. Print the value of each comparison itself."}]}]}