Python-day4-算术运算符与赋值运算符
算术运算符与赋值运算符
算术运算符
算术运算符
常规运算,输出结果
+ # 加
- # 减
* # 乘
/ # 除
% # 余数
** # 次方,取模(取余数)
// # 除以,只保留整数部分
print(10 + 3.1)
print(10 - 3.1)
print(10 * 3.1)
print(10 / 3.1)
print(10 % 3.1)
print(10 ** 3.1)
print(10 // 3.1)
案例
>>> print(10 + 3.1)
13.1
>>> print(10 - 3.1)
6.9
>>> print(10 * 3.1)
31.0
>>> print(10 / 3.1)
3.225806451612903
>>> print(10 % 3.1)
0.6999999999999997
>>> print(10 ** 3.1)
1258.9254117941675
>>> print(10 // 3.1)
3.0
比较运算符
>、<、>=、<=、==、
结果为真即为
True
,结果为假则为False
print(10 > 3)
print(10 < 3)
print(10 >= 3)
print(10 <= 3)
print(10 == 3)
案例
>>> print(10 > 3)
True
>>> print(10 < 3)
False
>>> print(10 >= 3)
True
>>> print(10 <= 3)
False
>>> print(10 == 3)
False
赋值运算符
赋值
将一个值赋值给一个变量名
age = 18
height = 180
print(age)
print(height)
案例
>>> age = 18
>>> height = 180
>>> print(age)
18
>>> print(height)
180
增量赋值
在变量名原有值的基础上再次赋值
# 定义初始变量值
age = 18
age += 1 # 等同于 age = age + 1
print(age)
age -= 1
print(age)
age *= 1
print(age)
age /= 1
print(age)
age %= 1
print(age)
age **= 1
print(age)
age //= 1
print(age)
案例
# 定义初始变量值
age = 18
>>> age += 1
>>> print(age)
19
>>> age -= 1
>>> print(age)
18
>>> age *= 1
>>> print(age)
18
>>>
>>> age /= 1
>>> print(age)
18.0
>>>
>>> age %= 1
>>> print(age)
0.0
>>>
>>> age **= 1
>>> print(age)
0.0
>>>
>>> age //= 1
>>> print(age)
0.0
链式赋值
变量名引用另一个变量名的值,即把一个值绑定给多个变量名
a = 10
b = a
z = b
x = a
print(a,b,z,x)
print(id(a),id(b),id(z),id(x))
# 或以更高效简洁的方式来传达定义
a = b = z = x = 100
print(a,b,z,x)
print(id(a),id(b),id(z),id(x))
案例
除了值相同,
id
也相同
>>> a = 10
>>> b = a
>>> z = b
>>> x = a
>>> print(a,b,z,x)
10 10 10 10
>>> print(id(a),id(b),id(z),id(x))
140546546997840 140546546997840 140546546997840 14054654699784
# 或以更高效简洁的方式来传达定义
>>> a = b = z = x = 100
>>> print(a,b,z,x)
100 100 100 100
>>> print(id(a),id(b),id(z),id(x))
140546547189200 140546547189200 140546547189200 140546547189200
交叉赋值
通过中间临时变量实现值的暂存
# 原定义
m = 10
n = 20
print(m,n)
# 交换值
temp = m # 注意位置不能调换,是m把值临时赋值给temp
m = n
n = temp # 注意位置不能调换,是temp把临时值赋值回给n
print(m,n)
>> 20 10
# 或以更高效简洁的方式来传达定义
# 或写为(m,n)=(n,m)带括号的形式也可以
m = 10
n = 20
m,n=n,m
print(m,n)
>> 20 10
案例
>>> # 原定义
>>> m = 10
>>> n = 20
>>> print(m,n)
10 20
>>>
>>> # 交换值
>>> temp = m# 注意位置不能调换,是m把值临时赋值给temp
>>> m = n
>>> n = temp# 注意位置不能调换,是temp把临时值赋值回给n
>>> print(m,n)
20 10
# 或以更高效简洁的方式来传达定义
>>> m,n=n,m
>>> print(m,n)
20 10
解压赋值
解压赋值主要用于取前后两头的值,几乎不会取中间
# 把五个月的工资取出来,分别赋值给不同的变量名
salaries = [111,222,333,444,555]
mon0 = salaries[0]
mon1 = salaries[1]
mon2 = salaries[2]
mon3 = salaries[3]
mon4 = salaries[4]
# 解压赋值
mon0,mon1,mon2,mon3,mon4 = salaries
print(mon0,mon1,mon2,mon3,mon4)
普通取值
# 把五个月的工资取出来,分别赋值给不同的变量名
salaries = [111,222,333,444,555]
mon0 = salaries[0]
mon1 = salaries[1]
mon2 = salaries[2]
mon3 = salaries[3]
mon4 = salaries[4]
mon0,mon1,mon2,mon3,mon4
>> (111, 222, 333, 444, 555)
# 解压赋值,左侧对应的变量名不可多或少,必须对应
mon0,mon1,mon2,mon3,mon4 = salaries
salaries
>> [111, 222, 333, 444, 555]
mon0,mon1,mon2,mon3,mon4
>> (111, 222, 333, 444, 555)
引入*
常用于取前、后的几个值,
*
是将其他值合并到一个临时变量名,比如我们用*_
来代表,不需要的值就交给下划线
x,y,z,*_ = salaries = [111,222,333,444,555]
print(x,y,z)
print(_)
# 取前3个
>>> x,y,z,*_ = salaries = [111,222,333,444,555]
>>> print(x,y,z)
111 222 333
>>> print(_)
[444, 555]
# 取后三个
>>> *_,x,y,z = salaries = [111,222,333,444,555]
>>> print(x,y,z)
333 444 555
>>> print(_)
[111, 222]
# 去中间这种方式几乎不用,有点反人类
# 取中间一个
>>> _,_,*a,_,_ = salaries = [111,222,333,444,555]
>>> print(a)
[333]
>>> print(_)
555
>>> # 取中间三个
>>> _,a,b,c,_ = salaries = [111,222,333,444,555]
>>> print(a,b,c)
222 333 444
>>> print(_)
555
# 附:字典解压取值(字典默认取的是key,并不是value)
>>> x,y,z = dic = {'a':123,'b':456,'c':789}
>>> print(x,y,z)
a b c