All programming languages reserve specific words or keywords that have a definite meaning. In addition to that, the languages keep a restriction on their use. Keywords and identifiers are an integral part of any Python program as well. A programmer should be aware of the Python reserved words.
Python Keywords
The Python keywords act as the building blocks of a python program. These words have a specific meaning and can’t be used for other purposes. The keywords will always be available to a programmer for coding. You need not import them. To get a list of the Python reserved words, follow the below process-
Syntax:
>>> help(“keywords”)
Output:
Here is a list of the Python keywords. Enter any keyword to get any more help
None | break | except | in | raise |
False | await | else | import | pass |
and | continue | for | lambda | try |
True | class | finally | is | return |
as | def | from | nonlocal | while |
async | elif | if | not | with |
assert | del | global | or | yield |
As a rule, the above Python reserved words can’t be used as Python identifiers. Additionally, it is important to note that the keywords are case-sensitive. Hence, ‘break’ will not be the same as ‘Break’. There were 33 keywords in Python 3.7. However, the number increased to 35 in Python 3.8.
Use of Keywords in a Python Program
Python Identifiers
A python identifier is a name given to various entities like variables, functions, and classes. It helps a programmer to distinguish one entity from another entity. Below are a few rules to be kept in mind while naming an identifier-
1) An identifier can be a composition of alphabets (a-z) and numbers (0-9). The alphabets can be in uppercase or lowercase. An underscore can also be used in an identifier. However, we must note that Python is a case-sensitive language. Hence, “testing” will not be the same as “Testing”.
Valid identifiers: testing, variable_1
2) You cannot start an identifier with a number. Thus, 2square will be an invalid Python identifier. However, square2 will be a valid identifier.
3) You cannot use reserved keywords as Identifiers. Thus, “break” will be an invalid identifier.
An invalid identifier
Source: Self
4) You can use ‘_’ as a special character in the naming of a variable. However, there is a restriction on the use of other special characters like ‘&’, and ‘@’, and ‘!’.
Valid Identifier: test_name
Invalid Identifier: abc@
The above identifier will be an invalid identifier
Source: Self
5) There is no restriction on the length of a Python identifier.
You can use any name for an identifier. However, it makes sense to name a variable so that it is easy to understand the use of the variable. You can use sum= a+ b instead of s=a+b.
Difference between a Python Keyword and a Python Identifier
S.No | Keyword | Identifier |
1 | A keyword refers to a predefined word that python reserves for working programs that have a specific meaning, You can’t use a keyword anywhere else. | Python Identifiers are the different values that a programmer can use to define various variables, integers, functions, and classes. |
2 | A keyword can specify the type of entity. | An identifier can identify a single entity (a variable, a class, or a function). |
3 | All the keywords except ‘True’, ‘False’, and ‘None’ start in lowercase letters. | The first character can be a lowercase letter or an uppercase letter. However, an identifier can’t start with a digit. |
4 | Keywords are generally in lower case. | A variable can be in uppercase or lowercase letters. |
5 | Python Keywords comprise alphabetical characters. | An identifier can comprise alphabets, numbers, and underscore. |
6 | There is no use of special characters. | No special character is used except underscore (‘_’). |
7 | A few examples of Python keywords are: True, False, else, import, finally, is, and global | A few examples of identifiers are testing, sq4 sides, area_square, etc. |
FAQs on Python Keywords and Identifiers
Q: Is list a reserved word in Python?
A: In python, the keywords act as reserved words. The list() behaves like a constructor that returns a Python list. The syntax of a list() is :
list([iterable])
The above constructor can take a single value as an argument. ‘Iterable’ refers to any iterable object or a sequence (tuples, strings), or a dictionary and a set. The constructor will return a list depending on the argument passed. list() returns an empty value if no parameters are passed.
Hence, ‘list’ is not a Python reserved word.
Q: Which is not a Python reserved word?
A: A keyword in Python is a reserved word that can’t be used for other purposes. They perform an internal operation. A programmer can’t use the reserved words as Python identifiers. Below is a list of the Python Reserved keywords list-
None | break | except | in | raise |
False | await | else | import | pass |
and | continue | for | lambda | try |
True | class | finally | is | return |
as | def | from | nonlocal | while |
async | elif | if | not | with |
assert | del | global | or | yield |
Any word that is not present in the above list is not a reserved keyword in Python. You can use the unreserved keywords as identifiers in naming variables and functions.
Q: Is break a reserved keyword in Python?
A: Python reserves a certain set of keywords for its internal operations. A programmer can’t use those keywords as identifiers. ‘break’ is one of the reserved keywords in Python. Hence, you can’t use it while naming variables. Refer to the above table to know the Python reserved words.
Q: What is meant by reserved words?
A: Python reserved words are also known as Python keywords. Python reserves these words for various internal operations. Thus, programmers can’t use it for naming variables and classes. The list of the reserved keywords is mentioned above.
Leave a Reply