In this article, you will learn about keywords (reserved words), List of Keywords and Identifiers (with naming rules & examples) in Python.
So, Let’s dive right in!
Before diving deep into the technical terms,
I want to ask you a question.
Have you ever written a single line of code ?
then, You are so… smart.
then, Listen carefully!
Whenever you write a program (code) in your computer,
You will see that each and every program consists of so many lines (sentences).
Each line is called an instruction.
Each instruction is made up of so many words.
The word may be either a user-defined word (Identifier) or a reserved-word (keyword).
Basically, A word is a building block of a program.
Let’s jump into our main topic which you are really searching for in this article. i.e., Keywords and Identifiers.
First of all, I would like to explain about Identifiers.
So that you can easily understand keywords.
Because, Identifiers and Keywords are sort of related to each other.
So, Here we go!
What are Identifiers in Python?
There is no only one way to define Identifiers.
We can define Identifiers in a few ways:
# 1st Way:
As the name indicates,
Identifiers are the smallest identifying unit in your program.
We can say – The smallest meaningful component in a program is known as an Identifier.
# 2nd Way:
A program consist of so many lines (i.e., Instructions), each line has a few words. Think about each word, each word is an Identifier.
Mind it – Identifiers are only the user-defined words.
Which indicates that, In any programming language we have also some reserved-words (Keywords) other than the user-defined words.
It simply means that:
All the words written in your program except Keywords are Identifiers.
We can call all the Keywords as an standard Identifier.
Because, Keywords are also an Identifying unit in your program.
# 3rd Way:
Let’s understand what actually an identifier is with a real world example.
Suppose, You have a shelf (collection) of books in your study table.
You can identify each of the books simply by their names.
Similarly, A name in Python Programming is called an Identifier.
Books are similar to ➡️ Programming elements such as variables, functions, classes, methods, modules and so on…
Name of each Books are similar to ➡️ Identifier
An Identifier is a name given to Programming elements (entities).
Which are used to identify or represent these programming elements such as variables, functions, arrays, classes, methods, modules and other objects.
In simple words:
An Identifier is a string (sequence of characters) of alphabets (A to Z or a to z), digits (0 to 9) and underscores (_).
The first character of which can only be either an Alphabet or an Underscore.
Which means that, An Identifier cannot start with a digit.
Rules for naming Python Identifiers:
- The first character of an identifier must be an alphabet [ uppercase (A to Z) or lowercase (a to z) ] or an underscore (_), And then it can be followed by any of the character, alphabet, digit, or underscore.
>>>Name = "Python" >>> Name 'Python' >>> name = 'C++' >>> name 'C++' >>> _standard = 12 >>> _standard 12 >>> roll_no_123 = 'Harry' >>> roll_no_123 'Harry' >>> B16_Cat = 'Tom' >>> B16_Cat 'Tom'
- It cannot start with a digit.
Python Console>>> 1number = 739823... ^ SyntaxError: invalid syntax >>> number12 = 739823... >>> number12 739823...
- The only special character used in Python identifier is an underscore (_).
- No special characters like @, #, ! , $, %, ^, & or punctuation characters ( ? , “ , : , ; etc.) is allowed.
Python Console>>> @gmail = "Imran" ^ SyntaxError: invalid syntax >>> wow! = "happy" ^ SyntaxError: invalid syntax >>> ca$h = 100 ^ SyntaxError: invalid syntax
- Keywords (reserved words/predefined-words) cannot be used as identifiers.
Python Console>>> for = 10 ^ SyntaxError: invalid syntax >>> break = "break the execution" ^ SyntaxError: invalid syntax
Why above examples are showing invalid syntax ?
break are keywords in Python.
- We cannot use a space for separating multiple words. Multiple words can be separated using an underscore.
Python Console>>> what is your name = 'Imran' ^ SyntaxError: invalid syntax >>> what_is_your_name = 'John' >>> what_is_your_name 'John'
- Python is a case sensitive language. This means that student and STUDENT are not same. Because one word (student) is in lowercase and the other in uppercase respectively.
Python Console>>> Student = 'Future' >>> STUDENT = 'Past' >>> student = 45 >>> print(student) 45 >>> print(STUDENT) past >>> print(Student) Future
- Identifiers can be of any length.
Python Console>>> abcdefghijklmnopqrstuvwxyz = 'alphabet' >>> abcdefghijklmnopqrstuvwxyz 'alphabet'
Python Keywords are standard identifiers.
Keywords are the predefined words or reserved words, that have some special meaning and have specific purpose in Python language.
Keywords are generally used for defining the syntax and structure of Python language.
The meaning of Python Keywords has already been described to the Python interpreter.
These meaning cannot be changed.
Thus, Keywords cannot be used as a Variable name, function name, class name or any other Identifier.
Because, That would try to change the existing meaning of the keyword, Which is not allowed.
Points to be noted:
- In Python, Keywords are case sensitive.
- All the Python Keywords except True, False and None must be written in lowercase letters only.
List of Keywords in Python:
At the time of writing this article Python 3.8.2 is the latest version.
There are total 35 Keywords in Python 3.8.
But, The number can vary from one version to another.
The list of all the Keywords in Python 3.8. is given below:
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