小码哥的IT人生

Python 运算符

Python基础 2022-06-06 12:52:44小码哥的IT人生shichen

Python 运算符

Python 运算符

运算符用于对变量和值执行操作。

Python 在以下组中划分运算符:

  • 算术运算符
  • 赋值运算符
  • 比较运算符
  • 逻辑运算符
  • 身份运算符
  • 成员运算符
  • 位运算符

Python 算术运算符

算术运算符与数值一起使用来执行常见的数学运算:

运算符 名称 实例 试一试
+ x + y 试一试
- x - y 试一试
* x * y 试一试
/ x / y 试一试
% 取模 x % y 试一试
** x ** y 试一试
// 地板除(取整除) x // y 试一试

Python 赋值运算符

赋值运算符用于为变量赋值:

运算符 实例 等同于 试一试
= x = 5 x = 5 试一试
+= x += 3 x = x + 3 试一试
-= x -= 3 x = x - 3 试一试
*= x *= 3 x = x * 3 试一试
/= x /= 3 x = x / 3 试一试
%= x %= 3 x = x % 3 试一试
//= x //= 3 x = x // 3 试一试
**= x **= 3 x = x ** 3 试一试
&= x &= 3 x = x & 3 试一试
|= x |= 3 x = x | 3 试一试
^= x ^= 3 x = x ^ 3 试一试
>>= x >>= 3 x = x >> 3 试一试
<<= x <<= 3 x = x << 3 试一试

Python 比较运算符

比较运算符用于比较两个值:

运算符 名称 实例 试一试
== 等于 x == y 试一试
!= 不等于 x != y 试一试
> 大于 x > y 试一试
< 小于 x < y 试一试
>= 大于或等于 x >= y 试一试
<= 小于或等于 x <= y 试一试

Python 逻辑运算符

逻辑运算符用于组合条件语句:

运算符 描述 实例 试一试
and 如果两个语句都为真,则返回 True。 x > 3 and x < 10 试一试
or 如果其中一个语句为真,则返回 True。 x > 3 or x < 4 试一试
not 反转结果,如果结果为 true,则返回 False not(x > 3 and x < 10) 试一试

Python 身份运算符

身份运算符用于比较对象,不是比较它们是否相等,但如果它们实际上是同一个对象,则具有相同的内存位置:

运算符 描述 实例 试一试
is 如果两个变量是同一个对象,则返回 true。 x is y 试一试
is not 如果两个变量不是同一个对象,则返回 true。 x is not y 试一试

Python 成员运算符

成员资格运算符用于测试序列是否在对象中出现:

运算符 描述 实例 试一试
in 如果对象中存在具有指定值的序列,则返回 True。 x in y 试一试
not in 如果对象中不存在具有指定值的序列,则返回 True。 x not in y 试一试

Python 位运算符

位运算符用于比较(二进制)数字:

运算符 描述 实例
& AND 如果两个位均为 1,则将每个位设为 1。
| OR 如果两位中的一位为 1,则将每个位设为 1。
^ XOR 如果两个位中只有一位为 1,则将每个位设为 1。
~ NOT 反转所有位。
<< Zero fill left shift 通过从右侧推入零来向左移动,推掉最左边的位。
>> Signed right shift 通过从左侧推入最左边的位的副本向右移动,推掉最右边的位。

完整实例1:

x = 5
y = 3
print(x + y)

完整实例2:

x = 5
y = 3
print(x - y)

完整实例3:

x = 5
y = 3
print(x * y)

完整实例4:

x = 15
y = 3
print(x / y)

完整实例5:

x = 5
y = 2
print(x % y)

完整实例6:

x = 2
y = 5
print(x ** y) #same as 2*2*2*2*2

完整实例7:

x = 15
y = 2
print(x // y)
#the floor division // rounds the result down to the nearest whole number

完整实例8:

x = 5
print(x)

完整实例9:

x = 5
x += 3
print(x)

完整实例10:

x = 5
x -= 3
print(x)

完整实例11:

x = 5
x *= 3
print(x)

完整实例12:

x = 5
x /= 3
print(x)

完整实例13:

x = 5
x%=3
print(x)

完整实例14:

x = 5
x//=3
print(x)

完整实例15:

x = 5
x **= 3
print(x)

完整实例16:

x = 5
x &= 3
print(x)

完整实例17:

x = 5
x |= 3
print(x)

完整实例18:

x = 5
x ^= 3
print(x)

完整实例19:

x = 5
x >>= 3
print(x)

完整实例20:

x = 5
x <<= 3
print(x)

完整实例21:

x = 5
y = 3
print(x == y)
# returns False because 5 is not equal to 3

完整实例22:

x = 5
y = 3
print(x != y)
# returns True because 5 is not equal to 3

完整实例23:

x = 5
y = 3
print(x > y)
# returns True because 5 is greater than 3

完整实例24:

x = 5
y = 3
print(x < y)
# returns False because 5 is not less than 3

完整实例25:

x = 5
y = 3
print(x >= y)
# returns True because five is greater, or equal, to 3

完整实例26:

x = 5
y = 3
print(x <= y)
# returns False because 5 is neither less than or equal to 3

完整实例27:

x = 5
print(x > 3 and x < 10)
# returns True because 5 is greater than 3 AND 5 is less than 10

完整实例28:

x = 5
print(x > 3 or x < 4)
# returns True because one of the conditions are true (5 is greater than 3, but 5 is not less than 4)

完整实例29:

x = 5
print(not(x > 3 and x < 10))
# returns False because not is used to reverse the result

完整实例30:

x = ["apple", "banana"]
y = ["apple", "banana"]
z = x
print(x is z)
# returns True because z is the same object as x
print(x is y)
# returns False because x is not the same object as y, even if they have the same content
print(x == y)
# to demonstrate the difference betweeen "is" and "==": this comparison returns True because x is equal to y

完整实例31:

x = ["apple", "banana"]
y = ["apple", "banana"]
z = x
print(x is not z)
# returns False because z is the same object as x
print(x is not y)
# returns True because x is not the same object as y, even if they have the same content
print(x != y)
# to demonstrate the difference betweeen "is not" and "!=": this comparison returns False because x is equal to y

完整实例32:

x = ["apple", "banana"]
print("banana" in x)
# returns True because a sequence with the value "banana" is in the list

完整实例33:

x = ["apple", "banana"]
print("pineapple" not in x)
# returns True because a sequence with the value "pineapple" is not in the list

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024