Thursday, July 22, 2021

# 7. Type conversion in Python

                                     Type Conversion in Python

Python defines type conversion functions to directly convert one data type to another which is useful in day-to-day and competitive programming.


There are two types of Type Conversion in Python:

1.    Implicit Type Conversion

2.    Explicit Type Conversion


Implicit Type Conversion:

In Implicit type conversion of data types in Python, the Python interpreter automatically converts one data type to another without any user involvement.

>>> x = 10

        print("x is of type:",type(x))

        y = 10.6

        print("y is of type:",type(y))

        x = x + y

        print(x)

        print("x is of type:",type(x))

Output :

x is of type: <class 'int'>

y is of type: <class 'float'>

20.6

x is of type: <class 'float'>

As we can see the type of ‘x’ got automatically changed to the “float” type from the “integer” type. this is a simple case of Implicit type conversion in python.

Explicit Type Conversion:

In Explicit Type Conversion in Python, the data type is manually changed by the user as per their requirement.

In this method, Python need user involvement to convert the variable data type into certain data type in order to the operation required.

Mainly in type casting can be done with these data type function:

·        Int() : Int() function take float or string as an argument and return int type object.

·        float() : float() function take int or string as an argument and return float type object.

·        str() : str() function take float or int as an argument and return string type object.

 

Type Casting int to float:

Here, we are casting integer object to float object with float() function.

# Python program to demonstrate 

# type Casting 

# int variable

>>>a = 5

       # typecast to float

       n = float(a)

       print(n)

       print(type(n))

Output :

5.0

<class 'float'>

Type Casting float to int:

Here, we are casting float data type into integer data type with int() function.

# Python program to demonstrate 

# type Casting 

# int variable

a = 5.9

# typecast to int

n = int(a)

print(n)

print(type(n))

Output:

5

<class 'int'>

Type casting int to string:

Here, we are casting int data type into string data type with str() function.

# Python program to demonstrate 

# type Casting 

# int variable

a = 5

# typecast to str

n = str(a)

print(n)

print(type(n))

Output:

5

<class 'str'>

Type Casting string to int:

Here, we are casting string data type into integer data type with int() function.

# Python program to demonstrate 

# type Casting 

# string variable

a = "5"

# typecast to int

n = int(a)

print(n)

print(type(n))

Output:

5

<class 'int'>

 Type Casting String to float:

Here, we are casting string data type into float data type with float() function.

# Python program to demonstrate 

# type Casting 

# string variable

a = "5.9"

# typecast to float

n = float(a)

print(n)

print(type(n))

Output:

5.9

<class 'float'>

 

No comments:

Post a Comment

# 7. Type conversion in Python

                                      Type Conversion in Python Python defines type conversion functions to directly convert one data type t...