The process of converting a Python data type into another data type is known as type conversion. There are mainly two types of type conversion methods in Python, namely, implicit type conversion and explicit type conversion. Show
In this module, we will go through the following topics: So, without any further delay, let’s get started. Implicit Type Conversion in PythonIn Python, when the data type conversion takes place during compilation or during the run time, then it’s called an implicit data type conversion. Python handles the implicit data type conversion, so we don’t have to explicitly convert the data type into another data type. We have seen various examples of this kind of data type conversion throughout the tutorial. Don’t worry if you can’t recall it. Let’s see another example. Let’s add two Python variables of two different data types and store the result in a variable and see if the Python compiler is able to convert the data type of the resultant variable or not. a = 5 b = 5.5 sum = a + b print (sum) print (type (sum)) #type() is used to display the datatype of a variable Output: 10.5 In the above example, we have taken two variables of integer and float data types and added them. Further, we have declared another variable named ‘sum’ and stored the result of the addition in it. When we checked the data type of the sum variable, we can see that the data type of the sum variable has been automatically converted into the float data type by the Python compiler. This is called implicit type conversion. The reason that the sum variable was converted into the float data type and not the integer data type is that if the compiler had converted it into the integer data type, then it would’ve had to remove the fractional part and that would’ve resulted in data loss. So, Python always converts smaller data type into larger data type to prevent the loss of data. In some cases, Python cannot use implicit type conversion and that’s where explicit type conversion comes into play. Moving ahead, let’s learn more about it. Enroll yourself in Online Python Training in Sydney and give a head-start to your career in Python Programming! Get 100% Hike! Master Most in Demand Skills Now ! Explicit Type Conversion in PythonExplicit type conversion is also known as typecasting. Explicit type conversion takes place when the programmer clearly and explicitly defines the same in the program. For explicit type conversion, there are some in-built Python functions. The following table contains some of the in-built functions for type conversion, along with their descriptions. FunctionDescriptionint(y [base])It converts y to an integer, and Base specifies the number base. For example, if you want to convert the string in decimal numbers then you’ll use 10 as base.float(y)It converts y to a floating-point number.complex(real [imag])It creates a complex number.str(y)It converts y to a string.tuple(y)It converts y to a tuple.list(y)It converts y to a list.set(y)It converts y to a set.dict(y)It creates a dictionary and y should be a sequence of (key, value) tuples.ord(y)It converts a character into an integer.hex(y)It converts an integer to a hexadecimal string.oct(y)It converts an integer to an octal stringNow that we know the in-built functions provided by Python that are used for explicit type conversion, let’s see the syntax for explicit type conversion: Every value in Python has a datatype. Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes. There are various data types in Python. Python Numbers
Python List
Python Tuple
Python Strings
Python Set
Python Dictionary
Conversion between data typesData types can be converted using different type conversion functions like 4, 5, 6, etc.
Type ConversionThe process of converting the value of one data type (integer, string, float, etc.) to another data type is called type conversion. Python has two types of type conversion.
Implicit Type ConversionIn Implicit type conversion, Python converts one data type to another data type. This process doesn’t need any user involvement.
Addition of string(higher) data type and integer(lower) datatype
Explicit Type Conversion In Explicit Type Conversion, users convert the data type of an object to required data type. Users use the predefined functions like 4, 5, 6, etc to perform explicit type conversion.
What is data type conversion in Python?Type conversion is the process of converting a data type into another data type. Implicit type conversion is performed by a Python interpreter only. Explicit type conversion is performed by the user by explicitly using type conversion functions in the program code. Explicit type conversion is also known as typecasting.
What is data type conversion explain with an example?In computer science, type conversion, type casting, type coercion, and type juggling are different ways of changing an expression from one data type to another. An example would be the conversion of an integer value into a floating point value or its textual representation as a string, and vice versa.
What are the types of type conversion in Python?There are two types of Type Conversion in Python:. Implicit Type Conversion.. Explicit Type Conversion.. What are data type conversions?Data type conversion occurs automatically for different numeric types such as from floating-point database column values into integer C variables, and for character strings, such as from varying-length Ingres character fields into fixed-length C character string buffers.
|