Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

Python data type conversion

In Python, you might often need to convert data from one type to another, such as converting a string to an integer or a float to an integer. This tutorial will guide you through converting data types in Python.

  1. Converting between numeric types:

    • Convert a float to an int: Use the int() function to convert a float to an integer. Note that this will truncate the decimal part of the float, not round it.

      float_num = 5.7
      int_num = int(float_num)
      
      print(int_num)  # Output: 5
      
    • Convert an int to a float: Use the float() function to convert an integer to a float.

      int_num = 5
      float_num = float(int_num)
      
      print(float_num)  # Output: 5.0
      
  2. Converting between strings and numeric types:

    • Convert a string to an int: Use the int() function to convert a string to an integer. The string must contain a valid integer value, or a ValueError will be raised.

      int_string = "42"
      int_num = int(int_string)
      
      print(int_num)  # Output: 42
      
    • Convert a string to a float: Use the float() function to convert a string to a float. The string must contain a valid float value, or a ValueError will be raised.

      float_string = "5.7"
      float_num = float(float_string)
      
      print(float_num)  # Output: 5.7
      
    • Convert an int or a float to a string: Use the str() function to convert an integer or a float to a string.

      int_num = 42
      float_num = 5.7
      
      int_string = str(int_num)
      float_string = str(float_num)
      
      print(int_string)  # Output: "42"
      print(float_string)  # Output: "5.7"
      
  3. Converting between lists, tuples, and sets:

    • Convert a list to a tuple: Use the tuple() function to convert a list to a tuple.

      my_list = [1, 2, 3]
      my_tuple = tuple(my_list)
      
      print(my_tuple)  # Output: (1, 2, 3)
      
    • Convert a list to a set: Use the set() function to convert a list to a set. This will remove duplicate items and order the elements arbitrarily.

      my_list = [1, 2, 3, 2, 1]
      my_set = set(my_list)
      
      print(my_set)  # Output: {1, 2, 3}
      
    • Convert a tuple or a set to a list: Use the list() function to convert a tuple or a set to a list.

      my_tuple = (1, 2, 3)
      my_set = {4, 5, 6}
      
      my_list_from_tuple = list(my_tuple)
      my_list_from_set = list(my_set)
      
      print(my_list_from_tuple)  # Output: [1, 2, 3]
      print(my_list_from_set)  # Output: [4, 5, 6] (order may vary)
      

In summary, Python provides various functions to convert data between different types, including numeric types, strings, and collection.

  1. Type casting in Python:

    • Description: Type casting is the process of converting a variable from one data type to another.
    • Code example:
      float_number = 3.14
      int_number = int(float_number)
      
  2. Implicit vs explicit type conversion in Python:

    • Description: Implicit type conversion is done automatically by Python. Explicit type conversion is done using functions like int(), float(), etc.
    • Code example:
      implicit_conversion = 5 + 3.14  # Implicit conversion to float
      explicit_conversion = int(3.14)
      
  3. Converting between different data types in Python:

    • Description: Python provides functions like int(), float(), str(), etc., for converting between different data types.
    • Code example:
      str_to_int = int("42")
      int_to_str = str(42)
      
  4. String to int conversion in Python:

    • Description: Use the int() function to convert a string containing digits to an integer.
    • Code example:
      string_number = "42"
      integer_number = int(string_number)
      
  5. Float to int conversion in Python:

    • Description: Use the int() function to convert a float to an integer. This truncates the decimal part.
    • Code example:
      float_number = 3.14
      int_number = int(float_number)
      
  6. Converting lists and tuples in Python:

    • Description: You can convert lists to tuples, tuples to lists, or use specific conversion functions like list() and tuple().
    • Code example:
      my_list = [1, 2, 3]
      my_tuple = tuple(my_list)
      
  7. Handling errors in data type conversion in Python:

    • Description: Use try-except blocks to handle errors that might occur during type conversion.
    • Code example:
      try:
          invalid_conversion = int("abc")
      except ValueError as e:
          print(f"Error: {e}")
      
  8. Type conversion functions in Python:

    • Description: Functions like int(), float(), str(), list(), and tuple() are commonly used for type conversion.
    • Code example:
      str_to_int = int("42")
      float_to_str = str(3.14)