Numbers and strings
Every value in Python has a type. Use the >>> prompt as a calculator and meet the three ways to divide, then step through a five-line program, tea.py, that makes an int, a float and a string, prints them, and turns a number into text with str(). Finish with what happens when you mix text and a number without converting: a TypeError.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01What does Python give for 7 / 2 ?
/ always gives a float, so 7 / 2 is 3.5. Use // to drop the fraction (3) and % for the remainder (1).
02What is 2 + 3 * 4 ?
As in maths, * is worked out before +: 3 * 4 is 12, plus 2 is 14. Write (2 + 3) * 4 to get 20.
03What is the type of 5 * 1.5 ?
1.5 is a float, and an int combined with a float gives a float: 7.5.
04What does "ha" * 3 give?
* repeats a string: "ha" * 3 is 'hahaha'. + would join two strings end to end.
05cups is the int 5. Which line prints Cups: 5 without an error?
+ joins a string only to another string. str(cups) turns 5 into '5'. Without it Python stops with TypeError: can only concatenate str (not "int") to str.
Glossary
- Type
- The kind of a value, such as int, float or str. It decides what you can do with the value; type() tells you a value's type.
- int
- A whole number such as 5 or -12. Python ints can be as big as you like.
- float
- A number with a decimal point, such as 1.5 or 3.0. Stored in binary, so it can be very slightly off (0.1 + 0.2).
- str (string)
- Text in single or double quotes, such as "tea". + joins strings and * repeats them.
- Operator
- A symbol that works on values: + - * / // % ** for numbers, + and * for strings.
- Floor division and remainder
- // divides and rounds down (7 // 2 is 3); % gives what is left over (7 % 2 is 1).
- Conversion
- Making a value of one type from another: str(5) is '5', int("42") is 42, float("1.5") is 1.5.
- TypeError
- The error Python raises when an operation gets a value of the wrong type, such as joining a str and an int with +.
Notes and sources
- The $ and >>> at the start of a line are prompts printed for you: you do not type them. On macOS and Linux the command may be python3.
- A float is stored in binary with about 15 to 17 significant digits, so most decimal fractions such as 0.1 are stored very slightly off; round(0.1 + 0.2, 2) gives 0.3.
- // rounds down, toward minus infinity, so -7 // 2 is -4; and % follows it, so -7 % 2 is 1.
- Python 3.11 and later also print a line of ~ and ^ under the failing code, pointing at the +; the figure leaves it out. In the real message, the ... is the full path to the file.
- Python ints have no fixed size: 2 ** 100 is exact. Floats do have limits; the largest is about 1.8 × 10 to the power 308.
- Python tutorial: An informal introduction to Python (numbers and text)
- Python library: Numeric types int, float, complex
- Python library: Text sequence type str
- Python reference: Operator precedence
- Python library: Built-in functions (type, int, float, str, print)
- Python tutorial: Floating-point arithmetic, issues and limitations
- Python library: Built-in exceptions (TypeError)
- Python tutorial: Errors and exceptions
Quiz Unlocks after the last step.