Friday, July 3, 2015

Python Basics-2

Variables – Variables are nothing but memory locations reserved by Python to store values. This means that when we create some thing like
Var = 10

We created a var variable which will be reserved some space by Python to store the value “10”

Python Provides us the facility not to define the type of the variables being used. That is in many other high level language we have to define

Int var =10 #define a int variable

Python has the capability to identify the type of the data being saved and reserves memory based on that. Hence by assigning different data types to variables, you can store integers, decimals or characters in these variables.

So why there is No Type Declaration in Python?
As Said above Python has the capability to figure out the types when we declare a variable in out Code. The reason is that variable does not contain any type information according to the Python. We already discussed that variables are just pointer to the memory location where we store the actual value. The memory value contains the type information. Since the value in the memory location holds the type value we don’t need the variable to hold the type values. Consider if we create many variables with same values and all these variables holding the Type value , there is considerably mis-use of the memory in our programs . In order to make the program execution fast with less memory usage python eliminates the need of declaring the type when defining the variables

Type declaration can be identified using “type” function like
>>> i = 10
>>> type(i)
<type 'int'>

Assigning – Assigning a value to a variables Is done in the same way as done in other languages using the “=” sign

counter = 100          # An integer assignment
miles   = 1000.0       # A floating point
name    = "John"       # A string

Python allows to assign multiple variables in one go as a = b = c = 1. In this case a memory is allocated with a value 1 and all 3 variables a, b and c point to this memory location.

Local and Global variables - If a variable is defined outside function then it is implicitly global. If variable is assigned new value inside the function means it is local. If we want to make it global we need to explicitly define it as global. Variable referenced inside the function are implicit global

Here is the sample Python Script that explains the use of local and global variables,


def hello():
    hello= ""
    global hai
    hai+="is Hai"
    print hello
    print hai

print hello()

Now the script will throw and error if it encountered the statement , Hello+=”Hello” , since we are accessing a Global variable and assigning the value. Since this is global variable is being accessed inside a function definition , it throws the below error.

[djas999@vx181d imp]$ python
Traceback (most recent call last):
  File "", line 14, in <module>
    print hello()
  File "", line 8, in hello
    hello+="is one"
UnboundLocalError: local variable 'hello' referenced before assignment

In order to access a global variable inside a function scope, we need to define the variable as global as we did that for the hai variables.

No comments :

Post a Comment