8. Variable conversion Pt. 2

  • Numbers can be integer (no decimal point) or float (decimal point)
  • The example below declares two variables, one of each type and prints them as integers, floating point numbers and as strings
  • if no type is specified, the variable will print as it was input
a = 3
b = 22.54

print(a)
print(int(a))
print(float(a))
print(str(a))

print(b)
print(int(b))
print(float(b))
print(str(b))

Tasks

  1. Type in the code and test that it works
  2. Save and label your code

Extension Task

When converting data types, the examples above are only temporary as they are enclosed within print statements. To permanently change the data type, reassign the variable as shown below

a = float(a)