Type Conversion in Python Class 11 CBSE Computer Science: Implicit and Explicit Type Conversion
Class 11 · Computer Science
Type Conversion in Python (CBSE Class 11 Computer Science)
Different data types are used in Python to store different kinds of values. Sometimes, it becomes necessary to convert one data type into another. This process is called Type Conversion. Python supports two types of type conversion: Implicit Type Conversion and Explicit Type Conversion.
Learning Objectives
- Understand Type Conversion.
- Learn Implicit Type Conversion.
- Learn Explicit Type Conversion (Type Casting).
- Use built-in conversion functions.
- Understand when type conversion is required.
What is Type Conversion?
Type Conversion is the process of converting a value from one data type to another.
Types of Type Conversion
Type Conversion
│
├── Implicit Type Conversion
└── Explicit Type Conversion (Type Casting)
1. Implicit Type Conversion
Implicit Type Conversion is performed automatically by Python whenever it is safe to do so. The programmer does not need to write any conversion code.
Example
a = 10
b = 2.5
c = a + b
print(c)
print(type(c))
Output
12.5
<class 'float'>
Python automatically converts the integer 10 into the floating-point value 10.0 before performing the addition.
Dry Run Table
| Step | Operation | Result |
|---|---|---|
| 1 | a = 10 | Integer |
| 2 | b = 2.5 | Float |
| 3 | Python converts 10 → 10.0 | Automatic |
| 4 | 10.0 + 2.5 | 12.5 |
2. Explicit Type Conversion (Type Casting)
Explicit Type Conversion is performed manually by the programmer using Python's built-in conversion functions.
Common Type Conversion Functions
| Function | Converts To |
|---|---|
| int() | Integer |
| float() | Float |
| str() | String |
| bool() | Boolean |
| list() | List |
| tuple() | Tuple |
| set() | Set |
| dict() | Dictionary (where applicable) |
Converting Float to Integer
x = 25.75
y = int(x)
print(y)
Output
25
The decimal part is discarded (not rounded).
Converting Integer to Float
x = 45
y = float(x)
print(y)
Output
45.0
Converting Number to String
marks = 95
text = str(marks)
print(text)
print(type(text))
Output
95
<class 'str'>
Converting String to Integer
age = "18"
num = int(age)
print(num + 2)
Output
20
Converting String to Float
price = "125.50"
x = float(price)
print(x)
Output
125.5
Converting Values to Boolean
| Expression | Result |
|---|---|
| bool(0) | False |
| bool(1) | True |
| bool("") | False |
| bool("Python") | True |
Why Type Conversion is Needed
Type conversion is commonly required while accepting input from the user because the input() function always returns a string.
Without Type Conversion
age = input("Enter Age: ")
print(age + "5")
Output
185
With Type Conversion
age = int(input("Enter Age: "))
print(age + 5)
Output (if input is 18)
23
Visual Memory Box
| Conversion | Function |
|---|---|
| String → Integer | int() |
| Integer → Float | float() |
| Any Value → String | str() |
| Any Value → Boolean | bool() |
Think Like a Programmer
Predict the output.
a = "25"
b = int(a)
c = float(b)
print(c)
print(type(c))
Click to View Answer
25.0
<class 'float'>
Output Prediction Challenge
x = 10
y = "20"
print(x + int(y))
print(str(x) + y)
Click to View Answer
30
1020
Real-Life Example
Suppose a student's age is entered using the keyboard. The keyboard provides the input as text. Before performing calculations, Python must convert that text into an integer.
Common Beginner Mistakes
- Trying to add a string and an integer without conversion.
- Using
int()on a non-numeric string such as"ABC", which raises aValueError. - Assuming
int(25.9)rounds the value. It truncates the decimal part. - Forgetting that
input()always returns a string.
Memory Trick
- int() → Integer
- float() → Decimal Number
- str() → Text
- bool() → True or False
Exam Tips
- Remember the difference between implicit and explicit conversion.
- Practice using
int(),float(), andstr(). - Know that
input()returns a string. - Do not expect
int()to round decimal values.
CBSE Important Questions
2 Marks
- Differentiate between implicit and explicit type conversion.
- What is type casting?
3 Marks
- Explain the use of
int(),float(), andstr()with examples.
5 Marks
- Explain type conversion in Python with suitable examples and programs.
Practice Yourself
- Convert an integer into a float.
- Convert a numeric string into an integer.
- Write a program to accept two numbers using
input()and display their sum. - Use
type()to display the data type before and after conversion. - Predict the output of programs using different conversion functions.
Frequently Asked Questions (FAQs)
1. What is type conversion?
Type conversion is the process of converting one data type into another.
2. What is implicit type conversion?
It is automatic conversion performed by Python.
3. What is explicit type conversion?
It is manual conversion performed using functions such as int() and float().
4. Which function converts a string into an integer?
int().
5. Why is type conversion required with input()?
Because input() always returns a string.
Summary
- Type conversion changes the data type of a value.
- Python supports implicit and explicit type conversion.
- Common conversion functions include
int(),float(),str(), andbool(). input()returns a string, so conversion is often necessary before calculations.- Understanding type conversion helps prevent type-related errors in Python programs.