Last updated: Apr 8, 2024
Reading timeยท6 min

Note: If you got the error: "SyntaxError: cannot assign to literal here", click on the second subheading.
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.

Here is an example of how the error occurs.
# โ๏ธ SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='? my-variable = 5

- in the name of the variable, so Python thinks we are trying to subtract two variables.If this is how you got the error, use an underscore instead of a hyphen.
my_variable = 5 print(my_variable) # ๐๏ธ 5

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.
Here is another example of how the error occurs.
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.
a = 20 b = 5 result = a / b print(result) # ๐๏ธ 4.0

Now that the division is moved to the right-hand side, the error is resolved.
If you mean to compare two values, use the double equals (==) sign.
a = 20 b = 5 if a/b == 4: # ๐๏ธ this runs print('Success') else: print('Failure')

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.
# โ assignment (=) a = 5 # โ comparison (==) if a == 5: print('success')
If you use a single equal (=) sign when comparing values, the error is raised.
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')
If you get the error when declaring a variable that stores a dictionary, use the following syntax.
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.
my_dict = { 'name': 'Bobby Hadz', 'age': 30 # ๐๏ธ missing comma 'tasks': ['dev', 'test'] }
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.

Here are 2 examples of how the error occurs.
# โ๏ธ 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'

1 and the string name.Literal values are strings, integers, booleans and floating-point numbers.
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 (=).
my_num = 1 name = 'Bobby Hadz'

Notice that variable names should be wrapped in quotes as that is a string literal.
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.
You can think of a variable as a container that stores a specific value.
employee = {'name': 'Bobby Hadz', 'age': 30}
Variable names should not be wrapped in quotes.
If you got the error while declaring multiple variables on the same line, use the following syntax.
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.
a = 1; b = 2 print(a) # ๐๏ธ 1 print(b) # ๐๏ธ 2
However, this is uncommon and unnecessary.
If you meant to perform an equality comparison, use double equals.
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 <=.
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.
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.
for loopThe error also occurs if you try to assign a value to a literal in a for loop by mistake.
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.
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.
If you meant to declare a dictionary, use curly braces.
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.
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().
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).
my_dict = {'id': 1, 'name': 'BobbyHadz'} # ๐๏ธ dict_items([('id', 1), ('name', 'BobbyHadz')]) print(my_dict.items())
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 _.
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.
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.
You can learn more about the related topics by checking out the following tutorials: