There are numerous types of data in Python programming. We must know their inter-conversion. How to covert these data types using inbuilt functions in Python i.e. Type Casting? Performing type conversions is one of the many things we can do with Python’s functions.
Let’s first discuss the Python data types in brief before proceeding towards the topic.
Python gives any number without a prefix as an integer. The values of the integer may be as long as we want. These are digits too in Python. We can’t have forever long integers if we have a supercomputer. These numbers also contain decimal digits.
Floating Point Numbers
For splitting the fractional digits and the integer floats are numbers that are written with a decimal point. Examples of floating-point numbers are 5.2, 8.17,3.31, etc.
In Python, complex numbers display this format:(the real part + the imaginary part). Examples of complex numbers are (4+1i), (6-4x), etc.
These may be any specific set of characters. Strings are referred to as str. We can write them by using quotation marks. We can use single quotes as well as double quotes for writing strings in Python. Strings can be any characters placed in quotation marks such as ‘9.1’, “hello”, etc.
Boolean Data Type
Boolean data may have one of two values, which are true and false. Boolean values are pretty vital in Python for different reasons. We can use Boolean values to define the truthiness of objects and values.
- Some data types are often mixed and matched thanks to implicit conversion.
- Implicit conversion is where the interpreter helps us out and automatically converts one data type into another, without having to explicitly tell it to try to do so.
- Against this, the explicit conversion is where we manually convert from one data type to a different one by calling the relevant function for the info type we would like to convert to.
- Once we wanted to print variety alongside some text. Before we could do this,we needed to call the str () function to convert the amount into a string.
- Once the amount was explicitly converted to a string, we could join it with the remainder of our textual string and print the result.
We should familiar with some type of conversion functions to perform typecasting in Python.
- Int (a base) is a popular function as it can convert any data type into an integer.
- float ( ) can turn any data type into a floating-point number.
- Oct ( ) converts integers to octal strings, and hex ( ) converts integers to hexadecimal strings.