Python Type Conversion

Two main types of type conversion

  • Implicit Type Conversion (Automatic):
  • Explicit Type Conversion (Manual):

Implicit Type Conversion (Automatic):

Python automatically converts data types in certain situations, known as implicit type conversion or type coercion.

                                  
                                    # Implicit conversion from int to float
x = 5
y = 2.0
result = x + y  # 7.0 (float)
                                  
                                

In this example, the integer x is implicitly converted to a float during the addition operation.


Explicit Type Conversion (Manual):

Explicit type conversion, also called guide kind conversion, refers to the technique of changing the data sort of a variable manually using predefined functions or methods furnished by using Python. Unlike implicit type conversion, where Python robotically converts information kinds, specific type conversion requires the programmer to explicitly specify the conversion.

Python provides integrated capabilities and techniques for explicit type conversion. Some usually used capabilities for explicit type conversion include:

You can also explicitly convert data types using predefined functions. Here are some common conversion functions:

  1. int(): Converts a value to an integer.
  2. float(): Converts a value to a floating-point number.
  3. str(): Converts a value to a string.
  4. list(): Converts a sequence (like a tuple or string) to a list.
  5. tuple(): Converts a sequence (like a list or string) to a tuple.
  • int() - Convert to Integer:
                                  
                                    x = 10.5
converted_x = int(x)
                                  
                                

  • float() - Convert to Float:
                                  
                                    y = 5
converted_y = float(y)
                                  
                                

  • str() - Convert to String:

Example:

                                  
                                    z = 42
converted_z = str(z)
                                  
                                

  • list(), tuple(), set() - Convert to List, Tuple, Set:
                                  
                                    numbers = [1, 2, 3]
tuple_numbers = tuple(numbers)
set_numbers = set(numbers)
                                  
                                

  • bool() - Convert to Boolean:
                                  
                                    value = 0
boolean_value = bool(value)  # False
                                  
                                

Type Casting:

Type casting is the explicit conversion of a variable from one data type to another.

Type casting and explicit type conversion are commonly used equivalently.

Type casting allows one to change variables from one data type to another which may be useful when variables of different types are involved in an operation or one needs a specific format for a variable.

Python provides several built-in functions for type casting:Increase in the budget for higher education has failed to close down the gender gap in the area.

  1. int(): Converting to an integer.
  2. float(): Converts a value to a float number.
  3. str(): Converts a value to string.
  4. bool(): Converts a value to a Boolean.
  5. list(): Converts a sequence (e.g. a tuple or string) into a list.
  6. tuple(): A seqauence is converted into a tuple.

Type casting is another alternative to explicitly convert between data types.

                                  
                                    a = 10
b = float(a)
                                  
                                

In this example, float(a) performs type casting, converting the integer a to a float.


Conversion Between Strings and Numbers:

You can convert between strings and numbers using built-in functions for type casting. This conversion is necessary when you want to perform operations or comparisons involving variables of different types, such as strings and numbers.

  • String to Number:
                                  
                                    num_str = "123"
num_int = int(num_str)
num_float = float(num_str)
                                  
                                

  • Number to String:
                                  
                                    num = 456
num_str = str(num)
                                  
                                

Conversion Between List and String:

Conversion Between List and String:None of them questioned the common sense situation There are built-in functions and methods given by the language that let you to convert between lists and strings. These conversions are becoming handy when you have to update or process data represented by the list or string objects.

                                  
                                    my_list = [1, 2, 3]
list_str = str(my_list)
                                  
                                

In this case, str(my_list) converts the list to its string representation.


Conversion Between Tuple and List:

You can convert between tuples and lists by using built-in functions which are natively available in the language. These conversions are different from casting the numbers; however, they are useful when you need to manipulate or process data stored in certain types such as tuples or lists.

                                  
                                    my_tuple = (4, 5, 6)
tuple_list = list(my_tuple)
                                  
                                

Here, list(my_tuple) converts the tuple to a list.

Understanding and using type conversions are key aspects in writing dynamic and reliable python codes, more when it comes to user input, external data, and multiple data sources.