变量命名规则:
变量名只能是字母、数字、下划线;
首字符不能是数字;
系统关键字(保留关键字)不能用在变量名中,eg,and,if,import;
变量名区分大小写;
变量没有类型限制(java,c#有变量类型)——python动态语言;
>>> A = [1,2,3,4,5,6]
>>> print(A)
[1, 2, 3, 4, 5, 6]
1.值类型:
不可变(int、str、tuple),内存地址不变,+运算过程中其实是指向了一个新的数据,并没有改变原有数据。
2.引用类型:
可以改变(list、set、dict),使用id()函数可以查看变量的内存地址是否有变化。
注意:列表可变,字符串、元组不可改变。
序列赋值出现变化!
>>> a = 1;
>>> b = a;
>>> a = 3;
>>> print(b)
1
>>> a = [1,2,3,4,5]
>>> b = a
>>> a[0] = '1'
>>> print(a)
['1', 2, 3, 4, 5]
>>> print(b)
['1', 2, 3, 4, 5]
字符串不可变
>>> 'python'[0]
'p'
>>> 'python'[0] = 'o'
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
'python'[0] = 'o'
TypeError: 'str' object does not support item assignment
>>>
列表可变
>>> a = [1,2,3,4,5,6]
>>> a[0] = 5
>>> print(a)
[5, 2, 3, 4, 5, 6]
元组不可变
>>> a = (1,2,3,4,5,6)
>>> a[0] = 9
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
a[0] = 9
TypeError: 'tuple' object does not support item assignment
3.多维数组的调用:
>>> a = [1,2,3,[1,2,4]]
>>> a[2]
3
>>> a[3]
[1, 2, 4]
>>> a[3][2]
4