Programming Search⌘K

Featureintro

Numbers

Also called Python as a calculator, Python arithmetic, int and float, floor division, underscore variable.

Python works as a calculator: + - * / ** and parentheses act on int and float values. The / operator always gives a float, // floors and % gives the remainder.

Language
Python

Synopsis

print(2 + 2, 50 - 5*6, (50 - 5*6) / 4, 8 / 5)
print(17 / 3, 17 // 3, 17 % 3, 5 * 3 + 2)
print(5 ** 2, 2 ** 7, 4 * 3.75 - 1)
Output
4 20 5.0 1.6
5.666666666666667 5 2 17
25 128 14.0

These are the examples of the tutorial. Whole numbers are int values, and numbers with a fractional part are float values.

Semantics

r1. The operators +, -, * and / do arithmetic, and parentheses group. The integer numbers "have type int", and the numbers with a fractional part "have type float".1

r2. "Division (/) always returns a float." The // operator does floor division and gives an integer result for integer operands, and % gives the remainder.1 Example: result-types.

r3. ** computes powers. It binds more tightly than unary minus, so -3**2 is -(3**2), which is -9; write (-3)**2 to get 9.1 Example: power-and-minus.

r4. With mixed operand types, the integer operand is converted to floating point.1 Built-in Types states the general rule: when a binary arithmetic operator has operands of different built-in numeric types, the narrower operand is widened to the type of the other. An int combined with a float is converted to a float.2

r5. In interactive mode, the last printed expression is assigned to the variable _. The tutorial says to treat it as read-only, because an assignment to _ makes "an independent local variable with the same name masking the built-in variable".1 Example: underscore.

Examples

The result type of each operator (rule r2). 6 / 3 is a float even though the division is exact.

print(type(17 // 3).__name__, type(17 / 3).__name__, type(6 / 3).__name__)
print(type(7 // 2.0).__name__, 7 // 2.0)
Output
int float float
float 3.0

The 'single' mode of compile() runs code as interactive input, so it shows the _ variable at work (rule r5).

import builtins

exec(compile("12.5 / 100 * 100.50\n", "<calc>", "single"))
exec(compile("round(_, 2)\n", "<calc>", "single"))
print(builtins._)
Output
12.5625
12.56
12.56

Pitfalls

Unary minus and powers

** is applied before the unary minus (rule r3).

This code runs but gives a wrong answer on purpose.

print(-3 ** 2)
print((-3) ** 2)
Output
-9
9

Assigning to _

After an assignment to _, the interactive value is hidden by the new variable (rule r5).

This code runs but gives a wrong answer on purpose.

_ = "my own value"
exec(compile("6 * 7\n", "<calc>", "single"))
print(_)
Output
42
my own value

Comparison

In Go, arithmetic operators "yield a result of the same type as the first operand", and for binary operators "the operand types must be identical unless the operation involves shifts or untyped constants".3 So in Go 8 / 5 is the integer 1, and 4 * 3.75 with an int variable does not compile. Python gives 1.6 and converts the int operand to float (rules r2 and r4).

References

  1. The Python Tutorial, Python 3.15, 3.1.1. Numbers, and footnote 1
  2. Built-in Types, Python 3.15 documentation, Numeric Types, int, float, complex
  3. The Go Programming Language Specification, Arithmetic operators

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source