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

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

8 Likes

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.

54 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’.

2 Likes

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.

10 Likes

Declaration and definition are done at the same time?

2 Likes

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.”

18 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

1 Like

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]
2 Likes

Can you please clarify the concept of duck typing & duck test?

Jephos249 this was really helpful. Thank you.

A post was split to a new topic: What is that supposed to mean?

How can we ensure that we don’t accidentally perform operations on a variable after reassigning it to another data type? Are there any best practices for handling reassignment of variables in Python?

1 Like

my question is -
for example in the lesson there is meal = “english muffin”
but later on it used again and provides next data with different wards
so do you have to keep repeating it in that case if u gotta use it multiple times or u can say meal1 and for next one meal2 and meal 3? or it doesn’t matter because it knows the pattern/can tell what goes first what second?
Is it better to lable them diffentet like “meal1” and so on , right?

Basic understanding would say that is only necessary when you want to keep the original value as well as a new one. So for instance, lets say you wanted to use multiple variables within a single sentence. You would then define each variable with a new command.
Here is an article that will better explain it: Why change a variable rather than just create a new one? - Get Help / Python - Codecademy Forums

1 Like

@swapnilb007 it’s colloquial, it means that Python looks at the value and determines the type of the variable based on what it notices as attributes - like the saying ‘If it walks like a duck and it quacks like a duck, then it must be a duck’.

I.e.: If the variable is ‘XXX’. Python looks at it and goes, well it has quotes around it, by all conventions it should be a string.
Another example, if the variable is 42. Python looks at it and determines that without any other information based on the fact it’s numeric in absence of anything else it is probably numeric.

There’s a wikipedia artiicle that explicitly explains this in the context of Python 3:

P.S: Duck test is also in there in the wikipedia article.

@ajax3916882621 Python doesn’t declare variable types nor track variable types as other have said. The onus is really on you the programmer to make sure you keep track of the variables you created and how you use them. If you are reassigning variables often, it would be good to have a great name for it so that it’s not confusing when you reassign them in a program.

It’s not the biggest deal in the world. The worst that happens is that you run into an error and you go back and fix the variable.