Python Typecasting Questions (All Datatypes)
Integer (int)
1 Convert a float 15.9 into an integer. What is the result?
2 Typecast the string '250' into int and subtract 50.
3 What happens if you convert '12.5' to int?
4 Convert boolean True and False into int.
5 Typecast a negative float into int.
Float (float)
1 Convert integer 100 into float.
2 Typecast string '45.67' into float.
3 What error occurs when converting 'python' into float?
4 Convert boolean True into float.
5 Typecast a very large integer into float.
Complex (complex)
1 Convert integer 7 into a complex number.
2 Typecast float 3.5 into complex.
3 Convert string '2+5j' into complex.
4 What is the imaginary part when converting int to complex?
5 Convert boolean False into complex.
String (str)
1 Convert integer 999 into string.
2 Typecast float 6.28 into string.
3 Convert boolean True into string.
4 Convert list [1, 2, 3] into string.
5 What happens when you typecast None into string?
Boolean (bool)
1 Convert integer 0 into boolean.
2 Convert integer -1 into boolean.
3 Typecast empty string '' into boolean.
4 Typecast string 'Python' into boolean.
5 Convert empty list [] into boolean.
List (list)
1 Convert tuple (1, 2, 3) into list.
2 Typecast string 'hello' into list.
3 Convert set {1, 2, 3} into list.
4 Convert dictionary {'a':1, 'b':2} into list.
5 Typecast range(5) into list.
Tuple (tuple)
1 Convert list [4, 5, 6] into tuple.
2 Typecast string 'abc' into tuple.
3 Convert set {7, 8, 9} into tuple.
4 Convert dictionary {'x':1, 'y':2} into tuple.
5 Typecast range(3) into tuple.
Set (set)
1 Convert list [1, 2, 2, 3] into set.
2 Typecast tuple (4, 4, 5) into set.
3 Convert string 'banana' into set.
4 Convert dictionary {'a':1, 'b':2} into set.
5 Typecast range(6) into set.
Dictionary (dict)
1 Convert list of tuples [('a',1), ('b',2)] into dictionary.
2 Typecast tuple of key-value pairs into dict.
3 Why does converting [1,2,3] into dict cause an error?
4 Create a dictionary using two lists with dict().
5 Can a set be directly converted into a dictionary? Explain.
Range (range)
1 Convert range(5) into list.
2 Convert range(1, 10, 2) into tuple.
3 Typecast range(4) into set.
4 Can a string be converted into range? Why?
5 Convert range into list and then into set.
NoneType
1 Convert None into string.
2 What happens when you try to convert None into int?
3 Typecast None into boolean.
4 Can None be converted into float? Explain.
5 Compare typecasting None with empty values like '' and [].