How to fix "SyntaxError: cannot assign to expression here" in Python
Tags: python, programming
Published: 2023-02-02 19:31:40

> ✋ **Update:** This post was originally published on my blog [decodingweb.dev](https://www.decodingweb.dev), where you can read the [latest version](https://www.decodingweb.dev/solved-syntaxerror-cannot-assign-to-expression-here-in-python) for a 💯 user experience. _~reza_

Python raises “SyntaxError: cannot assign to expression here. Maybe you meant ‘==’ instead of ‘=’?” when you assign a value to an expression. On the other hand, this error occurs if an expression is the left-hand side operand in an assignment statement.

```python
a = 12
b = 2

# 🚫 SyntaxError
a * b = c
```

Additionally, Python provides you a hint, assuming you meant to use the equality operator (`==`):

```python
File /dwd/sandbox/test.py, line 5
    a * b = c
    ^^^^^
SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?
```

But what's an expression? You may ask. 

The term "expression" refers to values or combination of values (operands) and operators that result in a value. That said, all the following items are expressions:

- `'someValue'`
- `12 * 2`
- `4 * (5 + 2)`
- `'someText' + 'anotherText'`

Most of the time, the cause of the "SyntaxError: cannot assign to expression here" error is a typo in your code - usually a missing `=` or invalid identifiers in assignment statements.

## How to fix "SyntaxError: cannot assign to expression here"

The long error "SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?" occurs under various scenarios:

1. <u>Using an invalid name (identifier) in an assignment statement</u>
2. <u>Using `=` instead of `==` in a comparison statement</u>

Let's see some examples.

**Using an invalid name (identifier) in an assignment statement:** Assignment statements bind names to values. (e.g., `total_price = 49.99`)

 Based on Python [syntax and semantics](https://www.decodingweb.dev/how-to-learn-a-programming-language#language-syntax-and-semantics), the left-hand side of the assignment operator (`=`) should always be an identifier, not an expression or a literal.

Identifiers (a.k.a names) are arbitrary names you use for definitions in the source code, such as variable names, function names, and class names. For instance, in the statement `age = 25`, `age` is the identifier.

Python identifiers are based on the Unicode standard annex UAX-31, which describes the specifications for using Unicode in identifiers.

That said, you can only use alphanumeric characters and underscores for names. Otherwise, you'll get a SyntaxError. For instance, `2 + 2 = a` is a syntax error because the left-hand side operator isn't a valid identifier - it's a Python expression.

A common mistake which results in this syntax error is using hyphens (`-`) in your variable names.

Hyphens are only valid in expressions like `45.54 - 12.12`. If you have a `'-'` in your variable name, Python's interpreter would assume it's an expression:

```python
# 🚫 SyntaxError
total-price = 49.45
```

In the above code, Python's interpreter assumes you're trying to subtract a variable name price from another variable named total.

And since you can't have an expression as a left-hand side operand in an assignment statement, you'll get this SyntaxError.

So if your code looks like the above, you need to replace the hyphen (`-`) with an underscore (`_`):

```python
# ✅ a valid identifier contains alphanumeric characters and underscores
total_price = 49.45
```

That's much better!

Invalid comparison statement : Another cause of "SyntaxError: cannot assign to expression here" is using an invalid sequence of comparison operators while comparing values.

This one is more of a syntax-related mistake and rarely finds its way to the runtime, but it's worth watching.

Imagine you want to test if a number is an even number. As you probably know, if we divide a number by `2`, and the remainder is `0`, that number is even.

In Python, we use the modulo operator (`%`) to get the remainder of a division:

```python
x = 45

# Checking if x is an even number
# But Python raises:
# 🚫 SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
if x % 2 = 0:
    print('x is even!')
```

In the above code, once Python encounters the assignment operator (`=`), it assumes we're trying to assign `0` to `x % 2`! No wonder the response is "SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?".

But once we use the equality operator (`==`), the misconception goes away:

```python
x = 34

# Checking if x is an even number
if x % 2 == 0:
    print('x is even!')

# Output: x is even!
```

Problem solved!

Alright, I think it does it. I hope this quick guide helped you solve your problem.

Thanks for reading.

❤️ **You might like:**

- [[Solved] SyntaxError: cannot assign to literal here in Python](https://www.decodingweb.dev/solved-syntaxerror-cannot-assign-to-literal-here-in-python)
- [SyntaxError: cannot assign to function call here in Python (Solved)](https://www.decodingweb.dev/python-syntaxerror-cannot-assign-to-function-call-here-fix)
- [SyntaxError: invalid decimal literal in Python (Solved)](https://www.decodingweb.dev/python-syntaxerror-invalid-decimal-literal-fix)
- [How to learn any programming language in a short time](https://www.decodingweb.dev/how-to-learn-a-programming-language)