+ 2
Python: difference assignment a = " " and a = [ ]
Can anyone tell me the difference between assigning quotes or square brackets to a variable? Example: a = "" a = [] Thank you
8 Respuestas
+ 7
a = "" is assigning an empty string literal to variable a
a = [] is assigning an empty list
+ 6
Let's say you want to add words to a string to create a longer string. You can start with an empty String a = ""... It is a bit like when we initialize an integer to 0 and then start adding stuff to it like in the case of using a loop to get the sum of the elements of an array... That is just one example of how an empty String can be helpful
+ 4
This is in Java. Would it be easier to understand the same example in another language? Let me know if it helps ☺
https://code.sololearn.com/cluTfBpN34SR/?ref=app
+ 2
a="" means that a string is assigned to a. And a=[] means to assign an array. This is a kind of hint given to interpreter about the data kind to be stored in the variable.
Extra tips:
a = 0 #assign a numerical value
a = false #assign a boolean value
+ 1
but in the case of a = "", how is it used in practice?
+ 1
a simple program with which I can understand?
+ 1
a = "" = a = str()
You can use this e.g. to declare a variable in order to avoid the classic "reference variable before assignment" error.
+ 1
Antonio Bernardini you can use it for append some chars using for loops for sometimes or anything related to string modification