Python Keywords and Identifiers
In today's tutorial, I will explain you about how to use keywords and identifiers in python with examples.
Introduction to Python
Python is a high-level general purpose programming laguage. It is an object oriented programming laguage and provides features of object oriented programming to make programs more readable and easier.
Python program can be written using an interactive session using IPython or by directly executing Python scripts – text files that end in the extension .py using the Python interpreter.
Features of Python Language
- One distictive feature of python is that it is an interpreted language.
- Python has simple program structure and few reserved keywords with simple and clearly specific syntax.
- Python program structure is simple and easy which makes it easy to read and understand.
- Python has very portable freely available library and they are platform compatible
- Easy for debugging and testing.
- Python supports GUI applications that can be created and ported to many system calls, libraries and windows systems.
- As compared to shell scripting, Python provides a better structure and support for large programs.
Python language has many such useful features which makes it a user friendly and easy to use programming language.
Let's write your first python program.
In python for printing any output we use print() method.
Write the code shown below in your python IDE and run the program to see the output.
print("Hello world !!!")
Hello world !!!
Keywords in Python
If you are familiar with coding in general you would know that there are some reserved words in a programming language.
These reserved words are also called keywords.
These keywords have a special meaning and they are used for special purposes in Python programming language. These keywords cannot be used as name for a variable, function, class or as any other identifier. They are used in syntax and structure of a python program.
Python is a case-sensitive language which means Name and name are two different identifiers in python.
Following is a list of common keywords used in python.
If you want to list all the keywords available using python then use the below given code :
import keyword print(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']
In the above python code, import is a python keyword used to include and use a python module. keyword is a python module which contains a list of all keywords available in python.
Looking at all the keywords and trying to know their meaning and use can be a bit overwhelming. We will get to know the meaning and usage of these keywords as we continue learning.
Identifiers in Python
An identifier is name given to different entities like class, functions, variables, etc. It helps to recognize and differentiate between one entity to another.
There are certain rules to write identifiers in python. They are as follows:
- Identifiers can be a combination of lowercase (a-z) or uppercase (A-Z) alphabets, digits(0-9) or an underscore ( _ ). Valid examples for identifier names are myName, number_1, get_value.
- An identifier cannot start with a digit. 1number is invalid, but number1 is a valid name.
- Keywords cannot be used as identifiers.
global = 23
", line 1 global = 23 ^ SyntaxError: invalid syntax
- We cannot use special symbols !,@,#,$,%,etc. in identifier.
user@$ = 274
", line 1 user@$ = 274 ^ SyntaxError: invalid syntax
- An identifier can be of any length.
This_is_a_very_long_identifier_name = "This is a sentence" print(This_is_a_very_long_identifier_name)
This is a sentence
We need to follow these rules while naming variables, functions, classes and other objects that we use in our python program.
In this tutorial, we have discussed features of python programming language, learnt about keywords in python, identifiers and naming convention for identifiers in python.
If you have any questions please comment below and also share your views and suggestions in the comment box.