0
0
Pythonprogramming~10 mins

How variable type changes at runtime in Python - Try It Yourself

Choose your learning style9 modes available
How variable type changes at runtime
📖 Scenario: Imagine you are working with a variable that can hold different types of information during a program. For example, a box that can hold a number first, then a word, and then a list. This shows how Python allows the type of a variable to change as the program runs.
🎯 Goal: You will create a variable and change its type step-by-step, then print its value and type each time to see how it changes.
📋 What You'll Learn
Create a variable called data and assign it the integer value 10
Change the variable data to hold the string value 'hello'
Change the variable data to hold the list [1, 2, 3]
Print the value and type of data after each change
💡 Why This Matters
🌍 Real World
In real programs, variables often change what they hold depending on the situation, like storing a number first and then a message.
💼 Career
Understanding how variables can change types helps in debugging and writing flexible code in many programming jobs.
Progress0 / 4 steps
1
Create a variable with an integer value
Create a variable called data and assign it the integer value 10.
Python
Need a hint?

Use the equals sign = to assign the value 10 to the variable data.

2
Change the variable to a string
Change the variable data to hold the string value 'hello'.
Python
Need a hint?

Assign the string 'hello' to the variable data using =.

3
Change the variable to a list
Change the variable data to hold the list [1, 2, 3].
Python
Need a hint?

Assign the list [1, 2, 3] to the variable data using =.

4
Print the value and type of the variable after each change
Print the value and type of the variable data after each change using print(data, type(data)).
Python
Need a hint?

Use print(data, type(data)) after each assignment to show the value and its type.