Watch Me Code 1: Variables And Types


In [2]:
name = "Tony"
age = 43
wage = 12.50
happy_employee = True

In [3]:
#what is the type of name?
type(name)


Out[3]:
str

In [4]:
#what is the type  of age?
type(age)


Out[4]:
int

In [5]:
# convert age to float
float(age)


Out[5]:
43.0

In [6]:
# convert age to float then ask for type
type(float(age))


Out[6]:
float

In [8]:
# from float to int, you lose decimal places
lost_wage = int(wage)
lost_wage


Out[8]:
12

In [9]:
# switching types from str to int
name = 99
type(name)


Out[9]:
int

In [10]:
# ValueError because you cannot convert "Tony" to float
name = "Tony"
float(name)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-10-9df6ec6eb306> in <module>()
      1 # Type error
      2 name = "Tony"
----> 3 float(name)

ValueError: could not convert string to float: 'Tony'

In [ ]: