Python 3.1 - Variables, Identifiers and Keywords


Data is very important in every field so it should be handled very carefully. Almost all applications that we see in digital world, need data. Best examples are Facebook and YouTube which contain an infinite amount of data. Every day, millions or billions of data is uploaded and downloaded on\from Facebook and YouTube such as images, videos, user names, passwords, date of birth, age, e mails etc. Every data also has some type. For example, when we talk about a user’s age such as ‘Age=28’ then 28 is called the data and ‘number’ is the type of data since 28 is a number.

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

10 is first stored into a memory location and then the name ‘x’ is given to it, then 20 is stored into another memory location and the name ‘y’ is given to it. Python works differently with variables as compared to other languages like C, C++, Java. In languages like C and Java, when we declare a variable named ‘X’ with value 10 then a block of memory is created and the value 10 will be stored at that memory location. If we store same value into another variable named ‘Y’, then a different memory will be allocated to variable ‘Y’ and the value 10 will be stored. But in Python if we store a value 10 in variable ‘X’ and then if we store value 10 in variable ‘Y’, then no different memory location will be created. The variable ‘Y’ will now point to the same location which ‘X’ is pointing to. It means Python saves the memory as compared to other languages. Python is memory efficient since it can store same values at one memory location for different variables. Observe the following image –
 

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 –


 

In first two statements ‘x=10 and y=20’, we have stored two different values 10 and 20 into two different variables ‘x’ and ‘y’ then of course these values will have different memory locations (….8816 and ….9136). In next two statements ‘x=10 and y=10’, we stored same value 10 into two different variables ‘x’ and ‘y’ then the same memory location (….8816) is used. This proves that Python is a memory efficient programming language unlike other languages. 

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

Popular posts from this blog

Python 4.4 - Points to remember

Python 3.6 - Points to remember

Python 3.3 - Datatypes in Python