To check if a number if larger than a given number N in Python, the easiest way is with the greater than > operator and an if statement.

num = 12
N = 10

if num > N:
    print("num is larger than N")
else:
    print("num is smaller than N")

#Output:
num is larger than N

When working with numbers in Python, the ability to easily be able to make comparisons is important.

One such case is if you want to check if a number is larger than a given number N.

To check if a number is greater than another, you can use the greater than > operator and an if statement.

Below is a simple example showing you how to check if a number is greater than another number in Python.

num = 12
N = 10

if num > N:
    print("num is larger than N")
else:
    print("num is smaller than N")

#Output:
num is larger than N

If you want to put this in a function, you can do the following.

def largerThan(num, N):
    if num > N:
        print("num is larger than N")
    else: 
        print("num is smaller than N")

largerThan(12,10)

#Output:
num is larger than N

Check if Number is Smaller than N in Python

If you want to go the other way and check if a number is smaller than a given number N, then you can use the less than operator <.

Below is a simple example showing you how to check if a number is less than another number in Python.

num = 5
N = 10

if num < N:
    print("num is smaller than N")
else:
    print("num is larger than N")

#Output:
num is smaller than N

Hopefully this article has been useful for you to learn how to check if a number is larger than N in Python.

Categorized in:

Python,

Last Update: March 22, 2024