0
0
Pythonprogramming~20 mins

How variable type changes at runtime in Python - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Runtime Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of variable type changes?
Consider the following Python code where a variable changes its type during execution. What will be printed?
Python
x = 10
print(type(x))
x = 'hello'
print(type(x))
A<class 'int'>\n<class 'str'>
B<class 'str'>\n<class 'int'>
C<class 'int'>\n<class 'int'>
DTypeError
Attempts:
2 left
💡 Hint
Remember that Python variables can hold values of any type and can change types anytime.
Predict Output
intermediate
2:00remaining
What is the final type of variable after reassignment?
Look at this code where a variable is reassigned multiple times. What is the type of variable 'data' at the end?
Python
data = 3.14
print(type(data))
data = [1, 2, 3]
print(type(data))
data = {'a': 1}
print(type(data))
A<class 'int'>\n<class 'list'>\n<class 'dict'>
B<class 'float'>\n<class 'dict'>\n<class 'list'>
C<class 'float'>\n<class 'list'>\n<class 'dict'>
DSyntaxError
Attempts:
2 left
💡 Hint
Check the type after each assignment carefully.
Predict Output
advanced
2:00remaining
What is the output when variable type changes inside a function?
What will this code print when a variable changes type inside a function?
Python
def change_var():
    var = 5
    print(type(var))
    var = 'changed'
    print(type(var))

change_var()
A<class 'str'>\n<class 'int'>
B<class 'int'>\n<class 'int'>
CNameError
D<class 'int'>\n<class 'str'>
Attempts:
2 left
💡 Hint
Variables inside functions behave the same way as outside in terms of type changes.
Predict Output
advanced
2:00remaining
What error occurs when using variable before assignment after type change?
What error will this code raise when a variable is used before assignment after changing its type?
Python
x = 10
print(x)
x = x + '5'
print(x)
ASyntaxError
BTypeError
CNameError
DNo error, prints 15
Attempts:
2 left
💡 Hint
Think about what happens when you add an integer and a string.
🧠 Conceptual
expert
2:00remaining
How does Python handle variable types at runtime?
Which statement best describes how Python handles variable types during program execution?
APython variables are dynamically typed and can change type when assigned new values.
BPython requires explicit type declarations before assigning values.
CPython variables have fixed types assigned at declaration and cannot change.
DPython variables are statically typed and checked at compile time.
Attempts:
2 left
💡 Hint
Think about how you can assign different types to the same variable name in Python.