Python is a high level general purpose programming language which has a clear/easy learning curve. Python programming language is massively used in various domains like Artificial Intelligence, Data Science, Web Development, Utilities Tools and Scripts and many more
Python is a Programming language, and the default Python we use is written in C programming language which is also referred to as CPython (Python implementation in C). There are various implementation of Python Programming language i.e Jython(in JAVA), Skulpt(in JS) e.t.c
To make everything easy: We refer CPython as Python
Datatypes conversion means the conversion of the datatype from one type to another type. For example, if you are converting string datatype to integer datatype that's a datatype conversion.
Python has two types of type conversion:
In this type of data conversion in python, python directly converts one data type to another data type. Data conversion is done automatically with the help of Implicit Type Conversion. Python converts the data from lower types to higher data types in order to prevent data loss. Data conversion is done automatically by the python interpreter.
Example:
>>>a=2
>>>b=3.2
>>>c=a+b #data is converted implicitly from integer to float.
>>>print(c)
5.2
>>>#Another example
>>>a=2
>>>b=2+3j
>>>c=a+b
>>>print(type(c))
<class 'complex'>
Explicit Type Conversion means conversion of datatypes with the involvement of the user. We can convert data to the
required type with the help of some functions like int(), float(), str(), complex(), etc.
Conversion syntax: cast-type(data).
>>>a='12'
>>>print(type(a))
<class 'str'>
>>>b=int(a) #cast to int
>>>print(type(b))
<class 'int'>
>>>c=float(a) #cast to float
>>>print(type(c))
<class 'float'>
>>>d=complex(a) #cast to complex
>>>print(type(d))
<class 'complex'>
Note:
Explicit type conversion is also called as type-casting in python.
Type Casting functions and conversionsSN | Functions | Conversions |
---|---|---|
1 | int() | string, float to int. |
2 | float() | string, int to float. |
3 | str() | int,float,list,tuple,di-ctionary to string. |
4 | complex() | int,float to complex. |
5 | set() | string, list, tuple to set. |
6 | list() | string,tuple,set,dictionary to list. |
7 | tuple() | string,list,set to tuple. |