Python 使用 “=” 做赋值操作符
变量名 = 值
变量名:必须是大小写英文,数字或_的任意组合,且不能以数字开头
另外python 内置关键字不能做为变量名
值可以是任意数据类型
例:
a
= 5
b =
'helloworld'
c =
[1,2,3,4,]
d = {'zhang':23,'li':18}
我们来执行一下看
print(type(a)) print(type(b)) print(type(c)) print(type(d))
显示
<class'int'>
<class'str'>
<class'list'>
<class'dict'>
从python的内部原理来了解变量的话,这个= 的操作实际上是一个引用的概念,这里延伸一下多重赋值的概念,通过id()à[id用来查看对象的内存地址]来证明这一点。
a = 5 b = a c = d = 6
print("a:",id(a)) print("b:",id(b)) print("c:",id(c)) print("d:",id(d))
输出结果如下
a:502387216
b:502387216
c:502387232
d:502387232
下面咱们再来通过一张图来说明这一点
多重变量实际上只是多了一个引用关系,并不会在内存中申请一块新的地址使用
多元赋值
a,b,c = 1,2,'hello world'
结果
print(a) print(b) print(c)
1
2
hello world