python中的a=b=c是什么? [英] What is a=b=c in python?
问题描述
我很困惑,连续相等的=
可以在python中使用,例如:
a = b = c
这个语言功能叫什么?有什么我可以阅读的内容吗?
可以生成4个等号吗?
a = b = c = d
这只是将 a
和 b
声明为等于 c代码>.
所以你可以随心所欲地使用:
<预><代码>>>>我=7>>>a=b=c=d=e=f=g=h=i您可以在此 Python 教程的多重赋值中阅读更多内容.
<块引用>Python 允许您将单个值分配给多个变量同时.例如:
a = b = c = 1
这里创建了一个值为 1 的整数对象,并且所有三个变量被分配到相同的内存位置.你也可以将多个对象分配给多个变量.例如:
a, b, c = 1, 2, "约翰"
这里,两个值为 1 和 2 的整数对象被分配给变量 a 和 b,一个值为john"的字符串对象是分配给变量 c.
<小时>
还有一个花哨的东西!您可以像这样交换值:a,b=b,a
:
I am quite confused, consecutive equal =
can be used in python like:
a = b = c
What is this language feature called? Is there something I can read about that?
Can it be generated into 4 equals?
a = b = c = d
This is just a way to declare a
and b
as equal to c
.
>>> c=2
>>> a=b=c
>>> a
2
>>> b
2
>>> c
2
So you can use as much as you want:
>>> i=7
>>> a=b=c=d=e=f=g=h=i
You can read more in Multiple Assignment from this Python tutorial.
Python allows you to assign a single value to several variables simultaneously. For example:
a = b = c = 1
Here, an integer object is created with the value 1, and all three variables are assigned to the same memory location. You can also assign multiple objects to multiple variables. For example:
a, b, c = 1, 2, "john"
Here, two integer objects with values 1 and 2 are assigned to variables a and b, and one string object with the value "john" is assigned to the variable c.
There is also another fancy thing! You can swap values like this: a,b=b,a
:
>>> a=2
>>> b=5
>>> a,b=b,a
>>> a
5
>>> b
2
这篇关于python中的a=b=c是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!