Numbers and operators basics

Basic python

Numbers and operators basics

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 #

OperationSymbolExampleResult
Addition+7 + 310
Subtraction-7 - 34
Multiplication*7 * 321
Division/7 / 32.3333333333333335
Floor division//7 // 32
Remainder%7 % 31
Exponentiation**2 ** 101024

/ 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 #

  1. Calculate the area of a circle with radius r = 5 using pi = 3.14159.
  2. Assuming one year has 365 days, think about what 365 % 7 represents.
  3. 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.