free geoip
38

Python Comparison Operators Exercises

Python comparison operators are essential for evaluating conditions, making decisions, and controlling the flow of your programs. They allow developers…

Python comparison operators are essential for evaluating conditions, making decisions, and controlling the flow of your programs. They allow developers to compare values and return a Boolean result: either True or False. In this article, we will explore the different types of comparison operators in Python, provide practical examples, and give you exercises to master this concept.

Python Comparison Operators Exercises

What Are Comparison Operators in Python?

Comparison operators are used to compare two values. They return a Boolean result depending on whether the condition is satisfied. These operators are often used inside conditional statements such as if, while, and loops.

List of Comparison Operators

OperatorDescriptionExample
==Equal to5 == 5 → True
!=Not equal to5 != 3 → True
>Greater than7 > 4 → True
<Less than2 < 8 → True
>=Greater than or equal to10 >= 10 → True
<=Less than or equal to6 <= 9 → True

Basic Examples of Comparison Operators

Let’s go through some Python code examples to better understand how these operators work in real situations.

# Example 1: Equal to (==)
x = 10
y = 10
print(x == y)  # True

# Example 2: Not equal to (!=)
a = 15
b = 20
print(a != b)  # True

# Example 3: Greater than (>)
print(8 > 3)  # True

# Example 4: Less than (<)
print(5 < 2)  # False

# Example 5: Greater than or equal to (>=)
print(12 >= 12)  # True

# Example 6: Less than or equal to (<=)
print(7 <= 10)  # True

Using Comparison Operators in Conditional Statements

Comparison operators are most powerful when used in conditional statements like if and loops. They help control the logic of the program.

age = 18

if age >= 18:
    print("You are eligible to vote.")
else:
    print("You are not eligible to vote.")

In this example, the program checks if the variable age is greater than or equal to 18. If true, it prints that the user is eligible to vote.

Comparison Operators with Strings

Python also allows comparison of strings based on their Unicode values. Let’s see some examples:

# String comparison
print("apple" == "apple")   # True
print("apple" != "banana")  # True
print("cat" > "bat")        # True, because 'c' comes after 'b'

Practical Exercises

Here are some exercises to test your understanding of Python comparison operators. Try solving them before checking the solution.

Exercise 1: Compare Numbers

Write a program to check whether a number entered by the user is positive, negative, or zero.

num = int(input("Enter a number: "))

if num > 0:
    print("Positive")
elif num < 0:
    print("Negative")
else:
    print("Zero")

Exercise 2: Compare Ages

Write a Python program that asks for two ages and checks which person is older.

age1 = int(input("Enter first age: "))
age2 = int(input("Enter second age: "))

if age1 > age2:
    print("The first person is older.")
elif age1 < age2:
    print("The second person is older.")
else:
    print("Both are the same age.")

Exercise 3: Password Validation

Check whether the password entered by the user matches the correct one.

correct_password = "python123"
user_input = input("Enter password: ")

if user_input == correct_password:
    print("Access granted.")
else:
    print("Access denied.")

Exercise 4: Grade Checker

Create a program that checks whether a student’s score is a passing grade or not.

score = int(input("Enter your score: "))

if score >= 50:
    print("Pass")
else:
    print("Fail")

Exercise 5: Maximum of Three Numbers

Write a program to find the largest of three numbers using comparison operators.

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

if a >= b and a >= c:
    print("The largest number is:", a)
elif b >= a and b >= c:
    print("The largest number is:", b)
else:
    print("The largest number is:", c)

Conclusion

Python comparison operators are fundamental for writing programs that make decisions. They are used for comparing values, validating conditions, and creating logic in your applications. By practicing the exercises provided, you will build a solid foundation in using these operators effectively. Keep practicing, and soon you will find comparison operators second nature in your Python journey.

For further reading on Python operators, check out the official Python documentation at Python Docs.

rysasahrial

Leave a Reply

Your email address will not be published. Required fields are marked *