Learning Python Part-18: Python Data Type Conversion

The process of converting the value of one data type (integer, string, float) to another data type is called type conversion. Python has two types of type conversion.

  • Implicit Type Conversion
    • In Implicit type conversion, Python automatically converts one data type to another data type. 
    • This process doesn’t need any user involvement.
    • Python avoids the loss of data in Implicit Type Conversion.
    • Implicit Type conversion example:

    • Output:

  • Explicit Type Conversion (AKA Type casting)
    • In Type Casting loss of data may occur as we enforce the object to specific data type.
    • In Explicit Type Conversion, users convert the data type of an object to required data type.
    • We use the predefined functions like int(), float(), str() to perform explicit type conversion.
    • This type conversion is also called typecasting because the user casts (change) the data type of the objects.
    • Syntax : (required_datatype)(expression)
  • Explicit Type example:
    • Without type casting

    •  Result will be error as below


    • With type casting

    •  Output:


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.