Numbers are fundamental to programming. They let you model real‑world quantities like money, scores, measurements, and more. Python handles numerical data with ease and provides powerful tools for mathematical operations. In this chapter, you’ll learn about Python’s numeric types and how to perform calculations with them.
In this chapter you will learn:
Python supports three main numeric types: integers, floating‑point numbers, and complex numbers. For most programming tasks, you’ll work primarily with integers and floats.
Integers are whole numbers without decimal points. They can be positive, negative, or zero:
There’s no limit to how large an integer can be in modern Python — you can store enormous values without worrying about overflow:
Floating‑point numbers (also called floats) represent decimal values. They are used for measurements, currency, and other values that need precision beyond whole numbers:
You can also use scientific notation for very large or very small numbers:
Float arithmetic can sometimes produce small rounding errors due to how computers represent decimal numbers in binary. For financial applications requiring perfect precision, use the decimal module.
Python also supports complex numbers for mathematical and scientific computing. Complex numbers have a real part and an imaginary part:
Use the type() function to check what kind of number you’re working with:
Python supports all basic arithmetic operations you’d expect from a calculator plus some additional ones that prove useful in programming.
Here are Python’s arithmetic operators:
| Operator | Description | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 3 | 13 |
| - | Subtraction | 10 - 3 | 7 |
| * | Multiplication | 10 * 3 | 30 |
| / | Division | 10 / 3 | 3.333... |
| // | Floor division | 10 // 3 | 3 |
| % | Modulus (remainder) | 10 % 3 | 1 |
| ** | Exponentiation | 2 ** 3 | 8 |
When you mix integers and floats in an expression, Python automatically converts the integers to floats before performing the operation:
Python’s built‑in math module provides many advanced mathematical functions and constants. To use it, you need to import it first:
Here are some of the most useful functions in the math module:
Angles are expressed in radians in the math module, not degrees. Use math.radians() to convert degrees to radians before using trig functions.
Sometimes you need to convert between different numeric types. Python provides built‑in functions for this purpose.
Use int() to convert floats or strings representing whole numbers to integers. The fractional part is truncated (not rounded):
Use float() to convert integers or numeric strings to floating‑point numbers:
When converting from float to int, you lose decimal precision. You can round first if you want to preserve the nearest whole number:
Write a program called multiply.py that asks the user for two numbers and then displays the product of those two numbers.
The program should:
When working with floating-point numbers, you may encounter some unexpected behavior due to how computers represent decimal numbers in binary.
It is well known that some decimal numbers cannot be represented exactly in binary. For example, dividing 1 by 3 gives a repeating decimal in decimal (0.333...), but in binary, dividing 1 by 10 gives a repeating binary fraction.
This can lead to what appears to be incorrect calculations:
Python, like most programming languages, uses IEEE 754 floating-point arithmetic, which can introduce small rounding errors in calculations.
When precision matters, you can use the round() function to round to a specific number of decimal places:
For applications requiring exact decimal representation, such as financial calculations, use the decimal module: