三、Python的变量与运算符

3 变量与运算符

3.1 变量

a.变量的命名规则
变量用字母、下划线及数字组成(不能以数字开头),且变量名不能是Python系统关键字。

1
2
3
4
5
6
7
8
9
>>> _aA = 2
>>> print(_aA)
2
>>> a = '24r'
>>> print(a)
24r
>>> 1w = 2
SyntaxError: invalid syntax
>>>

Python中变量是动态的,不需要先声明类型,可直接使用。

1
2
3
4
5
6
7
8
9
10
11
12
>>> a = 1
>>> b = a
>>> a =3
>>> print(b) # int、str、tuple为值类型,无法改变
1
>>>
>>> q = [1,2,3]
>>> w = q
>>> q[0] = 'a'
>>> print(w) # list、set、dict为引用类型,是可改变的
['a', 2, 3]
>>>

int、str、tuple为值类型,无法改变;
list、set、dict为引用类型,是可改变的,代码说明如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
>>> a = (1,2,'a')
>>> a[0]
1
>>> a[0] = 3
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
a[0] = 3
TypeError: 'tuple' object does not support item assignment
>>> s = 'wer3'
>>> id(s)
52198400
>>> s = s + 't'
>>> print(s)
wer3t
>>> id(s)
52199264
>>> l = [1,2,6]
>>> id(l)
52357760
>>> l[1] = 'k'
>>> print(l)
[1, 'k', 6]
>>> id(l)
52357760
>>>

list(列表)的可变与tuple(元组)的不可变

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
>>> a = [1,'we',(1,2,3)]
>>> a.append(['qw',[2,4,8]])
>>> print(a)
[1, 'we', (1, 2, 3), ['qw', [2, 4, 8]]]
>>> b = (1,8,4,'k')
>>> b.append(l) # 元组不可变 无法进行添加操作
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
b.append(l)
AttributeError: 'tuple' object has no attribute 'append'
>>>
>>> c = (1,3,[3,'tty',9])
>>> c[2].append(66) # 本质还是在对列表进行操作
>>> print(c)
(1, 3, [3, 'tty', 9, 66])
>>>
>>> c = (1,3,[3,'tty',9]) # 访问某个元素
>>> c[2][0]
3
>>> c[2][1] = 000
>>> print(c)
(1, 3, [3, 0, 9])
>>>

3.2 运算符(记清楚大类,用的时候查询,用多了就记着了):

1.算术运算符:+、-、、/、//(整除)、**(次方)、%(求余)
2.赋值运算符:=、+=、-=、
=、/=、%=、**=、//=
3.关系运算符: ==、!=、>、<、>=、<=
4.逻辑运算符:and、or、not
5.成员运算符:in、not in
6.身份运算符: is、not is
7.位运算符:&、|、<<、>>、^、~

3.2.1 算术运算符+、-、*、/、//(整除)、**(次方)、%(求余)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
>>> 'wk9'+ ' opp'
'wk9 opp'
>>> 3 - 4
-1
>>> [1,2,'fg'] * 2
[1, 2, 'fg', 1, 2, 'fg']
>>> 8 / 3
2.6666666666666665
>>> 8 // 3
2
>>> 2 ** 6
64
>>> 6 % 2
0
>>> 7 % 2
1
>>>
3.2.2 赋值运算符: =、+=、-=、*=、/=、%=、**=、//=
1
2
3
4
5
6
7
8
9
>>> a = 3
>>> b = 6
>>> a += 1
>>> print(a)
4
>>> b -= a
>>> print(b)
2
>>>
3.2.3 比较(关系)运算符:==、!=、>、<、>=、<=

操作结果将返回一个bool类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
>>> a = 1
>>> b = 2
>>> a != b
True
>>> a == b
False
>>> 运算优先级的问题,不必死记,用()即可
>>> c = 1
>>> c += c >= 1
>>> print(c)
2
>>>
>>> d = 1
>>> d += d >= 2
>>> print(d)
1
>>>
>>> 's' < 'l'
False
>>> ord('s')
115
>>> ord('l')
108
>>> 'abf' < 'abe'
False
>>> # 列表、元组、集合之间的比较规律:
>>> [1,2,4] < [1,3,7]
True
>>> (2,4,6) < (3,1,1)
True
>>> {1,2,3} < {2,1,0}
False
>>> {1,2,3} < {2,1,4}
False
>>> {1,2,3} == {2,1,3}
True
>>> # dict 之间无法比较
>>> {1:'ak',2:'ty'} < {2:'bl',1:'yt'}
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
{1:'ak',2:'ty'} < {2:'bl',1:'yt'}
TypeError: '<' not supported between instances of 'dict' and 'dict'
>>>

3.2.4 逻辑运算符

and(且,两个为真,才返回为真),or(或,有一个为真,返回就为真),not(非);
其操作对象是bool类型,返回结果也是bool类型。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 # True基本认识
>>> True and False
False
>>> True and True
True
>>> 1 and 1
1
>>> 1 and 0
0
>>> 2 and -2
-2
>>> -2 and 2
2
>>> '' and 2
''
>>>
# or 基本认识
>>> False or True
True
>>> False or False
False
>>> 1 or 9
1
>>> 9 or 1
9
>>> 0 or 3
3
>>>
# not 基本认识
>>> not True
False
>>> not not True
True
>>> not 1
False
>>> not 0
True
>>>
# strlist tuple set dict的空
# 以及int float 的 0 都被认为是 False
>> not ''
True
>>> not []
True
>>> not ()
True
>>> not {}
True
>>> not set()
True
>>> not 0
True
>>>

3.2.5 成员运算符(in,not in)

判断一个元素是否在一组元素中,返回结果为bool.

1
2
3
4
5
6
7
8
9
10
>>> a = 12
>>> a in [1,2,4,12]
True
>>> a not in (1,5,12)
False
>>> a in {'a':12,9:5}
False
>>> a in {12:1,5:2} # 字典里的成员运算是依据key进行判断
True
>>>

3.2.6 身份运算符(is,is not)

is 比较的是两个身份是否相等(id,内存地址),而不是值是否相等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
>>> a = 1
>>> b = 2
>>> a is b
False
>>> b = 1
>>> a is b
True
>>> id(a)
1730401328
>>> id(b)
1730401328
>>> c = 1.0
>>> a == c
True
>>> a is c
False
>>> a is not c
True
>>>
>>> a = {1,2,3}
>>> b = {2,3,1}
>>> a == b # 因为集合set是无序的
True
>>> a is b # 身份运算不等(id肯定不一样)
False
>>> c = (1,2,3)
>>> d = (2,3,1)
>>> c == d # tuple元组是有序的
False
>>> c is d
False
>>>

小结:
对象三特征: 值(value)、身份(id)、类型(type)
相应的判断方法分别是: ==,is,isinstance(不推荐使用type进行判断)

1
2
3
4
5
6
7
8
9
>>> a = 'ss'
>>> type(a) == str
True
>>> isinstance(a,int) # 判断某个变量是不是某个类型
False
>>> c = (1,3,'3')
>>> isinstance(c,(int,str,tuple)) # 判断某个变量是不是某几个类型
True
>>>

3.2.7 位运算符

按位与(&)、按位或(|)、按位异或(^)、按位取反(~)、左移动(<<)、右移动(>>)
都是把元素或者变量当作二进制进行运算

1
2
3
4
>>> a = 2 # 二进制 10
>>> b = 3 # 二进制 11
>>> a & b # 10 & 11 按位与 规则是:与运算
2

分享到