How to Solve Python SyntaxError: can’t assign to function call

by | Programming, Python, Tips

Function calls and variable assignments are distinct operations in Python. Variable assignments are helpful for code structure, and function calls help reuse code blocks. To assign the result of a function to a variable, you must specify the variable name followed by an equals = sign, then the function you want to call. If we do not follow this syntax, the Python interpreter will raise “SyntaxError: can’t assign to function call” when you execute the code.

This tutorial will go through the correct use of variable assignments and function calls. We will go through the premise of syntax errors and look at example scenarios that raise the “SyntaxError: can’t assign to function call” error and solve it.

SyntaxError: can’t assign to function call

In Python, variable assignment uses the following syntax

particle = "Muon"

The variable’s name comes first, followed by an equals sign, then the value the variable should hold. We can say this aloud as

particle is equal to Muon“.

You cannot declare a variable by specifying the value before the variable. This error occurs when putting a function call on the left-hand side of the equal sign in a variable assignment statement. Let’s look at an example of the error:

def a_func(x):

    return x ** 2

a_func(2) = 'a_string'
    a_func(2) = 'a_string'
SyntaxError: cannot assign to function call

This example uses a function called a_func, which takes an argument x and squares it as its output. We call the function and attempt to assign the string ‘a_string’ to it on the right-hand side of the equal sign. We will raise this error for both user-defined and built-in functions, and the specific value on the right-hand side of the equals sign does not matter either.

Generally, SyntaxError is a Python error that occurs due to written code not obeying the predefined rules of the language. We can think of a SyntaxError as bad grammar in everyday human language.

Another example of this Python error is “SyntaxError: unexpected EOF while parsing“. This SyntaxError occurs when the program finishes abruptly before executing all of the code, likely due to a missing parenthesis or incorrect indentation.

Example: Square Root Function for an Array

Let’s build a program that iterates over an array of numbers and calculates the square root of each, and returns an array of the square root values.

To start, we need to define our list of numbers:

square_numbers = [4, 16, 25, 36, 49, 64]

Then we define our function that calculates the square root of each number:

def square_root(numbers):

   square_roots = []

   for num in numbers:

       num_sqrt = num ** 0.5


   return square_roots

Let’s try to assign the value it returns to a variable and print the result to the console

square_root(square_numbers) = square_roots

    square_root(square_numbers) = square_roots
SyntaxError: cannot assign to function call

The error occurs because we tried to assign a value to a function call. The function call in this example is square_root(square_numbers). We tried to assign the value called square_roots to a variable called square_root(square_numbers).

When we want to assign the response of a function to a variable, we must declare the variable first. The order is the variable name, the equals sign, and the value assigned to that variable.


To solve this error, we need to reverse the variable declaration order.

square_roots = square_root(square_numbers)

[2.0, 4.0, 5.0, 6.0, 7.0, 8.0]

Our code runs successfully and prints the square root numbers to the console.


Congratulations on reading to the end of this tutorial. The error “SyntaxError: can’t assign to function call” occurs when you try to assign a function call to a variable. Suppose we put the function call before a variable declaration. In that case, the Python interpreter will treat the code as trying to assign a value to a variable with a name equal to the function call. To solve this error, ensure you follow the correct Python syntax for variable assignments. The order is the variable name first, the equals sign, and the value to assign to that variable.

Here are some other SyntaxErrors that you may encounter:

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!