Python Data Type Conversions Practice
SECTION A: Theory Questions
1. What does “data type conversion” mean in Python?
2. Which function is used to convert a number to a string?
3. What happens when you apply the int() function to a float?
4. How do you convert a string like '5' into an integer?
5. Which function converts an integer to a floating-point number?
6. What function converts a tuple to a list?
7. How can you convert a list into a tuple?
8. What function converts a list into a set?
9. Which functions are used to convert a decimal number to binary and hexadecimal
representations?
10. What arguments must be specified when converting from binary and hexadecimal
strings back to decimal using int()?
SECTION B: Code Analysis
11. What will be the output of the following code?
num = 3 num2 = str(num) print(type(num2))
12. What does the following code print?
str1 = '10' int1 = int(str1) print(int1 + 5)
13. What happens in this example?
f = 4.7 print(int(f))
14. Explain the output:
tup1 = (1, 2, 3) list1 = list(tup1) print(list1)
15. Predict the output:
num = 8 print(bin(num)) print(hex(num))
SECTION C: Coding Challenges
16. Convert the integer 15 into a string and print both the value and its type.
17. Convert the string '25' into an integer, then convert it into a float. Print all three values
and their types.
18. Convert the tuple (10, 20, 30) into a list, add the number 40 to it, and then convert it
back into a tuple. Print the final tuple.
19. Convert the list [2, 2, 4, 6, 6, 8] into a set, and explain what happens to duplicate
values.
20. Convert the number 12 into binary and hexadecimal format, then convert both
representations back to decimal.
SECTION D: Challenge Section
21. Create a dictionary named conversion_examples with the following keys and values:
- 'string_to_int': result of converting '50' to an integer
- 'int_to_float': result of converting 10 to a float
- 'tuple_to_list': conversion of (5, 10, 15) to a list
- 'decimal_to_binary': binary form of 9
- 'binary_to_decimal': conversion of '0b1001' back to decimal
Print each key and value pair using the items() method.
ANSWER SHEET
1. It means changing a variable’s data type from one type to another (e.g., int → str).
2. str()
3. It removes the decimal part and returns only the integer portion.
4. Using int('5')
5. float()
6. list()
7. tuple()
8. set()
9. bin() and hex()
10. 2 and 16, which represent the base numbers of binary and hexadecimal respectively.
11. Output:
12. Output: 15
13. Output: 4 (the decimal part is dropped)
14. Output: [1, 2, 3]
15. Output: 0b1000 and 0x8
16. num = 15; s = str(num) → Output: 15
17. str_num = '25'; int_num = int(str_num); float_num = float(int_num)
18. tup1 = (10, 20, 30); list1 = list(tup1); [Link](40); tup2 = tuple(list1) → (10, 20, 30, 40)
19. list1 = [2, 2, 4, 6, 6, 8]; set1 = set(list1) → {8, 2, 4, 6} (duplicates removed)
20. num = 12; bin(num) → 0b1100; hex(num) → 0xc; int('0b1100', 2) and int('0xc', 16) → 12
21. conversion_examples = {'string_to_int': int('50'), 'int_to_float': float(10), 'tuple_to_list': list((5,
10, 15)), 'decimal_to_binary': bin(9), 'binary_to_decimal': int('0b1001', 2)}