python 变量

来源:互联网 发布:radio flyer淘宝 编辑:程序博客网 时间:2024/06/01 10:24

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 = 5b = ac = d = 6

 

print("a:",id(a))print("b:",id(b))print("c:",id(c))print("d:",id(d))

输出结果如下

a:502387216b:502387216c:502387232d:502387232


下面咱们再来通过一张图来说明这一点


多重变量实际上只是多了一个引用关系,并不会在内存中申请一块新的地址使用

 

多元赋值

a,b,c = 1,2,'hello world'

 

结果

print(a)print(b)print(c)

 

 

12hello world

1 0