edubun
English

Numbers and arithmetic

In Python 3, 7 / 2 gives 3.5, 7 // 2 gives 3 and 7 % 2 gives the remainder 1.

Four divisions to predict before reading on

Cover the output below and write down what you expect each line to print. Most people get the first one right and at least one of the others wrong, and that wrong guess is where this unit starts.

print(7 / 2)
print(8 / 2)
print(7 // 2)
print(7 % 2)
Output
3.5
4.0
3
1

The second line surprises people. 8 / 2 is exactly 4, yet Python prints 4.0. The reason is that / always gives a float, a number with a decimal point, even when the division comes out even. If you want whole-number division, Python has a separate operator, //, called floor division. It divides and then rounds down to the whole number below. The partner operator % gives the remainder that is left over. Together they answer the primary-school question "7 divided by 2 is 3, remainder 1".

Three jumps of 2 fit into 7, with 1 left over: 7 // 2 is the number of jumps, 7 % 2 is what is left.

% turns up in more places than you would expect. A number is even when n % 2 is 0. The last digit of a number is n % 10. Turning 135 minutes into hours and minutes is 135 // 60 hours and 135 % 60 minutes.

minutes = 135
print(minutes // 60, "h", minutes % 60, "min")
print(2026 % 2, 2027 % 2)
print(4589 % 10)
Output
2 h 15 min
0 1
9

The other operators, and the order Python uses

OperatorMeaningExampleResult
+add7 + 29
-subtract7 - 25
*multiply7 * 214
/divide (float)7 / 23.5
//floor division7 // 23
%remainder7 % 21
**power7 ** 249

Python follows the same order as maths class: ** first, then *, /, // and %, then + and -, left to right within each group. Brackets come before everything, so when in doubt, add them. They cost nothing and make the intention plain to the next reader.

print(2 + 3 * 4)
print((2 + 3) * 4)
print(2 ** 10)
print(10_000 * 3)
Output
14
20
1024
30000

The last line shows a small help: Python lets you write underscores inside long numbers, so 10_000 is easier to read than 10000. They are ignored when the number is worked out and do not appear in the output. Commas cannot be used that way, because a comma already means "next value".

Decimals are stored in binary, so some answers come out a hair off

Try 0.1 + 0.2. The Python tutorial devotes a whole chapter to why the answer is 0.30000000000000004 rather than 0.3. Floats are stored in binary, and 0.1 has no exact binary form, just as 1/3 has no exact decimal form. The error is tiny, and round() hides it when you print. Be aware that round() rounds a value that sits exactly halfway to the even neighbour: the built-in functions page gives round(0.5) as 0 and round(1.5) as 2. For money in whole won or whole cents, keep amounts as int and avoid the problem entirely.

print(0.1 + 0.2)
print(round(0.1 + 0.2, 2))
print(round(0.5), round(1.5), round(2.5))
print(int(3.99), float(7))
Output
0.30000000000000004
0.3
0 2 2
3 7.0

input() always gives you a string

input() shows a prompt, waits for the user to type something and press Enter, and returns what they typed as a string. Even if they type 15, you get "15", text. To do arithmetic with it, convert it with int() or float(). int() on a float cuts off the decimal part rather than rounding, so int(3.99) is 3.

age = input("How old are you? ")
next_year = int(age) + 1
print("Next year you will be", next_year)
Output
How old are you? 14
Next year you will be 15
In the sample above, 14 is what the user typed. The program itself does not contain the number 14.
/Division that always gives a float: 8 / 2 is 4.0
//Floor division: 7 // 2 is 3
%Remainder: 7 % 2 is 1
**Power: 2 ** 3 is 8
int()Turns "15" or 3.9 into a whole number (15, 3)
input()Reads one line the user types, as a string
round(x, n)Rounds to n decimal places; halves go to the even side
  1. What does this print?
    print(9 / 3)
  2. What does this print?
    print(17 // 5, 17 % 5)
  3. What does this print?
    print(2 + 3 * 4 ** 2)
  4. What does this print?
    print(2 ** 3)
  5. What does this print?
    print(2 ^ 3)
    • 8
    • 6
    • 1
    • An error
  6. What does this print?
    print(int("20") + 5)
  7. What does this print?
    print("20" + "5")
  8. What does this print?
    print(int(7.9))
  9. What does this print?
    print(round(2.5))
  10. A roll costs 3,500 won and you have 10,000 won. What does this print?
    won = 10000
    price = 3500
    print(won // price, won % price)
  11. Which expression tells you whether n is even?
    • n / 2 == 0
    • n // 2 == 0
    • n % 2 == 0
    • n ** 2 == 0
Common mistakes
  • "2 ^ 3 is 8." In Python, ^ is a bitwise operator and 2 ^ 3 is 1. Powers use **, so 2 ** 3 is 8. The program runs without an error, which makes this mistake hard to spot.
  • "input() gives me a number if I type a number." It always gives a string. "14" + 1 stops with TypeError: can only concatenate str (not "int") to str. Convert first with int(input(...)).
  • "0.1 + 0.2 == 0.3 is True." It is False, because 0.1 + 0.2 is 0.30000000000000004. Compare rounded values, such as round(0.1 + 0.2, 2) == 0.3, or count in whole units (won, cents) with int.
Teacher's noteStart with the prediction exercise and let the 4.0 surprise do the work. Learners who know division with remainders from primary school pick up // and % fast once they are named that way. The float rounding error is worth showing once and not dwelling on; the important habit is to keep money as whole numbers. round(2.5) giving 2 contradicts the rounding taught in maths class, so expect a question, and point to the built-in functions documentation rather than arguing. The input() conversion error is the one learners hit most in their first projects.
Key points
  • / always gives a float; // gives the whole-number quotient; % gives the remainder.
  • ** is power; ^ is not.
  • Order: brackets, **, then * / // %, then + -.
  • 0.1 + 0.2 is 0.30000000000000004; keep money as int.
  • input() returns a string; convert with int() or float().
Sources for this course (6)
  • Python Software Foundation, The Python Tutorial for Python 3.12 (docs.python.org/3.12/tutorial: 2 Using the Python Interpreter, 3 An Informal Introduction to Python (numbers, text, lists), 4 More Control Flow Tools (if, for, range, break and continue, defining functions, default argument values, 4.10 coding style: 4-space indentation), 5 Data Structures (list methods, 5.5 dictionaries, insertion order), 8 Errors and Exceptions (syntax errors, exceptions, reading the traceback, handling exceptions), 15 Floating-Point Arithmetic (0.1 + 0.2))
  • Built-in Functions (docs.python.org/3.12/library/functions.html: print, input, len, int, float, str, type, round (round half to even: round(0.5) and round(-0.5) are 0, round(1.5) is 2), range, sum, min, max, sorted)
  • Built-in Types (str methods upper, lower, replace, strip; dict.get; list.append, pop, remove, sort returning None)
  • General Python FAQ (docs.python.org/3.12/faq/general.html: Guido van Rossum began Python in the 1989 Christmas holidays, posted it to USENET in February 1991, named it after Monty Python's Flying Circus)
  • Curriculum for the Korean pages: 교육부 고시 제2022-33호 [별책 10] 정보과 교육과정 (PDF read 2026-10-19: 중학교 정보 [9정03-05] 리스트 등 데이터를 순차적으로 저장하는 구조, [9정03-06] 논리 연산과 중첩 제어 구조, [9정03-07] 함수 활용과 디버거로 오류 수정, [9정03-08] 실생활 문제를 프로그래밍으로 해결, 적용 시 고려 사항 「학생의 수준을 고려하여 적합한 프로그래밍 언어를 선정」; 고등학교 정보 [12정03-04] 자료형, [12정03-05] 표준입출력, [12정03-07] 제어 구조)
  • Bug story only: Smithsonian National Museum of American History, Log Book With Computer Bug, 1947, Harvard Mark II, "first actual case of bug being found", probably not Hopper's log book (americanhistory.si.edu/collections/object/nmah_334663, read 2026-10-19)

Checked: 2026-10-19