How to Check If a Number Is Less Than Another in Python

PythonPythonBeginner
Practice Now

Introduction

In this lab, you will learn how to use the less than operator (<) in Python to compare numerical values. The lab guides you through practical examples, demonstrating how to determine if one number is less than another and how the operator returns a boolean value (True or False) based on the comparison.

You'll start by exploring basic comparisons with different values, then move on to scenarios where the condition is not met and cases where the values are equal. By modifying and running Python scripts in the LabEx environment, you'll gain a hands-on understanding of how the less than operator functions in various situations.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/ControlFlowGroup(["Control Flow"]) python/BasicConceptsGroup -.-> python/variables_data_types("Variables and Data Types") python/BasicConceptsGroup -.-> python/numeric_types("Numeric Types") python/BasicConceptsGroup -.-> python/booleans("Booleans") python/ControlFlowGroup -.-> python/conditional_statements("Conditional Statements") subgraph Lab Skills python/variables_data_types -.-> lab-559550{{"How to Check If a Number Is Less Than Another in Python"}} python/numeric_types -.-> lab-559550{{"How to Check If a Number Is Less Than Another in Python"}} python/booleans -.-> lab-559550{{"How to Check If a Number Is Less Than Another in Python"}} python/conditional_statements -.-> lab-559550{{"How to Check If a Number Is Less Than Another in Python"}} end

Explore Less Than Operator

In this step, you will learn about the less than operator (<) in Python. This operator is used to compare two values and determine if the left-hand side value is less than the right-hand side value. The result of this comparison is a boolean value: True if the condition is met, and False otherwise.

Let's start with a simple example. Open the VS Code editor in the LabEx environment and create a new file named less_than.py in the ~/project directory.

## ~/project/less_than.py
x = 5
y = 10

result = x < y
print(result)

In this code, we assign the value 5 to the variable x and the value 10 to the variable y. Then, we use the less than operator (<) to compare x and y. The result of this comparison (x < y) is assigned to the variable result. Finally, we use the print() function to display the value of result.

To run this script, open the terminal in VS Code and navigate to the ~/project directory. You should already be in this directory by default. Then, execute the following command:

python less_than.py

The output should be:

True

This indicates that x (which is 5) is indeed less than y (which is 10).

Now, let's try another example where the condition is not met:

## ~/project/less_than.py
x = 15
y = 10

result = x < y
print(result)

Modify the less_than.py file with the above content. Run the script again using the same command:

python less_than.py

The output should be:

False

This is because x (which is 15) is not less than y (which is 10).

You can also use the less than operator to compare variables with the same value:

## ~/project/less_than.py
x = 10
y = 10

result = x < y
print(result)

Modify the less_than.py file with the above content. Run the script again using the same command:

python less_than.py

The output should be:

False

Even though x and y have the same value, x is not less than y, so the result is False.

Compare Different Numeric Types

In this step, you will explore how the less than operator (<) works when comparing different numeric types in Python, such as integers and floating-point numbers.

Python supports various numeric types, including integers (int) and floating-point numbers (float). Integers are whole numbers without any decimal part, while floating-point numbers have a decimal part.

Let's create a new Python file named compare_types.py in the ~/project directory using the VS Code editor.

## ~/project/compare_types.py
integer_num = 10
float_num = 10.5

result = integer_num < float_num
print(result)

In this code, we have an integer variable integer_num with a value of 10 and a floating-point variable float_num with a value of 10.5. We then use the less than operator to compare these two variables.

Run the script using the following command in the terminal:

python compare_types.py

The output should be:

True

This is because the integer 10 is less than the floating-point number 10.5.

Now, let's try comparing an integer with a floating-point number that has the same whole number part:

## ~/project/compare_types.py
integer_num = 10
float_num = 10.0

result = integer_num < float_num
print(result)

Modify the compare_types.py file with the above content. Run the script again:

python compare_types.py

The output should be:

False

Even though the whole number part is the same, the integer 10 is not less than the floating-point number 10.0. They are considered equal in value, but the less than operator only returns True if the left-hand side is strictly less than the right-hand side.

Let's consider another example:

## ~/project/compare_types.py
integer_num = 5
float_num = 2.5

result = integer_num < float_num
print(result)

Modify the compare_types.py file with the above content. Run the script again:

python compare_types.py

The output should be:

False

In this case, the integer 5 is greater than the floating-point number 2.5, so the result is False.

Handle Equality Cases

In this step, you will learn how the less than operator (<) behaves when the values being compared are equal. Understanding this behavior is crucial for writing accurate and reliable code.

As you learned in the previous steps, the less than operator (<) returns True only if the left-hand side value is strictly less than the right-hand side value. If the values are equal, the operator returns False.

Let's create a new Python file named equality_cases.py in the ~/project directory using the VS Code editor.

## ~/project/equality_cases.py
x = 10
y = 10

result = x < y
print(result)

In this code, we assign the same value, 10, to both variables x and y. We then use the less than operator to compare them.

Run the script using the following command in the terminal:

python equality_cases.py

The output should be:

False

This is because x is not less than y; they are equal.

Now, let's consider a case with floating-point numbers:

## ~/project/equality_cases.py
x = 5.5
y = 5.5

result = x < y
print(result)

Modify the equality_cases.py file with the above content. Run the script again:

python equality_cases.py

The output should be:

False

Again, the result is False because x and y have the same value.

It's important to remember that the less than operator (<) only checks if the left-hand side is strictly smaller than the right-hand side. If you want to check if a value is less than or equal to another value, you should use the less than or equal to operator (<=), which will be covered in a later lab.

Summary

In this lab, you explored the less than operator (<) in Python, which compares two values and returns True if the left-hand side is less than the right-hand side, and False otherwise. You created a less_than.py file and tested the operator with different values assigned to variables x and y, observing the boolean results of the comparisons.

The lab demonstrated how the < operator evaluates to True when x is less than y, and False when x is greater than or equal to y. You ran the script using python less_than.py in the terminal to verify the output for various scenarios.