Type Conversion
Type conversion means changing a value from one data type to another. Python provides built-in functions that make it easy to convert values between different data types.
What is Type Conversion?
Python variables can contain values of different data types. Sometimes a program needs a value in a different type to perform a particular operation.
For example, input received using input() is stored as a string. If we want to perform mathematical calculations, we can convert that string into an integer or float.
Type Conversion Flow
"25" → String
int()
25 → Integer
Common Type Conversion Functions
| Function | Purpose | Example |
|---|---|---|
| int() | Converts a value to integer | int("25") |
| float() | Converts a value to float | float("25.5") |
| str() | Converts a value to string | str(25) |
| bool() | Converts a value to boolean | bool(1) |
| list() | Converts an iterable to a list | list("ABC") |
| tuple() | Converts an iterable to a tuple | tuple([1, 2, 3]) |
| set() | Creates a set from an iterable | set([1, 2, 2]) |
1. Convert to Integer using int()
The int() function converts a compatible value into an integer.
age = "25"
age = int(age)
print(age)
print(type(age))
Output
25
<class 'int'>
2. Convert to Float using float()
The float() function converts a compatible value into a floating-point number.
price = "499.50"
price = float(price)
print(price)
print(type(price))
Output
499.5
<class 'float'>
3. Convert to String using str()
The str() function converts a value into a string.
age = 25
age_text = str(age)
print(age_text)
print(type(age_text))
Output
25
<class 'str'>
4. Convert to Boolean using bool()
The bool() function converts a value to either True or False.
value = 1
result = bool(value)
print(result)
print(type(result))
Output
True
<class 'bool'>
Type Conversion with input()
The input() function returns user input as a string. Therefore, numerical input usually needs to be converted before performing calculations.
age = input("Enter your age: ")
age = int(age)
print("Your age is:", age)
Example Input
Enter your age: 25
Output
Your age is: 25
Type Conversion for Calculations
Consider a simple student marks example. The marks are received from the user as strings, so we convert them into integers before calculating the total.
math = int(input("Enter Math marks: "))
python = int(input("Enter Python marks: "))
total = math + python
print("Total Marks:", total)
Example
Enter Math marks: 80
Enter Python marks: 90
Total Marks: 170
Convert String to List
The list() function can convert an iterable such as a string into a list of individual elements.
text = "SAM"
letters = list(text)
print(letters)
Output
['S', 'A', 'M']
Convert List to Tuple
The tuple() function can convert a list into a tuple.
courses = ["Python", "C#", "Java"]
course_tuple = tuple(courses)
print(course_tuple)
print(type(course_tuple))
Output
('Python', 'C#', 'Java')
<class 'tuple'>
Convert List to Set
The set() function can be used to create a set and remove duplicate values.
numbers = [10, 20, 20, 30, 30]
unique_numbers = set(numbers)
print(unique_numbers)
Output:
{10, 20, 30}
Implicit Type Conversion
Sometimes Python automatically converts one compatible numeric type into another during an operation.
For example, when an integer and a float are added, Python produces a float result.
number1 = 10
number2 = 5.5
result = number1 + number2
print(result)
print(type(result))
Output
15.5
<class 'float'>
Explicit Type Conversion
When the programmer manually converts a value using functions such as int(), float(), or str(), it is called explicit type conversion.
number = "100"
number = int(number)
print(number + 50)
Output
150
Invalid Type Conversion
Not every value can be converted into every data type. For example, normal text cannot be directly converted into an integer.
value = "Python"
number = int(value)
This causes a ValueError because "Python" is not a valid integer.
CIIT Learning Point
Always check the data type before performing an operation. Type conversion is especially important when accepting input from users or receiving data from APIs, files, and databases.
CIIT Practice Task
Write a Python program that accepts two numbers from the user, converts them into integers, and displays their sum.
number1 = int(input("Enter first number: "))
number2 = int(input("Enter second number: "))
sum = number1 + number2
print("Sum:", sum)
Output:
Enter first number: 10
Enter second number: 20
Sum: 30
Try different numbers and observe the result.
Summary :
Type conversion allows Python values to be changed from one data type to another. You learned common functions such as int(), float(), str(), bool(), list(), tuple(), and set(), along with implicit and explicit conversion. Type conversion is especially useful when working with user input and application data.