Friday, June 26, 2015

Python on the go - Revision of Variables

Variables in Python can be declared on the go. For example if you input your age with raw_input which inputs your age as a string , it can be converted into a number by declaring it as a float. eg float(age)    as shown below

age = raw_input('How old are you? ')
age2=float(age)
print "You are ",age2,"  years old"

Variables  can be  declared as numbers usually in the form  of a float or integer.  For accuracy, numbers are usually declared as floats.

But you don't always have to declare variables in Python . Sometimes Python recognizes it for you automatically. If you type  a variable like temp=31.0 (short form of temperature) ,  Python recognizes it as a float. In otherwords, any variable with  a decimal number assigned to it,  will be recognized as a float in Python. You can check this with the program as shown


temp=31.0

print type(temp)

The program will print the type of variable temp is - which is a float.


updated 12-7-2015


No comments:

Post a Comment