Friday, January 8, 2010

Python is dynamically typed

Python is dynamically typed, the type of a object, or variable, is determined ar runtime. If you assign a int to g, g is int; then you re-assign a float to g, g is a float.

The Python build-in funtion type can be used to check the type of a object.

Try the following code:

g = 1
print(g, " g is ", type(g))
g =  5/2
print(g, " g is ", type(g))
g =  5//2
print(g, " g is ", type(g))
g = False
print(g, " g is ", type(g))
g = True * 1
print(g, " g is ", type(g))
g =  "Hello"
print(g, " g is ", type(g))
g =  ['hello', 'how', 'are', 'you']
print(g, " g is ", type(g))
g =  [1, 2, 3, 'you']
print(g, " g is ", type(g))
print(g[0], " g[0] is ", type(g[0]))
print(g[3], " g[3] is ", type(g[3]))
g = 3 + 2j
print(g, " g is ", type(g))