Sometimes, a variable can hold different types of values as the program runs. This helps make code flexible and easy to change.
How variable type changes at runtime in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
variable = value variable = another_value
You do not need to declare the type of a variable in Python.
The type of the variable changes automatically when you assign a new value of a different type.
Examples
x starts as a number, then changes to text.Python
x = 10 print(type(x)) x = "hello" print(type(x))
data first holds a decimal number, then a list of numbers.Python
data = 3.14 print(type(data)) data = [1, 2, 3] print(type(data))
Sample Program
This program shows how the variable value changes type from integer to string to list.
Python
value = 5 print(f"Value: {value}, Type: {type(value)}") value = "five" print(f"Value: {value}, Type: {type(value)}") value = [5] print(f"Value: {value}, Type: {type(value)}")
Important Notes
Python variables are like labels that can be moved from one box to another with different contents.
Changing variable types can be useful but be careful to avoid confusion in your code.
Summary
Python variables do not have fixed types.
The type changes automatically when you assign a new value.
This makes Python flexible and easy to use for beginners.
Practice
1. What happens to the type of a Python variable when you assign a new value of a different type to it?
easy
Solution
Step 1: Understand Python variable typing
Python variables are dynamically typed, meaning their type depends on the current value assigned.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.Final Answer:
The variable's type changes to the new value's type automatically. -> Option AQuick 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
Solution
Step 1: Identify Python assignment syntax
Python uses the single equals sign (=) to assign values to variables.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.Final Answer:
x = 10 -> Option AQuick 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
Solution
Step 1: Check initial assignment and type
var is first assigned 5, an integer, so type(var) is <class 'int'>.Step 2: Reassign and check new type
var is then assigned 'hello', a string, so type(var) is <class 'str'>.Final Answer:
<class 'int'>\n<class 'str'> -> Option CQuick 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
Solution
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.Step 2: Identify the error type
Python raises a TypeError when adding incompatible types like int and str.Final Answer:
TypeError because int and str cannot be added -> Option DQuick 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
Solution
Step 1: Check initial type of data
data starts as 100, which is an int.Step 2: Evaluate the if condition
Since data is int, the if block runs, converting data to str('100').Final Answer:
str -> Option BQuick 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.
