0

I am a beginner at this course.

I am getting a SyntaxError: invalid syntax with the following code:

class = 8
print(class)

Output:

C:\Users\BISWAJIT\PycharmProjects\NewJourney\venv\Scripts\python.exe C:/Users/BISWAJIT/PycharmProjects/NewJourney/Jit.py
File "C:\Users\BISWAJIT\PycharmProjects\NewJourney\Jit.py", line 1
class = 8
^
SyntaxError: invalid syntax

Process finished with exit code 1

What am I doing wrong?

1 Answer 1

3

class is a Python keyword. It means that this word has a particular meaning for the Python language, and thus cannot be used as a variable name.

Here is the list of keywords, as given by the keyword module:

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

You can also get it directly from the documentation.

You should also note that there is another set of words called builtins (full set here) for which it is bad practice to use as variable names, although you won't get a SyntaxError.

A common way to go around this is either to use klass instead of class, or add a suffix to the keyword, like in class_ = 8. Or you can also find a better naming, depending on the purpose of your variable: class_number = 8.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.