Python: Identifier, Reserved Words and Data Types
A name in Python program is known as identifier. It may be variable name, class name or method name.
Rules to define Identifiers in Python:
Alphabets: both upper case (A-Z) and lower case (a-z) can be used.
Digits: 0-9 can be used.
Symbol: underscores (_) can be used.
$ symbol is not allowed in Python, so there is a Syntax error in the above code.
Identifiers should not start with digits
For example 'total1123=10' is valid but '123total=10' is not.
Python identifiers are case sensitive
'total=10' and 'TOTAL=20' are treated as separate variables.
Keywords (predefined words) are not allowed to be used as identifiers. By mistake if you have used keywords as identifiers, you will get syntax error. For Example 'If=10'
There is no length limit for python identifiers. But it is not recommended to use too lengthy identifiers because for too lengthy identifiers the readability of the code will be down.
For example for Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=10, you will get no error but it is not a good programming practice.
Now, can you tell which Identifiers names are valid among the following?
Note 1: if an identifier starts with _ (underscore) symbol, it is private.
Note 2: if an identifier starts with __ (two underscores) symbols, it is strongly private
Note 3: if an identifier starts with __ (two underscores) symbols and ends with __ (two underscores) symbols, it is language specific identifier.
In Python some words are reserved to represent some meaning or functionality, these words are known as reserved words. There are 33 reserved words in Python.
True, False, None (like null in other languages)
and , or, not, is
If, else, elif (in java and C elseif)
while, for, break, continue, return, in, yield
Note 1: 'switch' and 'do while' is not available in Python.
try, except (like catch in Java), finally, raise, assert
as (to create aliases)
class (to declare classes)
def (to declare methods and constructors), pass
lambda (to create anonymous functions)
del (to delete a variable, method or class)
Note 2: All reserved words consist of only alphabets.
Note 3: Except first 3 (True, False, None), all reserved words are only lower case alphabets.
List out all keyword:
Import keyword module. In keyword module there is a variable 'kwlist'. Print the value of variable 'kwlist'.
Data type represents type of data present inside a variable. When compared with other languages, specialty of Python is that you need not declare data type in it.
For example if x=10; x is integer type. In python it is not required to specify the type explicitly; based on the provided value Python is going to assign a type to the variable automatically. That’s why python language is considered as dynamically typed language.
Inbuilt data types: There are 14 inbuilt data types in Python.
Numeric: Int, float, complex
Sequence: str, tuple, list
bytes, bytearray, range, frozenset
Note 1: int, float, complex, bool, str; these five are knows as Python’s fundamental data types.
Note 2: size range is not applicable to python data types because it is object type only. Size is not fixed for an object in all languages; based on the values the size is adjusted automatically.
Python inbuilt functions:
print(): inbuilt function to print the value of object.
type(): inbuilt function to know the type of a object.
Id(): inbuilt function to know the address of the object.
Note: In Python everything is an object including int, float, bool, method etc. An entity that has state and behavior is known as an object e.g., chair, bike, marker, pen, table, car, etc. For example: x=10; internally it is going to represent:
a is a reference variable that is pointing to the object.
To know the address of this object and where this object is going to be stored in memory' we can use id(x).
For any Query/Suggestion, do let us know in the comment section.