Type conversions (Also known as Type Casting) in Python

When we convert the value of one datatype into another datatype then it is known as type conversion. There are two types of type conversions:

Implicit Type Conversion
Explicit Type Conversion

Implicit Type Conversion

In this type conve…


This content originally appeared on DEV Community and was authored by pulkitgovrani

When we convert the value of one datatype into another datatype then it is known as type conversion. There are two types of type conversions:

  1. Implicit Type Conversion
  2. Explicit Type Conversion

Implicit Type Conversion

In this type conversion python automatically converts a value of one datatype into another datatype. It will convert datatype having lower preference to the datatype having higher preference.

Code Example:

Type Conversion

Output:

Type Conversion Output

Since float has higher preference so the output will also be in float. This is very useful because we will not loose our data.
But there are some values of one datatypes which can be converted another implicitly. That’s why we have explicit type conversions.

Converting integer to string

Code

Converting INT TO STR

Output

INT TO STR Output

Explicit Type Conversion

In explicit type conversion, the user has to mention explicitly to which value of one datatype he/she wants to convert to another datatype. This can be done by using predefined functions like int(), str(), float().

Code

Explicit Type Conversion

Output

Explicit Type Conversion Output

I hope you were able to understand type conversions in python. Do comment below in case of any doubts.


This content originally appeared on DEV Community and was authored by pulkitgovrani


Print Share Comment Cite Upload Translate Updates
APA

pulkitgovrani | Sciencx (2024-06-16T10:21:10+00:00) Type conversions (Also known as Type Casting) in Python. Retrieved from https://www.scien.cx/2024/06/16/type-conversions-also-known-as-type-casting-in-python/

MLA
" » Type conversions (Also known as Type Casting) in Python." pulkitgovrani | Sciencx - Sunday June 16, 2024, https://www.scien.cx/2024/06/16/type-conversions-also-known-as-type-casting-in-python/
HARVARD
pulkitgovrani | Sciencx Sunday June 16, 2024 » Type conversions (Also known as Type Casting) in Python., viewed ,<https://www.scien.cx/2024/06/16/type-conversions-also-known-as-type-casting-in-python/>
VANCOUVER
pulkitgovrani | Sciencx - » Type conversions (Also known as Type Casting) in Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/16/type-conversions-also-known-as-type-casting-in-python/
CHICAGO
" » Type conversions (Also known as Type Casting) in Python." pulkitgovrani | Sciencx - Accessed . https://www.scien.cx/2024/06/16/type-conversions-also-known-as-type-casting-in-python/
IEEE
" » Type conversions (Also known as Type Casting) in Python." pulkitgovrani | Sciencx [Online]. Available: https://www.scien.cx/2024/06/16/type-conversions-also-known-as-type-casting-in-python/. [Accessed: ]
rf:citation
» Type conversions (Also known as Type Casting) in Python | pulkitgovrani | Sciencx | https://www.scien.cx/2024/06/16/type-conversions-also-known-as-type-casting-in-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.