Python Comparison Operators: The 6 Basics and How to Use Them

When using if statements to control the flow of a program, you often need to compare the relationship between two values. For example, “Is the variable greater than 100?” or “Does the input password match the saved one?”

Comparison operators are used for this purpose. They compare two values and return a Boolean value (True or False) indicating whether the relationship is correct.

This article explains the six basic comparison operators in Python with specific examples.


目次

The 6 Comparison Operators

Python provides the following six comparison operators.

OperatorMeaningExample
==Equala == b
!=Not equala != b
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b

Note: Do not confuse == (equal) with = (assignment).

= is used to store (assign) a value into a variable, not for comparison.


Examples of Comparison Operators

Let’s see how comparison operators return True or False.

1. Numerical Comparison

You can compare numbers. This is frequently used for checking limits.

# Speed limit and current speed
speed_limit = 60
current_speed = 75

# Is 'current_speed' greater than 'speed_limit'? (>)
is_over_limit = current_speed > speed_limit
print(f"Over limit: {is_over_limit}") # True

# Is 'current_speed' less than or equal to 'speed_limit'? (<=)
is_within_limit = current_speed <= speed_limit
print(f"Within limit: {is_within_limit}") # False

# Required stock and current stock
required_stock = 100
current_stock = 100

# Is 'current_stock' greater than or equal to 'required_stock'? (>=)
is_sufficient = current_stock >= required_stock
print(f"Sufficient stock: {is_sufficient}") # True

Output:

Over limit: True
Within limit: False
Sufficient stock: True

2. String Comparison

You can also compare strings. == (equal) and != (not equal) are mainly used.

# Registered User ID and Input ID
registered_id = "admin_user"
input_id = "admin_user"

# Are the two strings equal? (==)
is_match = (registered_id == input_id)
print(f"ID Match: {is_match}") # True

# ---
# Password Comparison (Case sensitive)
correct_password = "PassWord123"
input_password = "password123" # Starts with lowercase

# Are the two strings NOT equal? (!=)
is_different = (correct_password != input_password)
print(f"Password Mismatch: {is_different}") # True

Output:

ID Match: True
Password Mismatch: True

Using Directly in if Statements

The result of a comparison operator (True/False) is used directly in the condition of an if statement.

def check_access_level(level):
    """
    Check if access level is 50 or higher.
    """
    required_level = 50
    
    # Use comparison operator (>=) in if statement condition
    if level >= required_level:
        print(f"Level {level}: Access Granted.")
    else:
        print(f"Level {level}: Access Denied.")

# Execution
check_access_level(70)
check_access_level(30)

Output:

Level 70: Access Granted.
Level 30: Access Denied.

Summary

Comparison operators are basic tools for controlling the execution flow of a program.

  • They compare two values and return a Boolean value (True or False).
  • There are 6 types: ==, !=, >, <, >=, <=.
  • They are used in if statements to execute specific code only when the condition is True.
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次