Online Tutorials & Training Materials | STechies.com
Register Login

TypeError: '>' not supported between instances of 'str' and 'int'

30 Mar 2020 11:39 am || 0

What is TypeError: '>' not supported between instances of 'str' and 'int'?

In this article, we will learn about the error TypeError: ‘>’ not supported between instances of ’str’ and ‘int’. This error occurs when we try to perform a comparison between two variables of the different data type. In this case, we are performing a comparison between integers and a string value. Thus, the error is raised.

TypeError: '>' not supported between instances of 'str' and 'int

Let us understand it more briefly with the help of an example:

Example

# Python code to explain max() function

# Find maximum of integers with one string
print('The Max is: ',max(1, 'stechies', 3, 9))

Output

File "main.py", line 1, in <module>                                                                                         
print('The Max is: ',max(1, 'stechies', 3, 9))                                                                            
TypeError: unorderable types: str() > int()

Explanation

In the above example, we are trying to find the maximum number between the provided values.

Here we are provided with 3 integer values and a string value. Then we used the max() function to find the maximum of these values. But as soon as we run the program we encounter the TypeError.

This error is encountered as the max() method is unable to compare string values with integer values. As “stechies” is the only string among the integer values, max() cannot compare it and throws the error.

Correct code

# Python code to explain max() function

# Find maximum of integers 
print('The Max is: ',max(1, 4, 3, 9))

Output

The Max is: 9

Explanation

Here, the max() method passed all integer arguments (1,4,3,9) without any string values. So, the max() method is able to compare the largest value among the integers. Thus we got the output as 9.