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
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 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.â
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]
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?
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
@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.