Identifiers and Keywords in Python
Introduction
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. There are 33 keywords in Python 3.7 let’s go through all of them one by one. To get a list of the Python reserved words, follow the below process-
Syntax
help("keywords") |
List of All Python Keywords
and | Logical operators |
as | Alias |
assert | For debugging |
break | Break out of Python loops |
class | Used for defining Classes |
continue | Keyword used to continue with the Python loop by skipping the existing |
def | Keyword used for defining a function |
del | Used for deleting objects in python |
elif | Part of the if-elif-else conditional statement in python |
else | Same as above |
except | A Python keyword used to catch exceptions |
FALSE | Boolean value |
finally | This keyword is used to run a code snippet when no exceptions occur |
for | Define a Python for loop |
from | Used when you need to import only a specific section of a module |
global | Specify a variable scope as global |
if | Used for defining an “if” condition |
import | Python keyword used to import modules |
in | Checks if specified values are present in an iterable object |
is | This keyword is used to test for equality. |
lambda | Create anonymous function |
None | Represents a Null value in PYthon |
nonlocal | Declare a variable with non-local scope |
not | Logical operator to negate a condition |
or | A logical operator used when either one of the conditions needs to be true |
pass | This Python keyword passes and lets the function continue further |
raise | Raises an exception when called with the specified value |
return | Exits a running function and returns the value specified |
TRUE | Boolean value |
try | Part of the try except statement |
while | Used for defining a python while loop |
with | Creates a block to make exception handling and file operations easy |
yield | Ends a function and returns a generator object |
To see the latest list of python keywords, we can follow the following steps
Step 1: Open Python IDLE or Python Interpreter
Step 2: type the help() command to access the help shell.
Step 3: type keywords on the help shell, and you will get the following result of python keywords list inside the python interpreter.
What are Python Identifiers?
Python Identifier is the name we give to identify a variable, function, class, module or other object. That means whenever we want to give an entity a name, that’s called identifier.
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.
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
5) There is no restriction on the length of a Python identifier.
Python identifiers examples
Lets us see some examples of valid and invalid identifiers in python
Below are the list of valid python identifiers.
- abc123
- abc_de
- _abc
- ABC
- Abc
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 etc. |