After you learn about Python Strings in our previous guide, it’s now time to look at another important data type – Python Numbers.
Numbers are represented by numerical data types in Python. These data types include integers, floating-point numbers, and complex numbers.
We will take a closer look at the types of Python numbers along with detailed examples in this guide.
What are the types of Python Numbers?
There are three data types covered by Python Numbers. These include;
- Floating-point numbers
- Complex numbers
You can create Python variables with these numerical data types by simply assigning specific types of value.
Python can automatically figure out the data type based on the characteristic of the data assigned to the variable.
Here’s an example of each of the numerical data types assigned to sample variables.
In the next section, let’s look at what kind of values these numerical data types can hold with some real examples.
Integers in Python – int Data Type
Integers are whole numbers that can be positive, negative, or zero. In Python, integers are represented by the int data type.
Integers in Python can accommodate unlimited lengths. This means in Integers, the number can be of any length as long as it is positive or negative and without decimals.
>>> 0 0 >>> 500 500 >>> -100 -100 >>> 1234567890 1234567890
Floats in Python – float Data Type
Floats are decimal numbers or numbers that contain a fractional component.
In Python, the float data type represents a floating-point number. A floating-point number is simply a number with a decimal point.
For example, the number 2.5 is a floating-point number. The float data type can hold values that are larger than those that can be stored in a standard 32-bit integer data type.
In addition, the float data type can represent values that are smaller than integers.
However, the accuracy of a float value is limited to six decimal places. When storing extremely large or small values, it is better to use the Python decimal module instead of the float data type. The decimal module provides support for arbitrary-precision decimal numbers.
Floats in Python are usually used for numerical analysis or scientific calculations.
Complex Numbers in Python – complex Data Type
In Python, Complex numbers are represented by the complex data type and consist of two parts: real and imaginary components.
Both of these parts in the Complex numbers are floating point numbers.
c = complex("8.j")
Numerical Data Type Conversion
Similar to converting other data types, you can convert numerical data types into one another by Casting.
Converting Between Numeric Data Types
In Python, data type conversion is achieved by using built-in functions. The int() and float() methods can be used to convert integers to floats and vice versa.
Casting Between Strings and Numeric Data Types
You can also cast Numeric data types to convert them into strings and vice-versa.
The str() method can be used to convert data types such as integers and floats to strings.
Similarly, to convert a string to a number, the int() or float() method can be used. However, if the value inside the string cannot be converted to a number, a ValueError will be raised.
For example, the following code will raise a ValueError: int(“a”). In order to convert a string to a number, the string must contain only numeric characters.
example = int("a")
Arithmetic Operations with Numbers in Python
Python supports a wide range of arithmetic operations. These include basic mathematical operations like addition, subtraction, multiplication, division, and modulus. Additionally, Python also supports advanced arithmetic operators like floor division and exponentiation.
Addition in Python
In Python, you can add numbers by using the plus (+) operator. It behaves exactly like how you carry addition in Maths. The code below shows how to add two integers in Python.
#Addition sum = 5 + 7 print(sum) #Prints out 12
In the example above, + is an operator and the number on each side are called operands.
Subtraction in Python
In addition to addition, you can also subtract numbers in Python by using the minus (-) operator. See the example code below showing how to subtract two integers in Python.
Multiplication in Python
You can multiply numbers in Python by using the asterisk (*) operator. Here’s how to multiply two integers in Python;
Division in Python
You can divide numbers in Python by using the forward slash (/) operator. See how you can divide two integers in Python with the examples below;
Modulus in Python
The modulus operation is used to find the remainder of a division. In Python, you can take the modulus of two numbers by using the percent (%) operator. Learn how to find the modulus of two integers in Python with the following example.
Exponentiation in Python
Exponentiation is used to calculate the power of a number. In Python, you can calculate the power of a number by using the double asterisk (**) operator.
See the code below for how to calculate the power of a number in Python.
Floor Division in Python
In addition to division, there’s another type of division called floor division. Floor division is used to divide numbers and round down to the nearest integer.
You can perform floor division by using the double slash (//) operator. Here’s how to perform floor division on two integers.
You can learn more about Python operators in detail in another guide.
Mathematical Functions in Python
Apart from arithmetic operations, Python also supports a range of mathematical functions. These include trigonometric functions, logarithmic functions, and exponential functions.
The math module is used to access these mathematical functions in Python.
Some of the popular mathematical functions supported by the math module include;
You need to add the following line of code to access the above math functions;
Summary: Numbers in Python
To summarise, Python has built-in numeric data types including integers, floats, and complex numbers.
Python supports a wide range of numerical data types and arithmetic operations. You can convert numbers from one type to another by using built-in functions like int() and float().
You can also use Python for advanced arithmetic operators like floor division, exponentiation, and modulus. Additionally, the math module provides access to a range of mathematical functions in Python.
Continue learning about other data types in Python in these guides.