0
0
Pythonprogramming~5 mins

How variable type changes at runtime in Python - Quick Revision & Summary

Choose your learning style9 modes available
Recall & Review
beginner
What does it mean that Python variables can change type at runtime?
It means a variable can hold a value of one type at one moment, and later hold a value of a different type without errors.
Click to reveal answer
beginner
Show an example where a variable changes from an integer to a string.
x = 5 # x is an integer x = "hello" # now x is a string
Click to reveal answer
intermediate
Why can Python variables change type at runtime?
Because Python uses dynamic typing, it does not fix variable types when you write code. Types are checked when the program runs.
Click to reveal answer
beginner
What happens if you try to use a variable in a way that doesn't match its current type?
Python will raise an error if the operation is not supported for the variable's current type.
Click to reveal answer
beginner
Can you assign a list to a variable that was previously an integer? Why?
Yes, because Python variables are just names pointing to values. You can assign any type of value to the same variable name.
Click to reveal answer
What type does a Python variable have?
AA fixed type set when the variable is created
BAlways string
CThe type of the value it currently holds
DAlways integer
What happens if you assign a string to a variable that was holding an integer?
AThe variable changes to hold the string
BPython gives a syntax error
CThe variable keeps the integer value
DThe program crashes immediately
Which typing system allows variables to change type at runtime?
AStatic typing
BDynamic typing
CStrong typing
DWeak typing
If x = 10, then x = 'ten', what is the type of x now?
Aboolean
Binteger
Cfloat
Dstring
What error occurs if you try to add a number and a string in Python?
ATypeError
BSyntaxError
CNameError
DValueError
Explain how Python variables can change their type during program execution.
Think about how Python treats variable names and values.
You got /4 concepts.
    Give an example where a variable changes from one type to another and explain what happens.
    Use simple types like integer and string.
    You got /4 concepts.