Bird
Raised Fist0
Pythonprogramming~10 mins

How variable type changes at runtime in Python - Visual Walkthrough

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - How variable type changes at runtime
Assign value to variable
Check value type
Variable holds value and type
Reassign variable with new value
Check new value type
Variable updates to new type
This flow shows how a variable in Python can hold a value of one type, then be reassigned to a value of a different type, changing its type at runtime.
Execution Sample
Python
x = 10
print(type(x))
x = 'hello'
print(type(x))
Assign an integer to x, print its type, then assign a string to x and print its new type.
Execution Table
StepCode LineVariable x ValueType of xOutput
1x = 1010int
2print(type(x))10int<class 'int'>
3x = 'hello''hello'str
4print(type(x))'hello'str<class 'str'>
5End'hello'strExecution stops
💡 Code ends after printing the type of x as str.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
xundefined10 (int)'hello' (str)'hello' (str)
Key Moments - 2 Insights
Why does the type of x change after reassignment?
Because Python variables are labels for values, not fixed types. When x is assigned a new value, it points to that new value and its type changes accordingly, as shown in steps 1 and 3 of the execution_table.
Does the original integer value 10 stay linked to x after reassignment?
No, after step 3, x no longer refers to 10. It now refers to the string 'hello'. The integer 10 still exists in memory if used elsewhere, but x points to the new string.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the output?
A<class 'str'>
B<class 'int'>
C10
D'hello'
💡 Hint
Check the Output column at step 2 in the execution_table.
At which step does the variable x change its type from int to str?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the Type of x column in the execution_table between steps 2 and 3.
If we assign x = 3.14 after step 4, what would be the type of x?
Afloat
Bstr
Cint
Dbool
💡 Hint
Recall that 3.14 is a decimal number, which in Python is a float type.
Concept Snapshot
Python variables can hold any type of value.
Assigning a new value changes the variable's type.
Variable is a label pointing to a value.
Type is checked at runtime, not fixed.
Use type() to see current type.
Full Transcript
This example shows how a Python variable can change its type during program execution. Initially, x is assigned the integer 10, so its type is int. When we print type(x), it shows <class 'int'>. Later, x is assigned the string 'hello', changing its type to str. Printing type(x) then shows <class 'str'>. This happens because Python variables are just labels for values, and can point to any type of value at any time. The execution table traces each step, showing the value and type of x, and the output printed. This helps beginners see how variable types are dynamic in Python.

Practice

(1/5)
1. What happens to the type of a Python variable when you assign a new value of a different type to it?
easy
A. The variable's type changes to the new value's type automatically.
B. The variable keeps its original type regardless of the new value.
C. Python raises an error if the new value's type is different.
D. The variable becomes a list containing both old and new types.

Solution

  1. Step 1: Understand Python variable typing

    Python variables are dynamically typed, meaning their type depends on the current value assigned.
  2. Step 2: Assigning a new value changes the type

    When you assign a new value of a different type, the variable's type updates to match the new value automatically.
  3. Final Answer:

    The variable's type changes to the new value's type automatically. -> Option A
  4. Quick Check:

    Python variables are dynamic = A [OK]
Hint: Remember: Python variables follow the value's type, not fixed type. [OK]
Common Mistakes:
  • Thinking variables have fixed types like in some other languages.
  • Assuming Python throws an error on type change.
  • Believing variable types combine old and new types.
2. Which of the following is the correct way to assign a new value to a variable in Python so its type changes?
easy
A. x = 10
B. x := 10
C. int x = 10
D. var x = 10

Solution

  1. Step 1: Identify Python assignment syntax

    Python uses the single equals sign (=) to assign values to variables.
  2. Step 2: Check each option

    x = 10 uses correct syntax. x := 10 uses walrus operator which is for expressions, not simple assignment. int x = 10 and var x = 10 are invalid in Python.
  3. Final Answer:

    x = 10 -> Option A
  4. Quick Check:

    Assignment uses = in Python = A [OK]
Hint: Use single '=' for assignment in Python, not ':=' or types. [OK]
Common Mistakes:
  • Confusing ':=' walrus operator with assignment.
  • Using typed declarations like other languages.
  • Trying to declare variable types explicitly.
3. What will be the output of this code?
var = 5
print(type(var))
var = 'hello'
print(type(var))
medium
A. \n
B. \n
C. \n
D. Error

Solution

  1. Step 1: Check initial assignment and type

    var is first assigned 5, an integer, so type(var) is <class 'int'>.
  2. Step 2: Reassign and check new type

    var is then assigned 'hello', a string, so type(var) is <class 'str'>.
  3. Final Answer:

    <class 'int'>\n<class 'str'> -> Option C
  4. Quick Check:

    Type changes with value = C [OK]
Hint: Track variable type after each assignment line by line. [OK]
Common Mistakes:
  • Assuming type stays the same after reassignment.
  • Confusing output order of print statements.
  • Expecting a runtime error on type change.
4. Find the error in this code snippet:
value = 10
value = value + '5'
print(value)
medium
A. NameError because 'value' is undefined
B. SyntaxError due to invalid assignment
C. No error, output is 15
D. TypeError because int and str cannot be added

Solution

  1. Step 1: Analyze the operation between int and str

    value is an int (10), then tries to add a string ('5'), which is not allowed.
  2. Step 2: Identify the error type

    Python raises a TypeError when adding incompatible types like int and str.
  3. Final Answer:

    TypeError because int and str cannot be added -> Option D
  4. Quick Check:

    Adding int + str causes TypeError = D [OK]
Hint: Check types before adding; int + str causes error. [OK]
Common Mistakes:
  • Thinking Python auto-converts types in addition.
  • Expecting output '15' as string concatenation.
  • Confusing SyntaxError with TypeError.
5. Given the code below, what will be the final type of data?
data = 100
if isinstance(data, int):
    data = str(data)
else:
    data = [data]
hard
A. int
B. str
C. list
D. bool

Solution

  1. Step 1: Check initial type of data

    data starts as 100, which is an int.
  2. Step 2: Evaluate the if condition

    Since data is int, the if block runs, converting data to str('100').
  3. Final Answer:

    str -> Option B
  4. Quick Check:

    int converted to str by condition = B [OK]
Hint: If int, convert to str; else make list. [OK]
Common Mistakes:
  • Ignoring the if condition and assuming list.
  • Thinking data remains int after reassignment.
  • Confusing str and bool types.