This page reviews Python’s numeric types and the most common operators. Follow along in the REPL or a notebook.
Integers and floating-point numbers #
Python provides integers (int) and floating-point numbers (float). You can use them by writing literals.
42 # integer
3.14 # floating-point number
1_000_000 # underscores make large numbers easier to read
Use
type(value)to check the data type. Example:type(42)→<class 'int'>.
Arithmetic and exponentiation #
| Operation | Symbol | Example | Result |
|---|---|---|---|
| Addition | + | 7 + 3 | 10 |
| Subtraction | - | 7 - 3 | 4 |
| Multiplication | * | 7 * 3 | 21 |
| Division | / | 7 / 3 | 2.3333333333333335 |
| Floor division | // | 7 // 3 | 2 |
| Remainder | % | 7 % 3 | 1 |
| Exponentiation | ** | 2 ** 10 | 1024 |
/always returns a float. Use//when you want the integer result of division.
Operator precedence #
Just like in arithmetic, multiplication and division happen before addition and subtraction. Parentheses make the order explicit.
8 + 4 * 2 # 16
(8 + 4) * 2 # 24
Parentheses improve readability—use them generously in longer expressions.
Helpful built-in functions #
Python includes built-in functions for numeric work.
abs(-3) # absolute value => 3
round(3.14159, 2) # round to 2 decimal places => 3.14
pow(2, 8) # exponentiation => 256 (same as 2 ** 8)
Try it yourself #
- Calculate the area of a circle with radius
r = 5usingpi = 3.14159. - Assuming one year has 365 days, think about what
365 % 7represents. - Investigate why
round(2.675, 2)results in 2.67 and note your findings.
参考リンク
Next you’ll learn how to store results in variables and choose good names.