Type conversion simply means converting data from one data type(integer, string, list, tuple, dictionary, sets, etc) to another data.
It is supported by most of the programming languages.
Two types of type conversions are supported in python:
- Implicit
- Explicit
Contents
Previous Post’s Challenge’s Solution
Before we get into type conversion here is the solution for the challenge in the previous post.
Solution link: Click here for the solution.
Output:-
170.8888888888888888728
These are all arithmetic operators.
Implicit Type Conversion
Under this type, python automatically converts from one data type to another. No user involvement is required in the implicit type of type conversion.
Here’s an example where python saves data from drowning🏊♀️ into nothingness by converting an integer to a float.
num1 = 10 #it is an integer
num2 = 10.1 #it is an float
num3 = num1 + num2 #it will be converted to float to avoid data loss
print(num3) #20.1
print(type(num3)) #it got converted to float datatype
Output:-
20.1
<class 'float'>
In this program, we first created two variables(containers that store data in a python program):-
num1
num2
Next, we added their sum and assigned it to another variable num3
.
Python internally converted num3
to a float to avoid data loss.
Explicit Type Conversion
Now let’s see👀 how we can add “10” and 10.1 together.
num_str = "10" #it is a string because it is inside quotes
num = 10.1 #it is an float
num_result = num_str + num
print(num_result)
print(type(num_result))
Output:-
Traceback (most recent call last):
File "c:\Desktop\python-hub.com\test.py", line 3, in <module>
num_result = num_str + num
TypeError: can only concatenate str (not "float") to str
You must be wondering why this error?
Well, it is because we tried adding a string(a – z, some special characters) to an integer.
But, you might say🔉 that 10 is a number.
You are right✅, it is a number, but, to let python know it’s a number you should not❌ have used quotes.
However, there will be situations like this always coming up later on. To solve this python supports explicit type conversion.
This means you can tell python that "10"
is a number, not a string.
For this, you simply need to use an inbuilt function int()
:
num_str = "10" #it is a string because it is inside quotes
num = 10.1 #it is an float
num_result = int(num_str) + num #telling python that num_str
#is a number and to add it to num.
print(num_result)
print(type(num_result))
Output:-
20.1
<class 'float'>
There is a function like this for every data type in python. Like str()
for strings.
Conclusion
In nutshell, we saw two types of conversion in Python is converting one data type to another.
We saw that Implicit conversion is the one that python interpreter does.
And Explicit conversion is what the programmer does.
Challenge🧗♀️
Sum of Divisors
Write a Python program that prints the sum of all of divisors of 16 (excluding the number itself).
Go fast. I am waiting. Comment your answers below.
This was it for the post. Comment below suggestions if there and tell us whether you liked it or not. Do consider sharing this with your friends.
See you in the next post till then have a great time.😊