[Solved]: Python SyntaxError


What is Syntax in programming Language?

In any programming language, Syntax are set of rules which dictate how program need to written. Every language have different syntax from other language like C, C++, Java, C# having curely bracket ({}) to represent block of statements while in Python blocks like if-else, loops, functions etc. represent by indentations. We can also think of example of our spoken language having grammar, punctuation etc.

You can identify these exceptions on runtime when you code give SyntaxError the you can fix it. In Python, Sometime programmer omit some set statement, not following indentation or programmer move from another language put curly bracket({}),semi colon (:) etc. then it show SyntaxError because python doesn’t support that.

In this blog you will learn about the some of the common Python SyntaxErrors, provide examples, and show you how to fix them.

Example 1:

In this below example where calculating average of numbers and returning final result to print but this simple example will through exception as in output

numbers = [8, 7, 9, 8, 7]

def calculate_average_age():
    average = sum(numbers) / len(numbers)
    print(average)
return average

total_average = calculate_average_age(numbers)
print(total_average)

Output

File “C:/Users/saurabh.gupta/Desktop/Python Example/Exception Test.py”, line 12
return average
^
SyntaxError: ‘return’ outside function

Solution

The above issue is because of indentation of return statement that’s why it’s showing in error as ‘return is outside the function’. To fix this problem return should be same intended as other statement of function as below.

numbers = [8, 7, 9, 8, 7]

def calculate_average_age():
    average = sum(numbers) / len(numbers)
    print(average)
    return average
total_average = calculate_average_age(numbers)
print(total_average)

Example 2

Let’s take another example of dictionary which will print the elements as key and value but this program will through exception as “SyntaxError: Invalid syntax“.

user = {
    "name": "Saurabh Gupta",
    "age": 35
    "city": "Noida"
}
#iterate user dictionary
for key, value in user.items():
    print("Key:", key)
    print("Value:", str(value))

Output

File “C:/Users/saurabh.gupta/Desktop/Python Example/Exception Test.py”, line 4
“city”: “Noida”
^
SyntaxError: invalid syntax

Solution

In this above example, if you noticed this exception occurred in the line 4 inside the dictionary. It’s because of missing commas in each element for key and value that’s why it’s showing exception as “SyntaxError: invalid syntax“.

You can modify the above code after adding comma in dictionary as below:

user = {
    "name": "Saurabh Gupta",
    "age": 35,
    "city": "Noida"
}
#iterate user dictionary
for key, value in user.items():
    print("Key:", key)
    print("Value:", str(value))

Output

The above line of updated code will return output as below:

Key: name
Value: Saurabh Gupta
Key: age
Value: 35
Key: city
Value: Noida

Summary

In this topic you learn about the Syntax inside the programming language and in case any SyntaxError happen in your code you can identify the problem by checking he stack trace of logs by line number then identify the problem accordingly fix it.

Learn Python exception handling in more detain in topic Python: Exception Handling

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s