Lesson 4: Type Conversion

type() function returns the type of your object. It’s a straightforward function and the examples below can help you have a better understanding of how it works.

We will also look at some other functions that can help you convert the type of a data. These are: int(), float() and str().

Function 1: type()

type() is another basic cool function in Python that helps you learn the type of a variable. Now that we know about variables and print function. By learning type() function we can print the type of a variable.

Used Where?

  • type() is used to identify the type of a variable.
  • Type information can be critical for your programming. And it can be helpful in so many ways.
  • For instance, your program might collect data and you might want to know the type of that data.
  • You might also want to make specific executions on specific data types. i.e.: arithmetic calculations on integrals or floats. Letter or word searching on strings etc.
  • Also, there can be conditional situations where you’d like to treat certain data depending on their types or take action based on this information.
  • There will be many other cases where type() function can offer value to you as a programmer.

Syntax do(s)

1) You can convert int or float types into str type as str can have any letter, number or symbols in them.

2) Str type is always represented in quotes while int, float and bool types don't have quotes.

Syntax don't(s)

1) You can't convert a string consisting of letters into integer or float type.

Example 1

>>> game_count = 21
>>> print(type(game_count))

<class ‘int’>

Tips

1- In next lessons we will learn about more complex data types such as lists, tuples and dictionaries.

These are usually referred to as composite data types as well because they can be consisted of multiple data types and values. type() function can also be used on identifying these data structures.

Example 2

>>> person1_weight = 121.25
>>> print(type(person1_weight))

<class ‘float’>

Function 2: int()

By using int() function you can try to convert from another type of data to int type.

Below you can see on the first line a number in quotes gets assigned to variable inc_count.

Because of quotes, variable registers this data as a string. Following commands of print, print the initial type and value of the variable on the screen and then you can see int() function converting the variable to int type.

After which print function will show that the type of the variable now is int.

You can note that before and after the int conversion data appears similarly on the output screen suggesting it would be hard to distinguish the types without type() function.

Example 3

>>> inc_count = “2256”
>>> print(type(inc_count))
>>> print(inc_count)
>>> inc_count = int(inc_count)
>>> print(type(inc_count))
>>> print(inc_count)

<class ‘str’>
2256
<class ‘int’>
2256

Function 3: float()

float() is a function that can be used to convert data to float type from other types.

Example 4

>>> inc_count = “2256”
>>> print(type(inc_count))
>>> print(inc_count)
>>> inc_count = float(inc_count)
>>> print(type(inc_count))
>>> print(inc_count)

<class ‘str’>
2256
<class ‘float’>
2256.0

Function 4: str()

Like int() and float(), str() helps you convert a data to str from another type.

Example 5

>>> inc_count = 2256
>>> print(type(inc_count))
>>> print(inc_count)
>>> inc_count = str(inc_count)
>>> print(type(inc_count))
>>> print(inc_count)

<class ‘int’>
2256
<class ‘str’>
2256

Advanced Concepts (Optional)

1- Not any type of data can be converted to any other type of data. For instance if a string is consisted of letters you will get an error when you try to use int() function on it simply because letters can’t be converted to integer type. 

However, if a string is consisted of numbers, it will be a successful operation.

2- You can convert pretty much any type of letter, number or character to string type. This will be the equivelant of typing your numbers in quotes and this representation stands for string type in Python.

As you can see in the example below, since the variable is consisted of letters, Python is having a hard time executing the int() function and throws an error.

We will look into these errors in detail in the upcoming lessons as it can be important to understand what they mean and what might be causing them.

Example 6

>>> my_data = “Anything”
>>> my_data = int(my_data)

ValueError: invalid literal for int() with base 10: ‘any’

Lesson 5

Data Structures

1925 miro


Joan Miro

1925

1925 by Joan Miro

“Have you installed Python yet?”

Having Python setup on your computer can expedite the learning experience and help you gain the real life skills in a non-simulation environment. Installing Python can have different nuances that can be confusing for everyone. We compiled a complete tutorial about the best and most convenient Python installing practices Check it out by clicking the button below.
Sandra Files