Python 3.1 - Variables, Identifiers and Keywords
|
When we write programs then we perform operations on data. Data type refers to the type of data. Type of data must be known before storing data into system because if data type is not known then it may lead to various problems such as undesirable output.
These are the common types of data that we use generally.
There are many more data types in Python that we will discuss later in this
chapter. In programming languages like C, C++ and Java, we must define the type
of data before using it. But in Python, we do not need to worry about it since
Python dynamically checks for data type and that is why we called Python a
Dynamically typed language (see strength/features of Python in chapter 1). We
need not declare anything, but we should know the type of data as a programmer.
For example, if I want to store a number in Python then I will write something like this –
>>> x=20
As you can see we did not specify anything to Python before storing number 20 into x (here x is a variable which we will discuss soon). Python will find out automatically that this is an Integer number. But in other language like C, we must tell the data type –
int x=20;
See? We told the C compiler that x is an Integer but don’t
worry about it in Python. Just store any data without worrying about its type.
Variables
A variable is nothing but the name of a location in
computer memory where a value is stored. If we want to store any single value,
then we will need a Variable. So when we create a variable then a space or a
location will be reserved in memory (RAM) and the value of that variable will
be stored on that location and that location will be named after the variable
name. Now tell me, if I ask you that add two numbers 5 and 10, what will be
your answer? you will say the answer is 15. Right? But how did you find the
answer? What process did you do on that data to find the final result?
First, you stored data 5 and 10 into your brain/memory (5 and 10 are stored somewhere in your brain/memory, that location of your brain is called a variable) then you did addition on data. After that, you again stored the result 15 into your brain (in variable) and finally you told the result 15. The same concept works with variables in a programming language. Got it? As you can see how much important are variables in any programming language. To store a value, we need a variable. For example –
x = 10 # x is a variable
How Python handles variables
In Python, when we store a value into a variable then Python stores that value in memory and assigns a name to that memory. That name is nothing but a variable. See the following example –
>>> x=10
>>> y=20
So everything cleared. Huh! You can also test it
practically. There is a function id(object) in Python that tells the identity
or address of an object. Just pass the variable as object and see what address
the variable points to.
Both variables will have the same identity(address) because they are pointing to same memory location as shown in the code below –
Identifiers and keywords
An identifier is a name given to variables, functions, lists, tuples, dictionaries, classes etc. We identify things in Python by identifiers. In real world, we know each other by their names and so does the Python. So all the names such as the name of a variable, the name of a function, the name of a list, the name of a string, etc. are called identifiers. A variable name is also an identifier. So in the example above you can say that ‘x’ and ‘y’ are variables and also identifiers because ‘x’ and ‘y’ are names of variables. While naming things in Python, we must follow some rules. So these are the rules that we must follow while creating an identifier –
Identifier naming rules -
·
An identifier must begin with an alphabet
(Uppercase or Lowercase) or an underscore (_)
·
No special characters are allowed in an
identifier (except underscore)
·
Identifier can be of any length (but always
choose meaningful name and length)
·
No spaces and tabs are allowed
·
After first letter, the identifier can have
alphabets, underscore and numbers
· Keywords or reserved words cannot be used as identifiers
Some Valid and Invalid identifiers are –
Valid –
X
roll_number
SALARY123
_Address
My_phone_number
Invalid –
123x # reason - must begin with an alphabet or underscore
@ddress # reason – no special symbols allowed
My phone number # reason – no spaces allowed
Remember # is used to create single line comment so we have
added comments to tell the reasons.
Keywords are reserved words in Python that have some
special meaning and can only be used for specified or pre-defined task or
purpose. We cannot use keywords as identifiers. A keyword can be used for
specified purpose only and we cannot use that keyword for any other purpose.
Python has hundreds of keywords available. Some of them are –
print |
for |
if |
else |
elif |
while |
import |
class |
def |
True |
False |
del |
break |
continue |
return |
These all are keywords which are reserved for some special
purposes and cannot be used for anything else. For example, ‘print’ is used to
display messages and values on screen, ‘for’ is used for looping, ‘if’ is used
for conditions, ‘del’ is used to delete an element etc. So we cannot use them
as an identifier or for any other purposes.
Comments
Post a Comment