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 3.14 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 some cases, you may need to convert a number from one data type to another. For example, you may have a floating-point number that you need to convert to an integer.
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.
Arithmetic operations in Python behave exactly like in real maths, and the only difference is in the arithmetic symbols that are used.
Here’re the main arithmetic operations and their respective symbols used in Python.
|Python Arithmetic Operations||Usage||Symbol used|
|Addition||To add values on either side of the operator||+|
|Subtraction||To subtract the value on the right of the operator from the value on the left.||–|
|Multiplication||To multiply values on either side of the operator.||*|
|Division||To divide the value on the left of the operator by the value on the right.||/|
|Modulus||To divide the value on the left of the operator by the value on the right and returns the remainder.||%|
|Exponent||To perform exponential calculation, i.e. calculate the answer of the value on the left to the power of the value on the right.||**|
We will briefly look at each Python arithmetic operation with simple examples below.
Here are a few things to keep in mind when using operators in Python;
- In carrying out arithmetic operations, the symbol used is an operator, and the number on each side are called operands.
- You should surround the operators with a space on either side (as per the PEP 8 recommendation)
- The (-) operator is also used to denote negative numbers.
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
Adding two integers, like in the example above, results in an integer. You can also add an integer with a float.
#Addition sum = 5 + 7.1 print(sum) #Prints out 12.1
As you can see in the example above, you will get a float result when adding an integer to a float.
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.
#Subtraction in Python subtraction = 7 - 5 print(subtraction) #Prints out 2
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 the division, there’s another type of division called floor division. Floor division is used to divide numbers and round them 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, which are already built-in and can be accessed using mathematical 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;
|math.floor()||To Round a number down||print(math.floor(10.1111)) will print 10.0.|
|math.ceil()||To Round a number up||print(math.ceil(10.4444)) will print 11.0|
|math.trunc()||To Cut off the decimal part of the float||print(math.trunc(10.2222)) will print 10|
|math.sqrt()||To Find the square root of a number||print(math.sqrt(4)) will print 2.0|
|math.pi()||To Return the value for pi where pi is the number used to calculate the area of a circle||print(math.pi) will print 3.141592653589793|
You need to add the following line of code to access the above math functions;
Summary: Numbers in Python
To summarise, Python has three 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.