0% found this document useful (0 votes)
2 views6 pages

Data Type Conversion in Python

Uploaded by

punamrattan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Data Type Conversion in Python

Uploaded by

punamrattan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Data Type Conversion in Python

Data type conversion refers to the process of changing one data type into another.
Python supports two types of conversion:

1. Implicit Type Conversion


2. Explicit Type Conversion (Type Casting)

This is important when:

 Performing arithmetic operations


 Taking user input
 Working with mixed data types

Implicit Type Conversion


Implicit type conversion is automatically performed by Python without programmer
intervention, usually to avoid data loss.
Example 1: Integer to Float
a = 10 # int
b = 2.5 # float

c = a + b
print(c)
print(type(c))

✔ Output:

12.5
<class 'float'>

🔹 Python converts int to float automatically.

Example 2: Boolean Conversion


x = True
y = 5

print(x + y)

✔ Output:

🔹 True is treated as 1, False as 0.

Implicit Conversion Rules

 int → float
 int → complex
 float → complex
 No automatic conversion from string to number

Example (Error Case)


a = "10"
b = 5
print(a + b) # TypeError
Explicit Type Conversion (Type Casting)
Definition

Explicit type conversion is done by the programmer using built-in functions.

Common Type Casting Functions

Function Converts to
int() Integer
float() Floating-point
str() String
bool() Boolean
list() List
tuple() Tuple
set() Set
Examples of Explicit Conversion
Example 1: String to Integer
x = "25"
y = int(x)

print(y)
print(type(y))

Example 2: Integer to Float


a = 10
b = float(a)
print(b)

Example 3: Number to String


age = 21
msg = "Age is " + str(age)
print(msg)

Example 4: Float to Integer


x = 9.8
print(int(x))

✔ Output:

🔹 Decimal part is truncated, not rounded.

Example 5: String to Float


price = "99.50"
print(float(price))
Boolean Type Conversion
Rules
bool(0) # False
bool(1) # True
bool("") # False
bool("Python") # True
bool([]) # False
bool([1, 2]) # True

Conversion Between Collections


Example 1: List to Tuple
lst = [1, 2, 3]
t = tuple(lst)
print(t)

Example 2: Tuple to Set


t = (1, 2, 2, 3)
s = set(t)
print(s)

✔ Removes duplicates.

Example 3: String to List


s = "ABC"
print(list(s))
Type Conversion with User Input
x = input("Enter number: ")
y = int(x)

print(y + 10)

🔹 input() always returns a string, so conversion is required.

Comparison Table
Feature Implicit Conversion Explicit Conversion
Done by Python Programmer
Control Automatic Manual
Safety Prevents data loss Programmer responsibility
Example 10 + 2.5 int("10")

Common Errors
❌ Invalid conversion:

int("abc") # ValueError

❌ Float string to int:

int("9.8") # Error

✔ Correct:

int(float("9.8"))

 Data Type Conversion: The process of converting one data type into another.
 Implicit Conversion: Automatic type conversion by Python.
 Explicit Conversion: Manual type conversion using built-in functions.

 Python converts types automatically when safe


 Use type casting when required
 input() always returns string
 Boolean conversion follows truthy/falsy rules

You might also like