In your coding sessions, there are moments where you are unsure whether a word should be used as a variable or not, especially words such as python, list, try, raise, e.t.c. The word python is one that I have doubts about when sometimes I would use it to name my variables or any other identifier. One time, I had a task that required naming a class ‘Python’ as I was creating a Python program on Animals.

In general, python is not a keyword in Python programming language. The word ‘python’ is not listed as part of the keywords and reserved words designated for special functionality in Python. Thus, you can use the word ‘python’ to name your variables, functions, classes, and any other identifiers.

The main drive towards knowing whether python is a keyword in Python is to avoid compromises arising from naming identifiers, keywords, and variable names.

What are identifiers in Python?

Identifiers in Python are names given to a variable, function, class, module, or any other object. Identifiers are descriptive of the code they represent.

When naming identifiers, programmers and Python developers follow the rules to avoid conflicting words. Besides, Python Org offers these guidelines to help devs produce standard and understandable code.

  1. Identifiers must contain a sequence of letters, digits, or underscores. Thus, an identifier must have its first character, either a letter, digit, or underscore.
  2. You must separate the words with an underscore when using more than one word for your identifier. Here are examples: my_variable, test_speed, article_title, e.t.c.
  3. You may use any case for your identifiers. Thus, Student, STUDENT, subject, and hard_worker are examples of valid identifiers.
  4. Identifiers must not conflict with default Python keywords and reserved words. They must be unique and should not have similar spelling and case to any other Python keywords.

Whenever compromises or conflicting use of similar words arise, most likely, your Python program will throw an error or terminate prematurely.

For example, if you use keywords used in Python as default to name your identifiers, such as variables, you will have duplicate identifiers with different functionalities in your code. Thus, the compiler will have a hard time figuring out the functionality intended for a particular line or code block it encounters using conflicting naming.

So, you should take care to name your identifiers, such as variable names, function names, class names, and module names, with unique names. Besides, the names must not conflict with names already reserved by Python as keywords.

Keywords are reserved words in Python. By being reserved, these words hold special meanings to the interpreter, and overriding them leads to your program crashing. We cannot use a keyword as a variable name, function name, or any other identifier.

Python has a set of keywords designated for unique functionality and cannot be used as variable names or any other identifiers.

Table showing the 35 keywords used in Python

FalseclassfromorNone
continueglobalpassTruedef
ifraiseanddelimport
returnaselifin try
assertelseiswhileasync
exceptlambdawithawaitfinally
nonlocalyieldbreakfornot

From the table above, there is no ‘python’ keyword listed as a reserved word Python. Thus, it is safe to use the word python in your code.

Let’s see if Python will have a problem if we use the word python in our code.

Let’s say you have a class called ‘Python’

class Python():
    
    def __init__(self, id, classification, color):
        self.id = id 
        self.classification = classification
        self.color = color
        
    def slither(self):
        return 'Slither me'
        
    def hiss(self):
        return 'tss ts tss'


python = Python(1, 'cold-blooded', 'brown')
print(python.classification)
print(python.slither())

Python doesn’t have a problem when we use the word python to name our class and the instance objects created from that class. The reason is that ‘python’ is not a keyword in Python. If it were, we would not be able to use python in lowercase or uppercase letters.

All keywords in Python are in lowercase or uppercase. The majority of the keywords are in lowercase. Examples of Python keywords that are written in all lowercase are def, print, pass, class, e.t.c

However, 3 keywords in Python are not written with the first letter in lowercase. Instead, they start with an uppercase letter. The keywords in Python starting with an uppercase letter are None, False, and True.

Special reserved words with specific meanings and purposes can’t be used to name anything else; whether a variable, a function, a class, or a module. Thus, they are only created for special functionalities in Python programming language.

Related Questions

Is list a keyword in Python?

list is a keyword in Python programming language. The word ‘list’ is listed as part of the keywords and reserved words designated for special functionality in Python. The list keyword is used to create a list data structure, which is a data type used to store multiple items. Thus, you cannot use the word ‘list’ to name your variables, functions, classes, and any other identifiers.

Is try a keyword in Python?

try is a keyword in Python programming language. The word ‘try’ is listed as part of the keywords and reserved words designated for special functionality in Python. try keyword is used to define code blocks aimed at testing purposes, i.e., check your code for errors, mitigate the error if it occurs, and prevent your Python program from crashing. Thus, you cannot use the word ‘try‘ to name your variables, functions, classes, and any other identifiers.

Is print a keyword in Python?

print is a keyword in Python programming language. The word ‘print’ is listed as part of the keywords and reserved words designated for special functionality in Python. The keyword ‘print’ is used to send messages, usually as textual outputs to a standard output device such as the screen/console or the text stream file

Thus, you cannot use the word ‘print‘ to name your variables, functions, classes, and any other identifiers.

Is raise a keyword in Python?

raise is a keyword in Python programming language. The word ‘raise’ is listed as part of the keywords and reserved words designated for certain functionality in Python. raise keyword is used to interrupt the normal flow of program execution to alert the user that an error has occurred and triggered an exception. Thus, you cannot use the word ‘raise’ to name your variables, functions, classes, and other identifiers.

That’s it for this article.

Crack on Pythonista!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *