Numbers in Python
Python has three number types. int is for whole numbers, float for decimals and complex for math with imaginary parts. Seven arithmetic operators do the math, and the three that need a closer look are // for whole-number division, % for the remainder and ** for powers.
Key facts
- An
inthas no fixed size limit.2 ** 100is a 31-digit number and Python prints it in full. /gives a float even when both numbers are whole, as in8 / 2. Use//for a whole number.**is the power operator.^is something else entirely.- Floats are stored in binary, so
0.1 + 0.2prints as0.30000000000000004. Compare rounded values.
What are the number types in Python?
Three. Whole numbers are int, numbers with a decimal point are float, and complex covers numbers with an imaginary part, which you can ignore until a math course asks for them.
print(type(7))
print(type(7.0))
print(type(2 + 3j))
print(2 ** 100)<class 'int'> <class 'float'> <class 'complex'> 1267650600228229401496703205376
The last line shows that Python integers grow as large as memory allows, so adding, subtracting or multiplying integers never runs out of room, as it can in some other languages.
What are the 7 arithmetic operators in Python?
Python has seven arithmetic operators for numbers. They cover addition, subtraction, multiplication, division, floor division, modulo and power. The first four you know. The other three need a closer look.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 7 + 2 | 9 |
- | Subtraction | 7 - 2 | 5 |
* | Multiplication | 7 * 2 | 14 |
/ | Division | 7 / 2 | 3.5 |
// | Floor division | 7 // 2 | 3 |
% | Modulo (remainder) | 7 % 2 | 1 |
** | Power | 7 ** 2 | 49 |
print(7 + 2)
print(7 - 2)
print(7 * 2)
print(7 / 2)
print(7 // 2)
print(7 % 2)
print(7 ** 2)9 5 14 3.5 3 1 49
What is the difference between / and // in Python?
With ints and floats, / always returns a float, and // rounds the result down to a whole number. It rounds down, not toward zero, which matters for negative numbers. The result of // is an int when both numbers are ints, so 7 // 2 is 3 but 7.5 // 2 is 3.0.
print(8 / 2)
print(8 // 2)
print(-7 // 2)
print(-7 % 2)4.0 4 -4 1
8 / 2 is 4.0, a float, even though it divides cleanly. -7 // 2 is -4 because -3.5 rounds down to -4, and the remainder is then 1 so that -4 * 2 + 1 gets back to -7.
What does num % 2 == 0 mean?
It asks whether a number is even. % gives the remainder after division, so the remainder of dividing by 2 is 0 for even numbers and 1 for odd ones. The same trick checks whether a number is divisible by 3, 5 or any other number except zero.
for n in (4, 7, 10):
print(n, n % 2 == 0)4 True 7 False 10 True
The for line runs the print once for each number. Loops come later in the course. Here the loop saves writing three print lines.
Why does 0.1 + 0.2 not equal 0.3?
Because floats are stored in binary, and neither 0.1 nor 0.2 has an exact binary form. Their small errors add up to a value slightly different from the one Python stores for 0.3, so Python prints more digits to tell them apart. It is not a Python bug; nearly every language stores floats in the same standard binary format and gets the same value, even when it prints it rounded.
print(0.1 + 0.2)
print(0.1 + 0.2 == 0.3)
print(round(0.1 + 0.2, 2))
print(round(0.1 + 0.2, 2) == 0.3)0.30000000000000004 False 0.3 True
The fix is to round before comparing, or to work in the smallest unit that matters, such as cents as an int. For money and accounting, Python ships with a decimal module that does exact math with decimal amounts. Using it takes an import line, which comes later in the course.
How do I round and convert numbers in Python?
round() rounds to a whole number or to a number of decimal places, and int() chops the decimals off. Two more built-in functions help with numbers. abs() drops the sign, and divmod() gives the floor division result and the remainder in one call.
print(round(3.14159, 2))
print(round(2.5), round(3.5))
print(int(9.99))
print(abs(-4))
print(divmod(17, 5))3.14 2 4 9 4 (3, 2)
round(2.5) gives 2 and round(3.5) gives 4. Python rounds exact halves to the nearest even number, a rule called banker's rounding that keeps totals from drifting upward.
In what order does Python do the math?
Parentheses first, then powers, then multiplication, division, floor division and modulo, then addition and subtraction. It is the order you learned in school, but two details trip people up. ** is worked out from the right, and Python does ** before a minus sign on its left.
print(2 + 3 * 4)
print((2 + 3) * 4)
print(2 ** 3 ** 2)
print(-2 ** 2)14 20 512 -4
2 ** 3 ** 2 is worked out from the right, as 2 to the power of 9. And -2 ** 2 is -(2 ** 2), so it is -4. Write (-2) ** 2 when you mean 4.
Common mistakes with numbers in Python
Watch for these four mistakes with Python numbers.
- Using
^for powers.2 ^ 3is 1, because^is bitwise XOR, an operation on binary digits that has nothing to do with powers. The power operator is**. - Expecting
/to give a whole number.8 / 2is4.0. Use//when you need a whole number. - ZeroDivisionError: division by zero.
/,//and%all raise it. Check the divisor first when it comes from user input. - Testing floats with
==. Round both sides, or compare the difference against a small tolerance.
print(2 ^ 3)
print(2 ** 3)1 8
print(10 / 0)Traceback (most recent call last):
File "main.py", line 1, in <module>
print(10 / 0)
~~~^~~
ZeroDivisionError: division by zeroExercise
Forty-seven pieces of candy are shared equally among five people. Complete the two print lines so the program prints how many each person gets, 9, and how many are left over, 2, using the operators from this lesson.
total = 47
people = 5
print()
print()
9 2
Show the solution
total = 47
people = 5
print(total // people)
print(total % people)
Quiz
This quiz has 5 questions. Pick an answer to see why it is right or wrong.
-
1What does this program print?
print(7 // 2, 7 % 2)Floor division rounds down to a whole number, and modulo gives what is left over. 2 goes into 7 three times with 1 remaining.
-
2What does this program print?
print(8 / 2)With int and float numbers, the / operator always returns a float. Python prints a float in its shortest form, so it is 4.0, not 4.00, and 8 // 2 would print 4.
-
3Which expression is 2 to the power of 5?
** is the power operator. ^ is bitwise XOR, an operation on binary digits, so 2 ^ 5 is 7.
-
4What does this program print?
print(0.1 + 0.2 == 0.3)0.1 + 0.2 is 0.30000000000000004 in binary floating point, so the comparison is False. Round first.
-
5What does n % 3 == 0 check?
% gives the remainder of a division. A remainder of 0 means the division was exact. n / 3 gives 0 only when n is 0, so that is a different test.