Difference Between = and == in Python With Examples

As a Python developer, you should know the difference between = and ==. In this tutorial, I will explain the difference between the assignment operator (=) and the equality operator (==) in Python with examples.

The Assignment Operator (=)

The assignment operator (=) in Python is used to assign a value to a variable. It doesn’t compare values; instead, it takes the value on the right-hand side and assigns it to the variable on the left-hand side.

Here is an example.

Example:

# Assigning values to variables
city = "New York"
population = 8419000

In this example, the string "New York" is assigned to the variable city, and the integer 8419000 is assigned to the variable population.

The Equality Operator (==)

The equality operator (==) in Python is used to compare two values. It checks whether the values on either side of the operator are equal and returns True if they are, and False otherwise.

Let me show you an example.

Example:

# Comparing values
city1 = "Los Angeles"
city2 = "San Francisco"

print(city1 == city2)  # Output: False

Here, city1 is assigned the value "Los Angeles" and city2 is assigned "San Francisco". The == operator checks if the values of city1 and city2 are the same. Since they are not, the expression evaluates to False.

Check out Difference Between Mutable And Immutable In Python With Examples

Difference Between = and == in Python

Here’s a summary of the differences between = and == in Python.

Feature= (Assignment Operator)== (Equality Operator)
PurposeAssigns a value to a variableCompares two values for equality
Usagevariable = valuevalue1 == value2
Examplecity = "New York"city1 == city2
OperationStores the value on the right into the variable on the leftChecks if the values on both sides are equal
Return TypeNoneBoolean (True or False)
Common PitfallUsing = in conditions (results in syntax error)Misunderstanding that it compares values, not assigns
Syntax Error Exampleif (a = 5): (Invalid syntax)if (a == 5): (Valid syntax)
Practical Examplepopulation = 8419000state_capitals["Texas"] == "Austin"

This table highlights the key differences and usage scenarios for the assignment operator (=) and the equality operator (==) in Python.

Check out Difference Between / and // in Python Division

Example: = and == in Python

Now, let me show you a practical example to understand the difference between = and == in Python. Suppose we are developing a simple program to check if a given city is the capital of a specific state.

Here is the complete code.

# List of state capitals
state_capitals = {
    "California": "Sacramento",
    "Texas": "Austin",
    "New York": "Albany",
    "Florida": "Tallahassee"
}

# User input
input_city = "Austin"
input_state = "Texas"

# Check if the input city is the capital of the input state
is_capital = state_capitals[input_state] == input_city

print(is_capital)  # Output: True

In this example, we have a dictionary state_capitals that maps states to their capitals. We then take user input for a city and a state and use the == operator to check if the input city matches the capital of the input state. The assignment operator (=) is used to assign values to input_city and input_state, as well as to assign the result of the comparison to is_capital.

I executed the above Python code, and you can see the output in the screenshot below:

difference between = and == in python with example

Conclusion

In Python, the assignment operator assigns values to variables, while the equality operator compares values. In this tutorial, I explained the difference between = and == in Python with examples.

You may also like:

Leave a Comment