If you're coming to Python from a language in the C/Java/etc. family, it may help you to stop thinking about a
as a "variable", and start thinking of it as a "name".
a
, b
, and c
aren't different variables with equal values; they're different names for the same identical value. Variables have types, identities, addresses, and all kinds of stuff like that.
Names don't have any of that. Values do, of course, and you can have lots of names for the same value.
If you give Notorious B.I.G.
a hot dog,* Biggie Smalls
and Chris Wallace
have a hot dog. If you change the first element of a
to 1, the first elements of b
and c
are 1.
If you want to know if two names are naming the same object, use the is
operator:
>>> a=b=c=[0,3,5]
>>> a is b
True
You then ask:
what is different from this?
d=e=f=3
e=4
print['f:',f]
print['e:',e]
Here, you're rebinding the name e
to the value 4
. That doesn't affect the
names d
and f
in any way.
In your previous version, you were assigning to a[0]
, not to a
. So, from the point of view of a[0]
, you're rebinding a[0]
, but from the point of view of a
, you're changing it in-place.
You can use the id
function, which gives you some unique number representing the identity of an object, to see exactly which object is which even when is
can't help:
>>> a=b=c=[0,3,5]
>>> id[a]
>>> id[b]
>>> id[a[0]]
4297261120
>>> id[b[0]]
4297261120
>>> a[0] = 1
>>> id[a]
>>> id[b]
>>> id[a[0]]
4297261216
>>> id[b[0]]
4297261216
Notice that a[0]
has changed from
4297261120 to 4297261216—it's now a name for a different value. And b[0]
is also now a name for that same new value. That's because a
and b
are still naming the same object.
Under the covers, a[0]=1
is actually calling a method on the list object. [It's equivalent to a.__setitem__[0, 1]
.] So, it's not really rebinding anything at all. It's like calling my_object.set_something[1]
. Sure, likely the object is rebinding an instance attribute in order to implement this method, but that's
not what's important; what's important is that you're not assigning anything, you're just mutating the object. And it's the same with a[0]=1
.
user570826 asked:
What if we have,
a = b = c = 10
That's exactly the same situation as a = b = c = [1, 2, 3]
: you have three names for the same value.
But in this case, the value is an int
, and int
s are immutable. In either case, you can rebind a
to a different value [e.g., a = "Now I'm a string!"
], but the won't
affect the original value, which b
and c
will still be names for. The difference is that with a list, you can change the value [1, 2, 3]
into [1, 2, 3, 4]
by doing, e.g., a.append[4]
; since that's actually changing the value that b
and c
are names for, b
will now b [1, 2, 3, 4]
. There's no way to change the value 10
into anything else. 10
is 10 forever, just like Claudia the vampire is 5 forever [at least until she's replaced by Kirsten Dunst].
* Warning: Do not give Notorious B.I.G. a hot dog. Gangsta rap zombies should never be fed after midnight.
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 1999-2022 by Refsnes Data. All Rights Reserved.
W3Schools is Powered by W3.CSS.
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
A variable is a segment of memory with a unique name used to hold data that will later be processed. Although each programming language has a different mechanism for declaring variables, the name and the data that will be assigned to each variable are always the same. They are capable of storing values of data types.
The assignment operator[=] assigns the value provided to its right to the variable name given to its left. Given is the basic syntax of variable declaration:
Syntax: var_name = value
Example:
a = 4
Assign Values to Multiple Variables in One Line
Given above is the mechanism for assigning just variables in Python but it is possible to assign multiple variables at the same time. Python assigns values from right to left. When assigning multiple variables in a single line, different variable names are provided to the left of the assignment operator separated by a comma. The same goes for their respective values except they should be to the right of the assignment operator.
While declaring variables in this fashion one must be careful with the order of the names and their corresponding value first variable name to the left of the assignment operator is assigned with the first value to its right and so on.
Example 1:
Variable assignment in a single line can also be done for different data types.
Python3
a, b
=
4
,
8
print
[
"value assigned to a"
]
print
[a]
print
[
"value assigned to b"
]
print
[b]
Output:
value assigned to a 4 value assigned to b 8
Example 2:
Not just simple variable assignment, assignment after performing some operation can also be done in the same way.
Python3
print
[
"assigning values of different datatypes"
]
a, b, c, d
=
4
,
"geeks"
,
3.14
,
True
print
[a]
print
[b]
print
[c]
print
[d]
Output:
assigning values of different datatypes 4 geeks 3.14 True
Example 3:
Assigning different operation results to multiple variable.
Python3
a, b
=
8
,
3
add, pro
=
[a
+
b], [a
*
b]
print
[add]
print
[pro]
Output:
11 24
Example 4:
Here, we are storing different characters in a different variables.
Python3
string
=
"Geeks"
a, b, c
=
string[
0
], string[
1
:
4
], string[
4
]
print
[a]
print
[b]
print
[c]
Output:
G eek s