# Python infinity

## Python infinity

As ironic as it may seem infinity is defined as an undefined number that can either be a positive or negative value. All arithmetic operations performed on an infinite value always lead to an infinite number, say it be sum, subtraction, multiplication, or any other operation.
In the world of computer science, infinity is generally used to measure performance and optimize algorithms that perform computations on a large scale application.

Representing infinity as an Integer in python
The concept of representing infinity as an integer violates the definition of infinity itself. As of 2020, there is no such way to represent infinity as an integer in any programming language so far. But in python, as it is a dynamic language, float values can be used to represent an infinite integer. One can use float(‘inf’) as an integer to represent it as infinity. Below is the list of ways one can represent infinity in Python.

## 1. Using float(‘inf’) and float(‘-inf’):

As infinity can be both positive and negative they can be represented as a float(‘inf’) and float(‘-inf’) respectively. The below code shows the implementation of the above-discussed content:

 `# Defining a positive infinite integer` `positive_infinity ``=` `float``(``'inf'``)` `print``(``'Positive Infinity: '``, positive_infinity)` `# Defining a negative infinite integer` `negative_infinity ``=` `float``(``'-inf'``)` `print``(``'Negative Infinity: '``, negative_infinity)`

Output:

```Positive Infinity:  inf
Negative Infinity:  -inf```

## 2. using Python’s math module:

Python’s math module can also be used for representing infinite integers. The below code shows how it is done:

 `import` `math` `# Defining a positive infinite integer` `positive_infinity ``=` `math.inf` `print``(``'Positive Infinity: '``, positive_infinity)` `# Defining a negative infinite integer` `negative_infinity ``=` `-``math.inf` `print``(``'Negative Infinity: '``, negative_infinity)`

Output:

```Positive Infinity:  inf
Negative Infinity:  -inf```

## 3. using Python’s decimal module:

Python’s decimal module can also be used for representing infinite float values.
It is used as Decimal(‘Infinity’) for positive and Decimal(‘-Infinity’) for negative infinite value.

The below code shows its implementation:

 `from` `decimal ``import` `Decimal` `# Defining a positive infinite integer` `positive_infinity ``=` `Decimal(``'Infinity'``)` `print``(``'Positive Infinity: '``, positive_infinity)` `# Defining a negative infinite integer` `negative_infinity ``=` `Decimal(``'-Infinity'``)` `print``(``'Negative Infinity: '``, negative_infinity)`

Output:

```Positive Infinity:  Infinity
Negative Infinity:  -Infinity```

## 4. using Python’s Numpy library:

Python’s Numpy module can also be used for representing infinite values. It is used as np.inf for positive and -np.inf for negative infinite value. The use of Numpy library for representing an infinite value is shown in the code below:

 `import` `numpy as np` `# Defining a positive infinite integer` `positive_infinity ``=` `np.inf` `print``(``'Positive Infinity: '``, positive_infinity)` `# Defining a negative infinite integer` `negative_infinity ``=` `-``np.inf` `print``(``'Negative Infinity: '``, negative_infinity)`

Output:

```Positive Infinity:  inf
Negative Infinity:  -inf```

## Checking If a Number Is Infinite in Python

To check if a given number is infinite or not, one can use isinf() method of the math library which returns a boolean value. The below code shows the use of isinf() method:

 `import` `numpy as np` `import` `math` `# Defining a positive infinite integer` `a ``=` `np.inf` `# Defining a negative infinite integer` `b ``=` `-``np.inf` `# Define a finite integer` `c ``=` `300` `# check if a in infinite` `print``(math.isinf(a))` `# check if b in infinite` `print``(math.isinf(b))` `# check if c in infinite` `print``(math.isinf(c))`

Output:

```True
True
False```

## Comparing infinite values to finite values in python

The concept of comparing an infinite value to finite values is as simple as it gets. As positive infinity is always bigger than every natural number and negative infinity is always smaller than negative numbers. For better understanding look into the code below:

 `import` `numpy as np` `# Defining a positive infinite integer` `a ``=` `np.inf` `# Defining a negative infinite integer` `b ``=` `-``np.inf` `# Define a finite + ve integer` `c ``=` `300` `# Define a finite -ve integer` `d ``=` `-``300` `# helper function to make comparisons` `def` `compare(x, y):` `    ``if` `x>y:` `        ``print``(``"True"``)` `    ``else``:` `        ``print``(``"False"``)` `        ` `compare(a, b)` `compare(a, c)` `compare(a, d)` `compare(b, c)` `compare(b, d)`

Output:

```True
True
True
False
False```

Last Updated on October 25, 2021 by admin

## Number guessing game in Python 3Number guessing game in Python 3

Number guessing game in Python 3 Most of the geeks from a CS (Computer Science) background, think of their very first project after doing a Programming Language. Here, you will get your very first project and the basic one, in this article.

## Python set() methodPython set() method

Python | set() method Set, a term in mathematics for a sequence consisting of distinct language is also extended in its language by Python and can easily be made using set(). set() method is used to convert any of the iterable

## Python – Using 2D arrays/lists the right wayPython – Using 2D arrays/lists the right way

Python | Using 2D arrays/lists the right way Python provides many ways to create 2-dimensional lists/arrays. However one must know the differences between these ways because they can create complications in code that can be very difficult to trace out.

## Difference between == and is operator in PythonDifference between == and is operator in Python

Difference between == and is operator in Python The Equality operator (==) compares the values of both the operands and checks for value equality. Whereas the ‘is’ operator checks whether both the operands refer to the same object or not (present in the same

## Python return statementPython return statement

Python return statement A return statement is used to end the execution of the function call and “returns” the result (value of the expression following the return keyword) to the caller. The statements after the return statements are not executed.

## Authentication using Python requestsAuthentication using Python requests

Authentication using Python requests Authentication refers to giving a user permissions to access a particular resource. Since, everyone can’t be allowed to access data from every URL, one would require authentication primarily. To achieve this authentication, typically one provides authentication

## Dunder or magic methods in PythonDunder or magic methods in Python

Dunder or magic methods in Python Dunder or magic methods in Python are the methods having two prefix and suffix underscores in the method name. Dunder here means “Double Under (Underscores)”. These are commonly used for operator overloading. Few examples for magic

## Python – Matplotlib.pyplot ticksPython – Matplotlib.pyplot ticks

Python | Matplotlib.pyplot ticks Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. It was introduced by