Python Tutorials, Tricks & Tips, Projects, etc..,

Keywords and Identifiers in Python Programming Language

| July 10, 2018 | TAG: python | No comments:

In this tutorial, You will Learn about Keywords (reserved words in python) and Identifiers (a name that is given to variables, functions, etc..,)

Keywords and Identifiers plays a significant role in any Programming language. You have to know about them.


Let's start.....

Python Keywords

Keywords are the reserved in Python.

We can't use the keywords as a variable name or function name or any other identifier. They have their unique usage, which is to define the structure and syntax of Python programming language.

Python is a case-sensitive programming language. Means the lowercase name and the UPPERCASE name are totally different.

Example:- a is different from A

There are 33 Keywords in the Python 3.6 Version as of now.

Later in upcoming versions, count maybe increases or decreases.

Keywords in Python Programming Language

False None True and as
assert break class continue def
del elif else except finally
for from global import if
in is lambda nonlocal not
or pass raise return try
while with yield

All the Keywords except True, False and None are all lowercase.

If you want to get all the Keywords in the interpreter, write the following code and execute it.


import keyword


['False', 'None', 'True', 'and', 'as', 'assert', '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']

If you don't know what print doing there, go to Introduction to Python Programming Language to find out Python applications and many more.

Python Identifiers

Identifier is the name given to variables, classes, functions, etc.., It helps to differentiate from one another.

Rules for Naming Identifiers

1. Length of the identifier can be anything.

2. Identifiers can be a combination of letters in lowercase (a-z) or uppercase (A-Z) or numbers (0-9) or underscore (_). Names like myName, var_1, printing_this_statement are valid.

3. Identifier can't start with a digit. It can start with an underscore (_). Names like 1_var, 1stdigit are valid whereas var_1, digit_1 are valid.

4. Keywords can't be used as an identifier name. Run the following code in interactive mode you will get an error as shown below.

>>> True = 1
File "<stdin>", line 1
SyntaxError: cant assign to keyword
>>> with = 1
File "<stdin>", line 1
with = 1
SyntaxError: invalid syntax

5. You can't use special characters in the Identifiers. Try to run the following code in interactive mode.

>>> n@me = 'python'
File "<stdin>", line 1
SyntaxError: cant assign to operator
>>> nam@ = 'python'
File "<stdin>", line 1
nam@ = 'python'
SyntaxError: invalid syntax

Things to Remember

1. Separate each word with an underscore ( _ ), if you have multiple words like my_name

2. Or use camelCase in which each starting letter of a word is capitalized like myName.

3. Try to use a meaningful name like blog_name = 'geeksbro' instead of using bn = 'geeksbro'. Here blog_name is more clear than bn. Also helps if, you are working with a Team.


If you have any doubts, comment below.

Post a Comment