Skip to main content

How to Check if a Number Is Greater Than Another in Python

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Most decisions in a program come down to comparing two numbers: is the temperature above 30, is the score at least 50, is the count below the limit. Python has four operators for it, each giving True or False. This guide uses all four on Python 3.15.2.

Syntax​

Syntax
<a> > <b>
<a> < <b>
<a> >= <b>
<a> <= <b>
OperatorTrue when
><a> is greater than <b>
<<a> is less than <b>
>=<a> is greater than or equal to <b>
<=<a> is less than or equal to <b>

The Python reference says that the operators <, >, ==, >=, <= and != "compare the values of two objects".

The Example​

A temperature of 31, compared with 30 and with 31.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

compare_numbers.py
temperature = 31

print(temperature > 30)
print(temperature < 30)
print(temperature >= 31)
print(temperature <= 30)

The compare_numbers.py script in a macOS-style code window: temperature is 31, then prints of whether temperature is greater than 30, less than 30, greater than or equal to 31, and less than or equal to 30.

Step 2: Run It​

Terminal
uv run python compare_numbers.py
Output
True
False
True
False

The terminal after uv run python compare_numbers.py: True, False, True, False.

31 is greater than 30, so > gave True and < gave False. >= gave True because 31 equals 31, and <= gave False because 31 is above 30. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Writing =>print(5 => 3) stopped with SyntaxError: expression cannot contain assignment, perhaps you meant "=="?>=: the = always comes second
Comparing text with a number"5" > 3 stopped with TypeError: '>' not supported between instances of 'str' and 'int'Turn the text into a number first: int("5") > 3 printed True

FAQ​

How do I check that a number is between two others?​

Chain the comparisons: 0 < temperature < 40 printed True. The reference says that x < y <= z "is equivalent to" x < y and y <= z, except that y is worked out only once.

Can I compare a whole number with a decimal?​

Yes: 3 < 3.5 printed True. Python compares the values, whatever kind of number they are.

Conclusion​

With temperature = 31, temperature > 30 and temperature >= 31 were True, while temperature < 30 and temperature <= 30 were False.

Additional Resources​