濮阳网站建设知名公司排名,城乡与建设部网站,房产网站制作,4.1进行网站建设与推广算术运算符python支持的算数运算符与数学上计算的符号使用是一致的(x 5 , y 2)#xff1a;算术运算符描述示例两个对象相加x y 7-两个对象相减x - y 3*两个对象相乘x * y 10/除#xff0c;返回值保留整数和小数部分x / y 2.5//整除#xff0c;只保留整数部分x // y …算术运算符python支持的算数运算符与数学上计算的符号使用是一致的(x 5 , y 2)算术运算符描述示例两个对象相加x y 7-两个对象相减x - y 3*两个对象相乘x * y 10/除返回值保留整数和小数部分x / y 2.5//整除只保留整数部分x // y 2%取余数x % y 1**一个数的n次方x ** y 25x 5y 2print(fx y {x y})print(fx - y {x - y})print(fx * y {x * y})print(fx / y {x / y})print(fx // y {x // y})print(fx % y {x % y})print(fx ** y {x ** y})# x y 7# x - y 3# x * y 10# x / y 2.5# x // y 2# x % y 1# x ** y 25比较运算符比较运算用来对两个值进行比较返回的是布尔值(x 5y 2)比较运算符描述示例相等x y // False!不相等x !y // True大于x y // True大于等于x y // True小于x y // False小于等于x y // Falsex 5y 2print(fx y {x y})print(fx ! y {x ! y})print(fx y {x y})print(fx y {x y})print(fx y {x y})print(fx y {x y})# x y False# x ! y True# x y True# x y True# x y False# x y False赋值运算符python语法中除了有号这种简单的赋值运算外还支持增量赋值、链式赋值、交叉赋值、解压赋值这些赋值运算符存在的意义都是为了让我们的代码看起来更加精简。增量赋值赋值运算符描述示例加法赋值运算符x 1 相当于 x x 1-减法赋值运算符x - 1 相当于 x x - 1*乘法赋值运算符x * 1 相当于 x x * 1/除法赋值运算符x / 1 相当于 x x / 1//整除赋值运算符x // 1 相当于 x x // 1%取余赋值运算符x % 1 相当于 x x % 1**幂赋值运算符x ** 1 相当于 x x ** 1x 5x 2print(fx 2 {x}) # 7x 5x - 2print(fx - 2 {x}) # 3x 5x * 2print(fx * 2 {x}) # 10x 5x / 2print(fx / 2 {x}) # 2.5x 5x // 2print(fx // y {x}) # 2x 5x % 2print(fx % y {x}) # 1x 5x ** 2print(fx ** y {x}) # 25链式赋值如果我们想把同一个值同时赋值给多个变量名可以这么做z 10y zx yprint(x, y, z) # 10 10 10链式赋值指的是可以用一行代码搞定这件事x y z 10print(x, y, z) # 10 10 10交叉赋值如果我们想交换两个变量的值可以这么做x 10y 20print(fx {x}, y {y})# x 10, y 20temp xx yy tempprint(fx {x}, y {y}) # x 20, y 10交叉赋值指的是一行代码可以搞定这件事x 10y 20print(fx {x}, y {y}) # x 10, y 20x, y y, xprint(fx {x}, y {y}) # x 20, y 10解压赋值如果我们想把列表中的多个值取出来依次赋值给多个变量名可以这么做numbers [1, 2, 3]a numbers[0]b numbers[1]c numbers[2]print(a, b, c) # 1 2 3解压赋值指的是一行代码可以搞定这件事numbers [1, 2, 3]a, b, c numbersprint(a, b, c) # 1 2 3解压赋值时等号左边的变量名个数必须与右面包含值的个数相同否则会报错numbers [1, 2, 3]# 变量个数少了a, b numbers# Traceback (most recent call last):# File /Users/fqyy/Downloads/Code/Learn/Python/demo.py, line 3, in # a, b numbers# ValueError: too many values to unpack (expected 2)# 变量个数多了a, b, c, d numbers# Traceback (most recent call last):# File /Users/fqyy/Downloads/Code/Learn/Python/demo.py, line 11, in # a, b, c, d numbers# ValueError: not enough values to unpack (expected 4, got 3)如果我们只想取头尾的几个值可以用 *变量名 进行匹配此时Python会将没有对应关系的值存成列表然后赋值给紧跟其后的变量名numbers [1, 2, 3, 4, 5, 6]# 取前面两个a, b, *_ numbers print(a, b) # 1 2# 取后面两个*_, a, b numbers print(a, b) # 5 6注意 这里的 _ 是变量名一般自己定义变量的时候不要使用下划线( _ )开头和结尾或者直接使用下划线( _ )从Python的规范上来说它们都有特殊的用处所有自己定义的变量不要去用。比如这里直接使用下划线( _ )接受数据表示该变量一般不会使用仅仅做为占位符。除了列表外字符串、字典、元组、集合类型都支持解压赋值# 列表l [1, 2, 3]a, b, c lprint(a, b, c) # 1 2 3# 元祖t (4, 5, 6)a, b, c tprint(a, b, c) # 4 5 6# 集合s {7, 8, 9}a, b, c sprint(a, b, c) # 8 9 7# 字符串a, b, c abcprint(a, b, c) # a b c# 字典 获取的是key值a, b, c {name: Tom, age: 18, gender: 男}print(a, b, c) # name age gender逻辑运算符逻辑运算符描述and逻辑与只有当两个对象都是True的时候返回True 否则返回Falseor逻辑或只有当两个对象都是False的时候返回False否则返回Truenot取反逻辑运算符用于连接多个条件进行关联判断会返回布尔值# 逻辑与print(1 and 2) # 2print(1 and 0) # 0print(0 and 0) # 0# 逻辑或print(1 or 2) # 1print(1 or 0) # 1print(0 or 0) # 0# 逻辑非print(not 1) # Falseprint(not 0) # True可以用and连接多个条件会按照从左到右的顺序依次判断一旦某一个条件为False则无需再往右判断可以立即判定最终结果就为False只有在所有条件的结果都为True的情况下最终结果才为True# 多个and# 有一个是False就为Falseprint(1 and 0 and 2) # 0# 全部是True的时候才是Trueprint(1 and 2 and 3) # 3同样也可以用or连接多个条件此时会按照从左到右的顺序依次判断一旦某一个条件为True则无需再往右判断可以立即判定最终结果就为True只有在所有条件的结果都为False的情况下最终结果才为False# 多个or# 有一个是True就是Trueprint(1 or 0 or 2) # 1# 全部是False的时候才是Falseprint(0 or 0 or 0) # 0注意 三者的优先级关系 not and or 同一优先级默认从左往右计算。最好使用括号来区别优先级其实意义与上面的一样。逻辑运算的结果一旦可以确定那么就以当前处计算到的值作为最终结果返回这种现象叫做 短路运算# 因为or只要有一个为True就是True因此当获取到1后不会在执行后面的print(1 or 0 or 2) # 1# 因为or只要有一个为False就是False因此当获取到0后不会在执行后面的print(0 and 1 and 2) # 0成员运算符成员运算符描述in某个对象是否包含另一个对象字符串、列表、元祖、集合和字典都支持not in某个对象是否不包含另一个对象# 字符串print(a in Hello) # Falseprint(a not in Hello) # Ture# 字典 默认判断是的keyprint(a in {name: age}) # Falseprint(a not in {name: age}) # True# 集合print(a in {a, b}) # Trueprint(a not in {a, b}) # False# 列表print(a in [a, b]) # Trueprint(a not in [a, b]) # False# 元祖print(a in (a, b)) # Trueprint(a not in (a, b)) # False注意 虽然两种判断可以达到相同的效果但推荐使用not in因为它的语义更加明确。身份运算符身份运算符描述is某个对象是否包含另一个对象字符串、列表、元祖、集合和字典都支持is not某个对象是否不包含另一个对象x Hello Worldy Hello Worldz xprint(x is y) # Falseprint(x is not y) # Trueprint(x is z) # Trueprint(x is not z) # False