In [ ]:
In [ ]:
print myinteger
print myfloat
print mystring
Confirm that the types are int, float, and string by running the following expressions.
In [ ]:
print type(myinteger) == int
print type(myfloat) == float
print type(mystring) == str
In [ ]:
myint * myfloat
int * string :
In [ ]:
float * string :
In [ ]:
string + float :
In [ ]:
string + string :
In [ ]:
In [ ]:
thethreeitems =
Test your work:
In [ ]:
print thethreeitems == "3, 5, and ten"
In [ ]:
In [ ]:
pythonlist =[2.43, 1, 0.92, 0, "0.38"]
print pythonlist
In [ ]:
print type(pythonlist[0]), type(pythonlist[1]), type(pythonlist[2]), type(pythonlist[3]), type(pythonlist[4])
numpy is an extremely useful library that has its own data types; you will frequently need to specify the types as float or possibly higher-precision float at the time you create them. The numpy data types are only sometimes compatible with the native data types.
In [ ]:
import numpy as np
numpyarray = np.array(pythonlist)
print numpyarray
numpy requires that all of the elements in its array are homogeneous, that is, they have to have the same type. What is the default data type of a after the conversion?
In [ ]:
type(numpyarray[0])
Ack! The results of type() do not portray the representation. For that we need
In [ ]:
print numpyarray.dtype
Which is not a numeric data type. We can cause it to be stored as numpy floats if we specify float when we convert it to numpy:
In [ ]:
numpyfloatarray = np.array(pythonlist, dtype="float")
In [ ]:
len(str(int(45)))
Test your expression on 45, 2, and 2.0. Does it work for all three?
In [ ]: