Can variables be reassigned to another data type?


#1

Question

After a variable is assigned to an initial value, can the variable later be reassigned to another value that is of a different data type than its initial value?

Answer

Yes, variables in Python can be reassigned to a new value that is a different data type from its current value. In fact, variables can be reassigned to any valid value in Python, regardless of its current value.

Variables are essentially like an empty box, that can contain something like a string, number, or other value. When you assign it a value, the box will contain that value, and when you reassign it, it will empty out the old value, and the new value will be placed inside of it.

Example code

# This variable is initially assigned as a string.
var = "Hi there"

# It can be reassigned to any other value, regardless of the type.
var = 35
var = False