What is Identifiers,Variables and keywords in Python?
Python Identifiers
Rules for making identifiers in Python
1. To form an identifier, use a sequence of letters either in lowercase (a to z) or uppercase (A to Z). However, you can also mix up digits (0 to 9) or an underscore (_) while writing an identifier.
For example – Names like rollnum, Class_1, and School_10th_reg_no are all valid identifiers.
2. You can’t use digits to begin an identifier name. It’ll lead to the syntax error.
For example – The name, 0roll is incorrect, but roll1 is a valid identifier.
2. Variables names can be alphabet, Underscore and digit but it can not be started with digit.
Example:-
valid variable names:- _A, A12_B, a
invalid variable names:- 1A, 12a, 1_A
3. The above expression will lead to the following actions.
'True', 'False' and 'None' are keywords having its first letter capital and other are small, except these three keywords all are started with small letter.
Python Variables
Python variables are used to store values which means reserved memory locations to store values. When you create variable then it means that you reserve some space in memory.
Some important fact about python variables:-
1. Variables don’t require declaration. If you don't declare variable type then it is implicitly declared at run time. It means that python variables are dynamically typed.
For example –
userid = 10
2. Variables names can be alphabet, Underscore and digit but it can not be started with digit.
Example:-
valid variable names:- _A, A12_B, a
invalid variable names:- 1A, 12a, 1_A
3. The above expression will lead to the following actions.
- Creation of an object to represent the value 10.
- If the variable (userid) doesn’t exist, then it’ll get created.
- Association of the variable with the object, so that it can refer the value.
The variable ‘userid’ is a reference to the value ’10’. Please refer to the illustration shown below.
Example.
| ~~~~~ | ----- ~~~~~~~~~ ------- ****( userid ) ----- Reference ------- ** 10 **| ~~~~~ | ----- ~~~~~~~~~ ------- ****Variable ----- ~~~~~~~~~~ ------- Object
4. Whenever the expression changes, Python associates a new object to the variable for referencing that value. And the old one goes to the garbage collector.
Example.
>>> userid = 10>>> id(userid)1916585700>>> userid = 11>>> id(userid)1716585986>>>
5. For optimization, Python builds a cache and reuses some of the immutable objects, such as small integers and strings.
6. An object is just a region of memory which can hold the following.
- The actual object values.
- A type designator to reflect the object type.
- The reference counter which determines when it’s OK to reclaim the object.
7. It’s the object which has a type, not the variable. However, a variable can hold objects of different types as and when required.
Python Keywords
Python keywords are reserved word which is already defined to interpreter of python. And these python keyword is not used as variable names.
There are 33 keywords in Python 3:-
['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']
we can easily see the list of python keyword in python 3 IDLE by typing the given script:
'True', 'False' and 'None' are keywords having its first letter capital and other are small, except these three keywords all are started with small letter.
There is no keyword like do while and switch as we have learn in C, C++.
Reference Books for Learning Python
Also share your knowledge about python to improve our knowledge and this post too.
Read also
share this post too...
ReplyDeletespread this post in your students.
ReplyDeleteThanks
ReplyDeleteinformative post about variable identifier and keyword.
Best IDE for C programming
All these codes were always difficult for me until the moment, when I have found a good teacher. He explained me how it all works in quite a short period.
ReplyDeleteAccording to my experience, Python is the best programming language. Even Java is not as good as Python. Though, everyone has his own opinion.
ReplyDelete