SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
6 min

banner

# Table of Contents

  1. SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?
  2. SyntaxError: cannot assign to literal here (Python)

Note: If you got the error: "SyntaxError: cannot assign to literal here", click on the second subheading.

# SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?

The Python "SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?" occurs when we have an expression on the left-hand side of an assignment.

To solve the error, specify the variable name on the left and the expression on the right-hand side.

syntaxerror cannot assign to expression here

Here is an example of how the error occurs.

main.py
# โ›”๏ธ SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='? my-variable = 5

hyphen in the name of the variable

We have a hyphen - in the name of the variable, so Python thinks we are trying to subtract two variables.

# Don't use hyphens in variable names

If this is how you got the error, use an underscore instead of a hyphen.

main.py
my_variable = 5 print(my_variable) # ๐Ÿ‘‰๏ธ 5

dont use hyphens in variable names

The name of a variable must start with a letter or an underscore.

A variable name can contain alpha-numeric characters (a-z, A-Z, 0-9) and underscores _.

Variable names cannot contain any other characters than the aforementioned.

# Don't use expressions on the left-hand side of an assignment

Here is another example of how the error occurs.

main.py
a = 20 b = 5 # โ›”๏ธ SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='? a/b = 4

We have an expression on the left-hand side which is not allowed.

The variable name has to be specified on the left-hand side, and the expression on the right-hand side.

main.py
a = 20 b = 5 result = a / b print(result) # ๐Ÿ‘‰๏ธ 4.0

use expression on right hand side

Now that the division is moved to the right-hand side, the error is resolved.

# Use double equals (==) when comparing values

If you mean to compare two values, use the double equals (==) sign.

main.py
a = 20 b = 5 if a/b == 4: # ๐Ÿ‘‡๏ธ this runs print('Success') else: print('Failure')

use double equals when comparing values

Notice that we use double equals == when comparing two values and a single equal = sign for assignment.

Double equals (==) is used for comparison and single equals (=) is used for assignment.

main.py
# โœ… assignment (=) a = 5 # โœ… comparison (==) if a == 5: print('success')

If you use a single equal (=) sign when comparing values, the error is raised.

main.py
a = 20 b = 5 # โ›”๏ธ SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='? if a/b = 4: # ๐Ÿ‘‡๏ธ this runs print('Success') else: print('Failure')

# Declaring a dictionary

If you get the error when declaring a variable that stores a dictionary, use the following syntax.

main.py
my_dict = { 'name': 'Bobby Hadz', 'age': 30, 'tasks': ['dev', 'test'] } print(my_dict['name']) # ๐Ÿ‘‰๏ธ Bobby Hadz print(my_dict['age']) # ๐Ÿ‘‰๏ธ 30

Notice that each key and value are separated by a colon and each key-value pair is separated by a comma.

The error is sometimes raised if you have a missing comma between the key-value pairs of a dictionary.

main.py
my_dict = { 'name': 'Bobby Hadz', 'age': 30 # ๐Ÿ‘ˆ๏ธ missing comma 'tasks': ['dev', 'test'] }

# SyntaxError: cannot assign to literal here (Python)

The Python "SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?" occurs when we try to assign to a literal (e.g. a string or a number).

To solve the error, specify the variable name on the left and the value on the right-hand side of the assignment.

syntaxerror cannot assign to literal here

Here are 2 examples of how the error occurs.

main.py
# โ›”๏ธ SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='? 1 = 'abc' # โ›”๏ธ SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='? 'name' = 'Bobby Hadz'

value on left hand side of assignment

The error is caused because we are trying to assign a value to 2 literals - the integer 1 and the string name.

Literal values are strings, integers, booleans and floating-point numbers.

# Variable names on the left and values on the right-hand side

When declaring a variable make sure the variable name is on the left-hand side and the value is on the right-hand side of the assignment (=).

main.py
my_num = 1 name = 'Bobby Hadz'

variable names on left and values on right hand side

Notice that variable names should be wrapped in quotes as that is a string literal.

The left-hand side of the assignment cannot be a literal like a string or a number.

The string "name" is always going to be equal to the string "name", and the number 100 is always going to be equal to the number 100, so we cannot assign a value to a literal.

# A variable is a container that stores a specific value

You can think of a variable as a container that stores a specific value.

main.py
employee = {'name': 'Bobby Hadz', 'age': 30}

Variable names should not be wrapped in quotes.

# Declaring multiple variables on the same line

If you got the error while declaring multiple variables on the same line, use the following syntax.

main.py
a, b = 1, 2 print(a) # ๐Ÿ‘‰๏ธ 1 print(b) # ๐Ÿ‘‰๏ธ 2

The variable names are still on the left, and the values are on the right-hand side.

You can also use a semicolon to declare multiple variables on the same line.

main.py
a = 1; b = 2 print(a) # ๐Ÿ‘‰๏ธ 1 print(b) # ๐Ÿ‘‰๏ธ 2

However, this is uncommon and unnecessary.

# Performing an equality comparison

If you meant to perform an equality comparison, use double equals.

main.py
my_num = 100 if 100 == my_num: # ๐Ÿ‘‡๏ธ This runs print('success') else: print('failure')

We use double equals == for comparison and single equals = for assignment.

If you need to check if a value is less than or equal to another, use <=.

main.py
my_num = 100 if 100 <= my_num: # ๐Ÿ‘‡๏ธ This runs print('success') else: print('failure')

Similarly, if you need to check if a value is greater than or equal to another, use >= operator.

main.py
my_num = 100 if 100 >= my_num: # ๐Ÿ‘‡๏ธ This runs print('success') else: print('failure')

Make sure you don't use a single equals = sign to compare values because single equals = is used for assignment and not for comparison.

# Assigning to a literal in a for loop

The error also occurs if you try to assign a value to a literal in a for loop by mistake.

main.py
a_list = ['bobby', 'hadz', 'com'] # โ›”๏ธ SyntaxError: cannot assign to literal for 'item' in a_list: print(item)

Notice that we wrapped the item variable in quotes which makes it a string literal.

Instead, remove the quotes to declare the variable correctly.

main.py
a_list = ['bobby', 'hadz', 'com'] # โœ… Declare item variable correctly for item in a_list: # bobby # hadz # com print(item)

Now we declared an item variable that gets set to the current list item on each iteration.

# Using a dictionary

If you meant to declare a dictionary, use curly braces.

main.py
my_dict = { 1: 'a', 2: 'b', 3: 'c', } # ๐Ÿ‘‡๏ธ {1: 'a', 2: 'b', 3: 'c'} print(my_dict) print(my_dict[1]) # ๐Ÿ‘‰๏ธ a print(my_dict[2]) # ๐Ÿ‘‰๏ธ b

A dictionary is a mapping of key-value pairs.

You can use square brackets if you need to add a key-value pair to a dictionary.

main.py
my_dict = {} my_dict['name'] = 'bobby hadz' my_dict['age'] = 30 print(my_dict) # ๐Ÿ‘‰๏ธ {'name': 'bobby hadz', 'age': 30} print(my_dict['name']) # ๐Ÿ‘‰๏ธ bobby hadz print(my_dict['age']) # ๐Ÿ‘‰๏ธ 30

If you need to iterate over a dictionary, use a for loop with dict.items().

main.py
my_dict = { 'name': 'bobby hadz', 'age': 30, } for key, value in my_dict.items(): # name bobby hadz # age 30 print(key, value)

The dict.items method returns a new view of the dictionary's items ((key, value) pairs).

main.py
my_dict = {'id': 1, 'name': 'BobbyHadz'} # ๐Ÿ‘‡๏ธ dict_items([('id', 1), ('name', 'BobbyHadz')]) print(my_dict.items())

# Valid variable names in Python

The name of a variable must start with a letter or an underscore.

A variable name can contain alpha-numeric characters (a-z, A-Z, 0-9) and underscores _.

main.py
my_num = 100 my_color = 'green'

Note that variable names cannot start with numbers or be wrapped in quotes.

Variable names in Python are case-sensitive.

main.py
my_color = 'green' MY_COLOR = 'red' print(my_color) # ๐Ÿ‘‰๏ธ 'green' print(MY_COLOR) # ๐Ÿ‘‰๏ธ 'red'

The 2 variables in the example are completely different and are stored in different locations in memory.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev