0% found this document useful (0 votes)
16 views2 pages

Document 1

Type conversion in Python involves changing an object's data type using conversion functions, categorized into implicit and explicit types. Implicit type conversion occurs automatically without user involvement, while explicit type conversion requires manual intervention and may risk data loss. Examples illustrate both types, showing how operations can promote data types and how strings can be converted to other types like float, set, and list.

Uploaded by

isrealdaniel863
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)
16 views2 pages

Document 1

Type conversion in Python involves changing an object's data type using conversion functions, categorized into implicit and explicit types. Implicit type conversion occurs automatically without user involvement, while explicit type conversion requires manual intervention and may risk data loss. Examples illustrate both types, showing how operations can promote data types and how strings can be converted to other types like float, set, and list.

Uploaded by

isrealdaniel863
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
You are on page 1/ 2

Type Conversion in Python Is the act of changing an object’s data type and this is

done by using the type conversion functions to directly convert one data type to
another

There are two types of Type Conversion in Python:


1. Python Implicit Type Conversion
2. Python Explicit Type conversion

In the implicit type conversion, the Python interpreter automatically converts one
data type of objects to another using specified functions without any user
involvement preventing data loss.
Example
x = 10

print(type(x))

y = 10.6
print(type(y))

z=x+y
print(type(z))

Output
x is of type: <class 'int'>
y is of type: <class 'float'>
20.6
z is of type: <class 'float'>

As we can see the data type of ‘z’ got automatically changed to the “float” type
while one variable x is of integer type while the other variable y is of float type.
The reason for the float value not being converted into an integer instead is due
to type promotion that allows performing operations by converting data into a
wider-sized data type without any loss of information. This is a simple case of
Implicit type conversion in Python.
Explicit Type Conversion in Python
In Explicit Type Conversion in Python, the data type is manually changed by the
user as per their requirement. With explicit type conversion, there is a risk of data
loss since we are forcing an expression to be changed in some specific data type.

Example
# initializing string
s = "10010"

# printing string converting to float


a = float(s)
print (a)
Output:
After converting to float : 10010.0

# initializing string

s = 'geeks'# printing string converting to set

c = set(s)

print (c)

# printing string converting to list

c = list(s)

print (c)
Output:
After converting string to set : {'k', 'e', 's', 'g'}
After converting string to list : ['g', 'e', 'e', 'k', 's']

You might also like