In Python, Numbers are a fundamental data type used to represent and manipulate numerical values. Python has support for three types of numbers, including integers, floating-point numbers, and complex numbers. These numbers are defined as int, float, and complex classes in Python.
The following example provides an overview of each:
Output:
<class 'int'> <class 'float'> <class 'complex'>
Explanation:
In the above example, we have defined three variables consisting of different numerical values and printed their types using the type() function. As a result, all these numbers belong to different classes in Python.
An Integer is said to be a whole number (positive, negative, or zero) without any decimal or fractional part. For example, 8, -5, 0, 12, -512, and more.
Python int is a class used to represent integers. In Python, there is no limit to the length of an integer.
Let us take a look at a simple example showing how to initialize an integer in Python.
Output:
6 -> <class 'int'> -7 -> <class 'int'> 0 -> <class 'int'>
Explanation:
In this example, we have defined several numbers, including positive, negative, and zero. We have then printed their types. As a result, all these initialized numerical values belong to the int class.
In Python, we can perform various arithmetic operations, such as addition, subtraction, multiplication, and more, on the int type.
Let us look at the following example:
Output:
8 + 3 = 11 8 - 3 = 5 8 * 3 = 24 8 / 3 = 2.6666666666666665 8 // 3 = 2 8 % 3 = 2 8 ** 3 = 512
Explanation:
In this example, we have performed various arithmetic operations like addition, subtraction, multiplication, division, floor division, modulus, and exponential on integers.
A Floating-point number is said to be a number that can have a decimal point or be written in scientific notation. For example, 7.435, 3.62, 7e3, and more.
In Python, a class known as float is used for representing decimal numbers which can either be positive, negative or zero. It has support for scientific notations (e.g., 5e4 = 5000.0).
The float class follows the IEEE 754 floating-point standard and has a precision limit of up to 15 decimal points. We can define the float values directly by entering numbers with decimal points or performing operations like division on integers.
The following is an example showing the way of creating floating-point numbers in Python.
Output:
5.85253 -> <class 'float'> -7.23 -> <class 'float'> 0.0 -> <class 'float'> 3000000.0 -> <class 'float'>
Explanation:
In the above example, we have defined several floating-point numbers and printed their data types. As a result, all the initialized variables belong to the Python float class.
Similar to the operations on integers, we can also perform various arithmetic operations, such as addition, subtraction, multiplication, and more, on the float data type.
Let us take a look at the following example:
Output:
8.4 + 2.9 = 11.3 8.4 - 2.9 = 5.5 8.4 * 2.9 = 24.36 8.4 / 2.9 = 2.896551724137931 8.4 // 2.9 = 2.0 8.4 % 2.9 = 2.6000000000000005 8.4 ** 2.9 = 479.0820834676715
Explanation:
In the above example, we have performed various arithmetic operations like addition, subtraction, multiplication, division, floor division, modulus, and exponential on floating-point numbers.
A Complex Number is a number consisting of a real part and an imaginary part. It is written in the form:
where:
For example, 3 + 4j is a complex number where 3 is the real part, and 4 multiplied by j is an imaginary part.
In Python, the complex numbers are stored in a class called complex.
Let us take an example to initialize a complex number in Python.
Output:
(4+7j) -> <class 'complex'> (6-2j) -> <class 'complex'> 3j -> <class 'complex'>
Explanation:
In this example, we have created some complex numbers and called the type() function to return their types. As a result, these numbers belong to the complex class.
Python allows us to perform different arithmetic operations like addition, subtraction, multiplication and more on the complex type. We can also get the real and imaginary parts of the complex number using the real and imag attributes.
Let us take a look at the following example:
Output:
(12+8j) + (4-3j) = (16+5j) (12+8j) - (4-3j) = (8+11j) (12+8j) * (4-3j) = (72-4j) (12+8j) / (4-3j) = (0.96+2.72j) (12+8j) ** 3 = (-576+2944j) Real part of (12+8j) = 12.0 Imaginary part of (12+8j) = 8.0 Conjugate of (12+8j) = (12-8j)
Explanation:
In the above example, we have performed various arithmetic operations like addition, subtraction, multiplication, division, and exponential on complex numbers.
We have also used the attributes like real and imag of the complex class to return the real and imaginary parts of the specified variable. We have also called the conjugate() method to return the conjugate of the specified complex number.
Python is a dynamically typed language, allowing us not to define the data types of the variables explicitly. However, Python offers us the accessibility to convert one data type into another.
Type conversion is the process in programming, to convert one type of number into another. Python primarily offers two ways to convert the type of a variable.
Python converts smaller data types to larger ones automatically. Let us take a look at the following example:
Output:
13 + 4.6 = 17.6 <class 'float'> 13 / 5 = 2.6 <class 'float'>
Explanation:
Here, we can observe that the performing operations like addition and subtraction convert the integers to float implicitly if one of the operands is float. However, in the case of division, the return value is float even when both the operands are integers.
Programmers use Python's built-in functions like int(), float(), complex() and more, to manually convert one data type into another. Here is a simple example of explicit type conversion in Python:
Output:
7 -> <class 'int'> 6.0 -> <class 'float'> (8+0j) -> <class 'complex'>
Explanation:
In this example, while converting from float to integer, the decimal part from the number is removed. Similarly, when converting an integer to float, .0 is post-fixed to the number. And 0j is added to the specified number in case of converting an integer to complex.
To learn more about type conversion in Python, visit Type Casting in Python.
Python provides a module called random that allows us to generate random numbers or to select a random element from an iterator. To work with the random module, we need to import it using the import statement.
Let us see a simple example to understand the working of the random module:
Output:
Random Number between 5 and 25: 21 Given List: [12, 15, 4, 8, 13] Random Item from the List: 13 Shuffling the List: [8, 4, 13, 12, 15] Printing Random Element: 0.488618511415422
Explanation:
In the above example, we can see various functions of the random module, such as returning a random number between specified ranges, selecting a random item from the given list, shuffling the list and printing a random element.
To learn more about the random module, visit Python Random Module.
In Python, we can use the math module in order to carry out different mathematical operations, like trigonometry, logarithms, probability, and statistics.
Here is an example showing the working of the math module in Python:
Output:
Value of Pi = 3.141592653589793 cos(pi) = -1.0 e^5 = 148.4131591025766 log10(10000) = 4.0 sinh(1/2) = 0.5210953054937474 7! = 5040
Explanation:
In this example, we have used the different attributes and functions of the math module, such as printing the value of pi, calculating the cosine of pi, e to the power of 5, the log to the base 10 of 1000, sinh 1/2, and factorial of 7.
To learn more about the math module and its functions and attributes, visit the Python math module.
Python numbers act as the backbone of mathematical operations, supporting integers, floats, and complex types, forming the basis of mathematical operations. Understanding these data types is key to writing efficient code, handling data, and solving real-world problems.
We request you to subscribe our newsletter for upcoming updates.