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:
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
type() function we can print the type of a variable.
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.
>>> game_count = 21
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.
>>> person1_weight = 121.25
Function 2: int()
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
>>> inc_count = “2256”
>>> inc_count = int(inc_count)
Function 3: float()
float() is a function that can be used to convert data to float type from other types.
>>> inc_count = “2256”
>>> inc_count = float(inc_count)
Function 4: str()
str()helps you convert a data to str from another type.
>>> inc_count = 2256
>>> inc_count = str(inc_count)
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.
>>> my_data = “Anything”
>>> my_data = int(my_data)
ValueError: invalid literal for int() with base 10: ‘any’
1925 by Joan Miro