In this article, let’s learn about Python Keywords and Identifiers. Keywords in Python are the reserved words and cannot be used as variable names, function names, or class names, etc. Python Identifiers are nothing but the name which we give to identify a variable, function, object, etc.
Python Keywords and Identifiers
Keywords are nothing but the reserved words used for defining a syntax or structure in the code, as they have a special meaning and purpose, and hence it cannot be used as an identifier.
For Example, let’s take ‘import’; it is a reserved keyword in Python used for importing a python module. If we are using it as a variable name, then we will be getting the “SyntaxError: invalid syntax” error.
There are 35 keywords in Python 3.8. In Python, Keywords are case sensitive all the keyword except True, False, and None should be in lower case.
We can get the list of all keywords in two different ways
- Using help()
- From the keyword module
1. Using help()
Open IDLE and type help(), to open the help utility of Python. Now type “keywords” command, to get the list of all keywords
>>> help() help> keywords Here is a list of the Python keywords. Enter any keyword to get more help. False class from or None continue global pass True def if raise and del import return as elif in try assert else is while async except lambda with await finally nonlocal yield break for not
2. From the keyword module
The other way of getting all the keywords is from the keyword module.
Open the IDLE and import keyword module, now run the command “keyword.kwlist”
>>> 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']
Identifiers are nothing but the name given to the various entities used in our code, such as variables, functions, classes, etc.
Below are the rules which we need to follow while creating an identifier
Rules to follow while creating an Identifier
- An identifier can be a combination of lowercase letters(a -z), uppercase letters (A-Z), numbers (0-9), and underscore (_). Example: myAge, new_year, val1 are valid identifiers.
- Identifiers cannot start with a number, 1name is invalid, whereas name1 is valid.
- An Identifier cannot have Special characters such as <>/%^&:'”,@#*~-+?|\! and Spaces in it.
- We cannot use Python keywords as an identifier, hence import, if, break, etc. are not valid identifiers.
- Identifiers are case sensitive; therefore, myAge and myage are considered as different identifiers.
Python also provides some special identifiers as well which uses underscore ‘_’ prefixed or suffixed
- Private Identifier _xxx
- System-defined name __xxx__
- Request private name mangling in classes __xxx
In the real world, we cannot directly keep working on the data. We may need to give names to the data so that we can save and process the data in that name. In short, we can consider the variable as a tag for the container. The type of the variable is the kind of data it stores.
In most of the strongly-typed languages such as C++ or Java, we need to declare the variable type before the variable since Python is not strongly typed, we don’t need to mention the variable type before the variable, and the Python interpreter will automatically assign them.
Using the = symbol we can perform a variable assignment, the syntax is
variable_name = data
Below are some of the valid assignments
val = 10
name = ‘CodingPointers’
price = 12.5
Whenever we assign an object to the variable the following happens, let’s take the int assignment as an example
- As a first step int type of object will be created in the memory
- Creation of the variable name ‘val’, The variable name as such will not have its own type, so if we check the type of the variable name, it gives us the type to which it points to.
- Now, attach the variable name with the object.
The Augmented assignment is nothing but combining arithmetic or bitwise operation and followed by an assignment.
For Example, from Python 2 onwards, we can write x = x + 1 as x +=1
The Augmented assignment operator supported by Python are the below
+=, -=, /=, %=, <<=, >>=, &=, ^=, |=
Python does not support either post-increment or post-decrement operations
x++ or x– will throw Syntax error
We can assign a single object to multiple variables in a single shot like below
>>> x = y = z = 10 >>> x 10 >>> y 10 >>> z 10
In the above code, we created an integer object (with the value 10), and all the three variables x, y, and z will refer to the same object int 10.
We can also assign multiple objects to multiple variables in a single assignment like below.
>>> x, y, z = 10, 22.5, 'CodingPointers' >>> x 10 >>> y 22.5 >>> z 'CodingPointers'
In the above code, we have assigned int, float, and string objects to the variable x, y, and z in a single assignment.
This approach comes in handy in many places; one of them would be swapping. Suppose in other programming languages such as Java, to swap two variables we will be following the below approach.
int x = 10; int y = 20; int temp = x; x = y; y = temp;
We needed a temp variable to hold one of the values to perform the swapping.
In Python, with the help of multiple assignments, we will no longer need a third variable to hold the value. We can directly do the swap like below.
>>> x, y = 10, 20 >>> x 10 >>> y 20 >>> x, y = y, x >>> x 20 >>> y 10
Leave a Reply