Python Interview Questions

32 Questions
Python

Python

Web DevelopmentFrontendBackendData Science

Question 8

How do you convert between different data types in Python?

Answer:

In Python, you can convert between different data types using built-in functions. This process is known as type casting or type conversion. Below are common type conversions along with examples:

Converting to String

  • str(): Converts a value to a string.

    num = 123
    str_num = str(num)  # "123"
    
    floating_point = 45.67
    str_float = str(floating_point)  # "45.67"
    
    lst = [1, 2, 3]
    str_lst = str(lst)  # "[1, 2, 3]"

Converting to Integer

  • int(): Converts a value to an integer. If converting a float, it truncates the decimal part. If converting a string, the string should represent a number.

    float_num = 45.67
    int_num = int(float_num)  # 45
    
    str_num = "123"
    int_str = int(str_num)  # 123
    
    bool_val = True
    int_bool = int(bool_val)  # 1

Converting to Float

  • float(): Converts a value to a floating-point number.

    int_num = 123
    float_num = float(int_num)  # 123.0
    
    str_num = "45.67"
    float_str = float(str_num)  # 45.67
    
    bool_val = False
    float_bool = float(bool_val)  # 0.0

Converting to Boolean

  • bool(): Converts a value to a Boolean. Any non-zero number or non-empty string is considered True, while zero, empty strings, and None are considered False.

    int_num = 0
    bool_int = bool(int_num)  # False
    
    float_num = 0.0
    bool_float = bool(float_num)  # False
    
    str_val = ""
    bool_str = bool(str_val)  # False
    
    none_val = None
    bool_none = bool(none_val)  # False

Converting to List

  • list(): Converts an iterable (e.g., tuple, string, set) to a list.

    tuple_val = (1, 2, 3)
    list_tuple = list(tuple_val)  # [1, 2, 3]
    
    str_val = "hello"
    list_str = list(str_val)  # ['h', 'e', 'l', 'l', 'o']
    
    set_val = {1, 2, 3}
    list_set = list(set_val)  # [1, 2, 3]

Converting to Tuple

  • tuple(): Converts an iterable (e.g., list, string, set) to a tuple.

    list_val = [1, 2, 3]
    tuple_list = tuple(list_val)  # (1, 2, 3)
    
    str_val = "hello"
    tuple_str = tuple(str_val)  # ('h', 'e', 'l', 'l', 'o')
    
    set_val = {1, 2, 3}
    tuple_set = tuple(set_val)  # (1, 2, 3)

Converting to Set

  • set(): Converts an iterable (e.g., list, string, tuple) to a set. This removes duplicate elements.

    list_val = [1, 2, 2, 3]
    set_list = set(list_val)  # {1, 2, 3}
    
    str_val = "hello"
    set_str = set(str_val)  # {'h', 'e', 'l', 'o'}
    
    tuple_val = (1, 2, 3, 3)
    set_tuple = set(tuple_val)  # {1, 2, 3}

Converting to Dictionary

  • dict(): Converts a list of key-value pairs (e.g., list of tuples) to a dictionary.

    list_of_tuples = [("name", "Alice"), ("age", 25)]
    dict_from_list = dict(list_of_tuples)  # {'name': 'Alice', 'age': 25}
    
    list_of_lists = [["name", "Bob"], ["age", 30]]
    dict_from_list_of_lists = dict(list_of_lists)  # {'name': 'Bob', 'age': 30}

Converting to Bytes

  • bytes(): Converts a string or other data to bytes.

    str_val = "hello"
    bytes_str = bytes(str_val, 'utf-8')  # b'hello'
    
    list_val = [1, 2, 3]
    bytes_list = bytes(list_val)  # b'\x01\x02\x03'

Summary

Type conversion in Python is straightforward using built-in functions. These conversions are crucial for ensuring data is in the correct format for operations, especially when dealing with user input, data from external sources, or when interfacing with different parts of a program.

Recent job openings