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.
- int: The int class holds signed integers of non-limited length.
- float: The float class holds floating decimal points and is accurate up to 15 decimal places.
- complex: The complex class holds complex numbers.
The example below offers a summary of each:
Example
# an overview on python numbers
# initializing variables
p = 6 # integer
q = 8.3 # floating-point number
r = 2 - 5j # complex number
# printing types
print(type(p))
print(type(q))
print(type(r))
Output:
<class 'int'>
<class 'float'>
<class 'complex'>
Explanation:
In the example provided, we have established three variables, each containing distinct numerical values, and utilized the type function to display their data types. Consequently, each of these numbers is categorized under different classes in Python.
Python int (Integer)
An Integer is defined as a complete number that can be positive, negative, or zero, lacking any decimal or fractional components. Examples of integers include 8, -5, 0, 12, -512, among others.
In Python, the int class serves the purpose of representing integer values. Notably, there are no restrictions on the size of an integer in Python, allowing for integers of arbitrary length.
Let us examine a straightforward illustration demonstrating how to declare an integer in Python.
Example
# python program to create integers
# initializing variables
p = 6 # positive integer
q = -7 # negative integer
r = 0 # zero (considered as integer)
# printing variable's value and type
print(p, "->", type(p))
print(q, "->", type(q))
print(r, "->", type(r))
Output:
6 -> <class 'int'>
-7 -> <class 'int'>
0 -> <class 'int'>
Explanation:
In this illustration, we have established a variety of numbers, which encompass positive values, negative values, and zero. Subsequently, we have displayed their respective types. Consequently, all of these assigned numerical values are categorized under the int class.
Arithmetic Operations on int
In Python, we are able to execute a range of arithmetic operations, including but not limited to addition, subtraction, and multiplication, on the int data type.
Let us look at the following example:
Example
# python program to perform arithmetic operations on integers
# initializing variables
p = 8
q = 3
# performing arithmetic operations
print(p,"+", q, "=", p + q) # addition
print(p,"-", q, "=", p - q) # subtraction
print(p,"*", q, "=", p * q) # multiplication
print(p,"/", q, "=", p / q) # division (returns a float value)
print(p,"//", q, "=", p // q) # floor division
print(p,"%", q, "=", p % q) # modulus
print(p,"**", q, "=", p ** q) # exponential
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 illustration, we have executed a range of arithmetic operations including addition, subtraction, multiplication, division, floor division, modulus, and exponentiation on integer values.
Python float (Floating-point Numbers)
A floating-point number is defined as a numerical value that may include a decimal point or be expressed in scientific notation. For instance, examples include 7.435, 3.62, 7e3, among others.
In Python, there exists a class called float that is utilized to represent decimal values which may be positive, negative, or zero. This class also accommodates scientific notation (for example, 5e4 is equivalent to 5000.0).
The float class adheres to the IEEE 754 standard for floating-point representation, offering a precision capacity of up to 15 decimal digits. Float values can be specified directly by inputting numbers that include decimal points or by executing operations such as division on integers.
The subsequent example illustrates the method of generating floating-point numbers in Python.
Example
# python program to create floating-point numbers
# initializing variables
p = 5.85253 # positive floating-point number
q = -7.23 # negative floating-point number
r = 0.0 # zero (as floating-point number)
s = 3e6 # scientific notation
# printing variable's value and type
print(p, "->", type(p))
print(q, "->", type(q))
print(r, "->", type(r))
print(s, "->", type(s))
Output:
5.85253 -> <class 'float'>
-7.23 -> <class 'float'>
0.0 -> <class 'float'>
3000000.0 -> <class 'float'>
Explanation:
In the preceding example, we have established multiple floating-point values and displayed their corresponding data types. Consequently, all the variables that have been initialized are categorized under the Python float class.
Arithmetic Operations on float
In a manner akin to integer operations, we can execute a range of arithmetic functions, including addition, subtraction, multiplication, and others, using the float data type.
Let us take a look at the following example:
Example
# python program to perform arithmetic operations on floating-point numbers
# initializing variables
p = 8.4
q = 2.9
# performing arithmetic operations
print(p,"+", q, "=", p + q) # addition
print(p,"-", q, "=", p - q) # subtraction
print(p,"*", q, "=", p * q) # multiplication
print(p,"/", q, "=", p / q) # division
print(p,"//", q, "=", p // q) # floor division
print(p,"%", q, "=", p % q) # modulus
print(p,"**", q, "=", p ** q) # exponential
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 preceding example, we conducted a range of arithmetic operations, including addition, subtraction, multiplication, division, floor division, modulus, and exponentiation, all utilizing floating-point numbers.
Python complex (Complex Numbers)
A Complex Number is defined as a number that includes both a real component and an imaginary component. It is expressed in the following format:
a + bj
where:
- a = It is the real It can either a float or integer.
- b = It is the imaginary It can either a float or integer.
- j = It represents √(-1) . It is an imaginary unit used in Python.
As an illustration, the expression 3 + 4j represents a complex number, wherein 3 signifies the real component, while 4 multiplied by j denotes the imaginary component.
In Python, complex numbers are represented by a class named complex.
Consider the following example to create a complex number in Python.
Example
# python program to create complex numbers
# initializing variables
p = 4 + 7j
q = 6 - 2j
r = 3j
# printing variable's value and type
print(p, "->", type(p))
print(q, "->", type(q))
print(r, "->", type(r))
Output:
(4+7j) -> <class 'complex'>
(6-2j) -> <class 'complex'>
3j -> <class 'complex'>
Explanation:
In this illustration, we have instantiated a few complex numbers and utilized the type function to determine their types. Consequently, these numbers are categorized under the complex class.
Arithmetic Operations on complex
Python enables us to carry out various arithmetic operations such as addition, subtraction, multiplication, and others on complex numbers. Additionally, we can access the real and imaginary components of a complex number through the real and imag attributes, respectively.
Let us take a look at the following example:
Example
# python program to perform arithmetic operations on floating-point numbers
# initializing variables
p = 12 + 8j
q = 4 - 3j
# performing arithmetic operations
print(p,"+", q, "=", p + q) # addition
print(p,"-", q, "=", p - q) # subtraction
print(p,"*", q, "=", p * q) # multiplication
print(p,"/", q, "=", p / q) # division
print(p,"**", 3, "=", p ** 3) # exponential
print("Real part of", p, "=", p.real) # real part
print("Imaginary part of", p, "=", p.imag) # imaginary part
print("Conjugate of", p, "=", p.conjugate()) # conjugate
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 preceding illustration, we executed a range of arithmetic operations including addition, subtraction, multiplication, division, and exponentiation on complex numbers.
In addition, we have utilized the attributes such as real and imag from the complex class to retrieve the real and imaginary components of the given variable. Furthermore, we have invoked the conjugate method to obtain the conjugate of the specified complex number.
Type Conversion in Python
Python is classified as a dynamically typed programming language, which means that it does not require the explicit declaration of variable data types. Nonetheless, Python provides the capability to transform one data type into another.
Type conversion refers to the procedure in programming where one numerical type is transformed into another. In Python, there are mainly two methods available for changing the type of a variable.
Implicit Type Conversion (Automatically)
Python automatically promotes smaller data types to larger ones when necessary. Consider the example below:
Example
# implicit type conversion in python
# defining variables
a = 13 # int
b = 4.6 # float
c = 5 # int
sum = a + b # int + float -> float
print(a, "+", b, "=",sum)
print(type(sum))
quot = a / c # int / int -> float
print(a, "/", c, "=", quot)
print(type(quot))
Output:
13 + 4.6 = 17.6
<class 'float'>
13 / 5 = 2.6
<class 'float'>
Explanation:
In this scenario, it is apparent that executing operations such as addition and subtraction will implicitly convert integers to float if at least one of the operands is a float. Conversely, when it comes to division, the result will be a float irrespective of whether both operands are integers.
Explicit Type Conversion (Manually)
Developers utilize Python's native functions such as int, float, complex, among others, to perform manual conversions between different data types. Below is a straightforward illustration of explicit type conversion in Python:
Example
# explicit type conversion in python
# converting float to int
var_1 = int(7.2)
print(var_1, "->", type(var_1))
# converting int to float
var_2 = float(6)
print(var_2, "->", type(var_2))
# converting int to complex
var_3 = complex(8)
print(var_3, "->", type(var_3))
Output:
7 -> <class 'int'>
6.0 -> <class 'float'>
(8+0j) -> <class 'complex'>
Explanation:
In this instance, when transforming a float into an integer, the fractional component of the number is omitted. Likewise, when converting an integer into a float, a .0 is appended to the number. Additionally, when converting an integer to a complex number, 0j is included with the specified integer.
For further information regarding type conversion in Python, please check out the section on Type Casting in Python.
Python random Module
Python includes a module known as random, which enables the generation of random numbers or the selection of a random item from an iterable. To utilize the random module, it is necessary to import it using the import statement.
Let's examine a straightforward example to comprehend how the random module operates:
Example
# importing the required module
import random
# printing a random number between 5 and 25
print("Random Number between 5 and 25:", random.randrange(5, 25))
# given list
list_one = [12, 15, 4, 8, 13]
print("Given List:", list_one)
# getting random item from list_one
print("Random Item from the List:", random.choice(list_one))
# shuffling list_one
random.shuffle(list_one)
print("Shuffling the List:", list_one)
# printing random element
print("Printing Random Element:", random.random())
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 preceding example, we observe multiple functionalities of the random module, including generating a random number within designated ranges, choosing a random element from a provided list, randomizing the order of the list, and displaying a random item.
For additional information regarding the random module, please refer to the Python Random Module.
Python math Module
In Python, the math module provides a variety of functions that enable us to perform a range of mathematical computations, including trigonometric functions, logarithmic calculations, as well as tasks related to probability and statistics.
Below is an illustration demonstrating the functionality of the math module in Python:
Example
# importing the required module
import math
# using different mathematically functions
print("Value of Pi =", math.pi) # pi
print("cos(pi) =", math.cos(math.pi)) # cosine of pi
print("e^5 =", math.exp(5)) # e^5
print("log10(10000) =", math.log10(10000)) # log 10000
print("sinh(1/2) =", math.sinh(1/2)) # sinh of 1/2
print("7! =", math.factorial(7)) # factorial of 7
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 illustration, we have utilized various attributes and functions from the math module. This includes displaying the value of pi, determining the cosine of pi, calculating e raised to the power of 5, finding the logarithm base 10 of 1000, evaluating the hyperbolic sine of 1/2, and computing the factorial of 7.
For further information regarding the math module along with its functions and attributes, please refer to the Python math module documentation.
Conclusion
In Python, numerical types serve as the foundation for mathematical computations, encompassing integers, floating-point numbers, and complex numbers. These types are essential for crafting efficient code, managing data, and addressing practical challenges effectively. Grasping the nuances of these data types is crucial for any programmer.
Python Numbers MCQs
- Which of the following is a valid numeric type in Python?
- float
- complex
- All of the above
- What will be the output of type(7.28)?
- <class 'int'>
- <class 'float'>
- <class 'double'>
- <class 'complex'>
- What is the type of 5 + 3j in Python?
- A complex number
- An integer
- A string
- A float
- What function is used to convert a string to a float in Python?
- int
- str
- complex
- float
- What will be the output of type(6/3) in Python?
- complex
- float
- string