Can variables be reassigned to another data type?

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
29 Likes

Do we place a divider or How do we know that the value of the second variable will be taken?

1 Like

The assignment operator, = (it is not an “equals sign”), does the work. Each time the Python interpreter sees this symbol, three things happen:

  1. the expression on the right side of the symbol is evaluated to obtain a value.
  2. that value is placed in a memory location
  3. the address of that location is assigned to the variable on the left of the symbol.

Once this is accomplished, the value is bound to the variable.

22 Likes

so can we define a variable a specific type? like var should be an integer type throughout the program.
var = 10
And if any other type value is assigned to var it should throw an error stating type mismatch?
var = ‘sleep’.

Not in Python! Python types variables at the time of assignment. Some people call it “duck typing,” as in, “if it walks like a duck…” etc.

You can reassign any variable at any time without error.

1 Like

Declaration and definition are done at the same time?

Not explicitly. In Python, we do not declare variables. The assignment statement does everything: it tells the interpreter to evaluate the expression on the right, place its value in memory, and assign the memory address to the variable on the left.

The interpreter keeps track of the number of references to the address, and when that number is zero, the memory is released (actually, tagged for “garbage collection”). Any type of object can go in any address. Again, there is no explicit typing or variable declaration; it’s all “under the hood.”

9 Likes
  1. What is the life span of a variable in python?
  2. Is there any concept of variable that is not recently used is GC’d or something like that?

hi Patrickd , thank you for your answers

Yes, Python uses garbage collection. The life span is as long as it is referenced and then once it isn’t anymore however long it takes before the garbage collection gets it - as a side note, it is possible to run the GC manually.

One interesting life span is for default values of functions, they are evaluated once and not each time the function is called, so if you used something mutable you could have some weird side effects, like so:

def test(lst=[]):
    lst.append(0)
    print list

test()
#[0]
test()
#[0, 0]