Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I am wondering how to define inf and -inf as an int in Python 2.7. I tried and it seems inf and -inf only works as a float.

a = float('-inf') # works

b = float('inf') # works 

c = int('-inf') # compile error, ValueError: invalid literal for int() with base 10: 'inf' 

d = int('inf') # compile error, ValueError: invalid literal for int() with base 10: 'inf'

1 Answer

0 votes
by (106k points)
edited by

In, Python there is no way to represent infinity as an integer in Python. This matches the behaviour of many other languages. However, due to Python's dynamic typing system, you can use float('inf') in place of an integer, and it will behave as you would expect. 

To know more about this you can have a look at the following video tutorial:-

Browse Categories

...