1.2.2 - Data type conversion

In the previous example we have read from the input and assign what we have read to a variable.

In the case of the age input is something preventing as from writting the age with letters?

Try this in CLI

>>> a = input("Enter a number : ")

>>>

Sixteen

>>> 

print (a)

How can we find what type is a variable? We can use the command type. Try the following code sequentially in the command line entering a floating point number (ex. 34.5).

>>> a = input("Enter a number : ")

>>> type (a)

As you can view variable a is a string. You must remember that anything that it is read from the input without conversion is treated as a string. To convert a string to an integer or to a floating point number we can use the int() or float() data conversion commands.

Now try the following commands sequentially:

>>> a = input("Enter a number : ")

>>> 17

>>> print (a)

>>> print(a+2)

The previous command produces an error. Why? Find out usind the following commands.

>>> type(a)

>>> a = int(a)

>>> type(a)

>>> print(a)

>>> print(a+2)

>>> a = float(a)

>>> type(a)

>>> print(a+5)