Python cmath.inf Constant
The Python cmath.inf constant defines positive infinity. This constant is a predefined value, that represents a number greater than any finite number.
We can use float('inf') as an integer to represent it as a positive infinity. When we set a variable, it is always greater or smaller than the other numbers.
This constant can only be used with floating-point numbers in Python. We can also use infinity values in Python arrays and other data structures like lists, sets, and dictionaries.
Syntax
Following is the basic syntax of the Python cmath.inf Constant −
cmath.inf
Return Value
This constant returns the value of a floating-point infinity.
Example 1
In the below example, we are representing positive and negative infinity using cmath.inf function.
import cmath
pos_infinity = float('inf')
print(pos_infinity)
neg_infinity = float('-inf')
print(neg_infinity)
Output
The result is obtained is as follows −
inf -inf
Example 2
Now, we are using cmath.inf constant to find the minimum values in a list of numbers. We are using the "minimum_value" variables with positive infinity, and then these values iterate through the list.
import cmath
x = [10, 15, 20, 25, 30, 35]
minimum_value = cmath.inf
for x in x:
if x < minimum_value:
minimum_value = x
print("The minimum value in the list is:", minimum_value)
Output
Following is the output of the above code −
The minimum value in the list is: 10
Example 3
Here, we are calculating arithmetic operations using cmath.inf.
import cmath positive_infinity = -200 print(positive_infinity + 2000) negative_infinity = 100 print(negative_infinity - 1000) print(positive_infinity * 1000) print(1 / negative_infinity)
Output
The output obtained is as follows −
1800 -900 -200000 0.01
Example 4
In the following example, dividing a finite number by infinite results in zero.
import cmath x = 102 result = x/cmath.inf print(result)
Output
We will get the output as follows −
0.0
python_modules.htm