Python - operator - the fifth day

Python language supports the following types of operators

  • Arithmetic Operators
  • Comparison (relationship) operator
  • Assignment Operators
  • Logical Operators
  • Bitwise Operators
  • Member operator
  • Identity operator
  • Operator Precedence

Arithmetic Operators Python

The following assumptions variable a is 10, b is variable 21:

Operators description Examples
+ Plus - two objects are added a + b output 31
- Save - to give a number to another number or a negative number by subtracting a - b output -11
* By - multiplying two numbers or returns the string to be repeated several times a 210 outputs a * b
/ In addition - x divided by y b / a 2.1 output
% Modulo - returns the remainder of division b% a 1 output
** Power - Returns x to the power of y a ** b 10 to the power of 21
// Take divisible - close to the rounded down integer divisor
>>> 9//2
4 >>> -9//2 -5

The following examples demonstrate the operation of all the arithmetic operators Python:

Python comparison operators

The following assumptions variable a is 10, b is variable 20:

Operators description Examples
== Equal - compare objects for equality (A == b) returns False.
!= It is not equal to - compare whether two objects are not equal (A! = B) return True.
> Greater than - Returns whether x is greater than y (A> b) return False.
< Less than - Returns x is less than y. All comparison operators return 1 for true, 0 for false returns. True and False, respectively, which is equivalent to the special variables. Note the capitalization of these variable names. (A <b) return True.
>= Not less than - equal Returns whether x is greater than y. (A> = b) returns False.
<= Or less - Returns whether x is less than or equal y. (A <= b) return True.

The following example demonstrates the operation of all Python comparison operators:

Python assignment operator

The following assumptions variable a is 10, b is variable 20:

Operators description Examples
= Simple assignment operator c = a + b a + b is the assignment of the operation result c
+= Addition assignment operator c + = a is equivalent to c = c + a
-= Subtraction assignment operator c - = a is equivalent to c = c - a
*= Multiplication assignment operator equivalent to c * = a c = c * a
/= Division assignment operator c / = a is equivalent to c = c / a
%= Modulo assignment operator c% = a is equivalent to c = c% a
**= Power assignment operator c ** = a is equivalent to c = c ** a
//= Assignment operator take divisible c // = a is equivalent to c = c // a

The following examples demonstrate the operation of all the Python the assignment operator:

Bitwise Operators Python

The bitwise operators is considered in binary digital calculated. Python bitwise algorithm is as follows:

Operators description Examples
& Bitwise AND operator: two values ​​involved in operations, if the corresponding two bits are 1, then the 1-bit result is 0 otherwise (A & b) output 12, a binary interpretation: 00001100
| Or bitwise operators: two long as the corresponding binary bit is a 1, it is a result bit. (A | b) output 61, a binary interpretation: 00111101
^ Bitwise exclusive OR operator: When two different corresponding binary, the result is a (A ^ b) output 49, a binary interpretation: 00110001
~ Bitwise operators: for each binary data bit inverse, i.e., the 1 to 0, the 0 to 1. similarly ~ x -x-1 (~ A) -61 output, binary interpreter: 11000011, in the form of a complement to unsigned binary number.
<< Mobile operators left: each binary operand to the left a number of bits of all, the number of bits of a mobile "<<" specifies the right, discarding the upper, lower 0s. output a << 2 240, the binary interpretation: 11110000
>> Right Mobile Operators: The various binary operand is ">>" All right a number of bits left, ">>" the number of bits specified right a >> 2 输出结果 15 ,二进制解释: 0000 1111

以下实例演示了Python所有位运算符的操作:

Python逻辑运算符

Python语言支持逻辑运算符,以下假设变量 a 为 10, b为 20:

运算符 逻辑表达式 描述 实例
and x and y 布尔"与" - 如果 x 为 False,x and y 返回 False,否则它返回 y 的计算值。 (a and b) 返回 20。
or x or y 布尔"或" - 如果 x 是 True,它返回 x 的值,否则它返回 y 的计算值。 (a or b) 返回 10。
not not x 布尔"非" - 如果 x 为 True,返回 False 。如果 x 为 False,它返回 True。 not(a and b) 返回 False

以上实例输出结果:

Python成员运算符

除了以上的一些运算符之外,Python还支持成员运算符,测试实例中包含了一系列的成员,包括字符串,列表或元组。

运算符 描述 实例
in 如果在指定的序列中找到值返回 True,否则返回 False。 x 在 y 序列中 , 如果 x 在 y 序列中返回 True。
not in 如果在指定的序列中没有找到值返回 True,否则返回 False。 x 不在 y 序列中 , 如果 x 不在 y 序列中返回 True。

以下实例演示了Python所有成员运算符的操作:

Python身份运算符

身份运算符用于比较两个对象的存储单元

运算符 描述 实例
is is 是判断两个标识符是不是引用自一个对象 x is y, 类似 id(x) == id(y) , 如果引用的是同一个对象则返回 True,否则返回 False
is not is not 是判断两个标识符是不是引用自不同对象 x is not y , 类似 id(a) != id(b)。如果引用的不是同一个对象则返回结果 True,否则返回 False。

注: id() 函数用于获取对象内存地址。

以下实例演示了Python所有身份运算符的操作:

is 与 == 区别:

is 用于判断两个变量引用对象是否为同一个, == 用于判断引用变量的值是否相等。

 

Python运算符优先级

以下表格列出了从最高到最低优先级的所有运算符:

运算符 描述
** 指数 (最高优先级)
~ + - 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)
* / % // 乘,除,取模和取整除
+ - 加法减法
>> << 右移,左移运算符
& 位 'AND'
^ | 位运算符
<= < > >= 比较运算符
<> == != 等于运算符
= %= /= //= -= += *= **= 赋值运算符
is is not 身份运算符
in not in 成员运算符
not and or 逻辑运算符

 

 xMind小结:

 

参考资料:

https://www.runoob.com/python3/python3-data-type.html

 

Guess you like

Origin www.cnblogs.com/jeremywucnblog/p/11617084.html