Saturday, August 6, 2011

Python Variable

#!/usr/bin/python

message = "Hello Bala"
print "\t", message, "\n"

#END

bala@bala-laptop:~/python$ python variable.py
    Hello Bala

bala@bala-laptop:~/python$

Variables are case sensitive so make sure you are printing the correct variable.

*******************************************************************************

#!/usr/bin/python

#Let us assign a string to a variable product

product = "Hi there how are you?"

print product

#type will tell us what type of variable is product

print "The variable product is of the type \t", type(product)


Int_Ten = 10

print "The variable Int_Ten is of the type \t", type(Int_Ten)

Float_Fifty = 50.00

print "The variable Float_Fifty is of the type \t", type(Float_Fifty)

#The memory location where 50 is stored can by give by a function called ID

print "The memory location of Float_Fifty is \t", id(Float_Fifty)

New_Variable = Float_Fifty

print "The memory location of New_Variable is \t", id(New_Variable)

Float_Fifty = 60.00

print "The mem locatn of Float_Fifty is \t", id(Float_Fifty), Float_Fifty

print "The mem locatn of New_Variable is still \t", id(New_Variable), New_Variable

*******************************************************************************

bala@bala-laptop:~/python$ python variabletype.py
Hi there how are you?
The variable product is of the type    
The variable Int_Ten is of the type    
The variable Float_Fifty is of the type    
The memory location of Float_Fifty is     27887104
The memory location of New_Variable is     27887104
The mem locatn of Float_Fifty is     27887080 60.0
The mem locatn of New_Variable is still     27887104 50.0
bala@bala-laptop:~/python$

No comments:

Post a Comment