2) e. Variable Type Conversion

You can convert numbers from one type to another using “float” and “int” functions.

You can also use these functions to convert a string into a number if it is in a right format.

Or you can do the opposite and convert numbers into strings by using the “str” function. This function works not only with numbers but with any type of object in Python.

The “str” function is especially helpful when concatenating strings. This is because you can only add same data-types together.

string + string is valid.
string + int is wrong.

Similarly, float + float is valid.
float + int is valid (because they’re both numbers)
float + string is wrong.

In addition to that all the objects in Python can represent a boolean value. An object is considered “False” if its value or length is zero or empty. Conversely an object is considered “true” if its value or length is non-zero. Use the “bool” function to convert an object to a boolean value.

Use int operator when getting the input:

Normally, when you get an input from the user it will be a string. What if the user actually entered a number and you wanted to perform some calculation with it?

You can change this behavior while getting the input when you’re expecting a numeral input. You do this using the “int” function. Take a look at the program below: