Python operators

高洛峰
Release: 2016-11-23 11:19:49
Original
1247 people have browsed it

What is an operator?

This chapter mainly explains Python’s operators. Let’s take a simple example: 4 +5 = 9. In the example, 4 and 5 are called operands, and the "+" sign is the operator.

The Python language supports the following types of operators:

Arithmetic operators

Comparison (relational) operators

Assignment operators

Logical operators

Bitwise operators

Member operators

Identity operators

Operator precedence

Next, let’s learn Python’s operators one by one.

Python Arithmetic Operators

The following assumes that variable a is 10 and variable b is 20:

Operator

Description

Example

+ Add - Add two objects a + b Output result 30

- Subtraction - Get a negative number or one number minus another number a - b Output result -10

* Multiply - Multiply two numbers or return a string repeated several times a * b Output result 200

/ Division-

// Divide - Return the integer part of the quotient 9//2 Output result 4, 9.0//2.0 Output result 4.0

The following example demonstrates the operation of all arithmetic operators in Python:

#!/usr/bin/python
 
a = 21
b = 10
c = 0
 
c = a + b
print "Line 1 - Value of c is ", c
 
c = a - b
print "Line 2 - Value of c is ", c
 
c = a * b
print "Line 3 - Value of c is ", c
 
c = a / b
print "Line 4 - Value of c is ", c
 
c = a % b
print "Line 5 - Value of c is ", c
 
a = 2
b = 3
c = a**b
print "Line 6 - Value of c is ", c
 
a = 10
b = 5
c = a//b
print "Line 7 - Value of c is ", c
Copy after login

The above example Output result:

Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1
Line 6 - Value of c is 8
Line 7 - Value of c is 2
Copy after login

Python comparison operator

The following assumes that variable a is 10 and variable b is 20:

Operator

Description

Instance

== equal to - Whether the comparison objects are equal (a == b) returns False.

!= Not equal - Compares whether two objects are not equal (a != b) Returns true.

<> Not equal - Compares whether two objects are not equal (a <> b) Returns true. This operator is similar to != .

> Greater than - Returns whether x is greater than y (a > b) Returns False.

< Less than - Returns whether x is less than y. All comparison operators return 1 for true and 0 for false. These are equivalent to the special variables True and False respectively. Note the capitalization of these variable names. (a < b) returns true.

>= Greater than or equal to - Returns whether x is greater than or equal to y. (a >= b) returns False.

<= Less than or equal to - Returns whether x is less than or equal to y. (a <= b) returns true.

The following examples demonstrate the operation of all comparison operators in Python:

#!/usr/bin/python
 
a = 21
b = 10
c = 0
 
if ( a == b ):
   print "Line 1 - a is equal to b"
else:
   print "Line 1 - a is not equal to b"
 
if ( a != b ):
   print "Line 2 - a is not equal to b"
else:
   print "Line 2 - a is equal to b"
 
if ( a <> b ):
   print "Line 3 - a is not equal to b"
else:
   print "Line 3 - a is equal to b"
 
if ( a < b ):
   print "Line 4 - a is less than b"
else:
   print "Line 4 - a is not less than b"
 
if ( a > b ):
   print "Line 5 - a is greater than b"
else:
   print "Line 5 - a is not greater than b"
 
a = 5;
b = 20;
if ( a <= b ):
   print "Line 6 - a is either less than or equal to  b"
else:
   print "Line 6 - a is neither less than nor equal to  b"
 
if ( b >= a ):
   print "Line 7 - b is either greater than  or equal to b"
else:
   print "Line 7 - b is neither greater than  nor equal to b"
   
以上实例输出结果:
Line 1 - a is not equal to b
Line 2 - a is not equal to b
Line 3 - a is not equal to b
Line 4 - a is not less than b
Line 5 - a is greater than b
Line 6 - a is either less than or equal to b
Line 7 - b is either greater than or equal to b
Copy after login

Python assignment operator

The following assumes that variable a is 10 and variable b is 20:

Operator

Description

Example

= Simple assignment operator c = a + b Assign the result of a + b to 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 c *= a is equivalent to c = c * a

/= Division assignment operator c /= a Equivalent to c = c / a

%= Modulo assignment operator c %= a Equivalent to c = c % a

**= Power assignment operator c **= a Equivalent to c = c * * a

//= Integer division assignment operator c //= a is equivalent to c = c // a

The following examples demonstrate the operation of all assignment operators in Python:

#!/usr/bin/python
 
a = 21
b = 10
c = 0
 
c = a + b
print "Line 1 - Value of c is ", c
 
c += a
print "Line 2 - Value of c is ", c
 
c *= a
print "Line 3 - Value of c is ", c
 
c /= a
print "Line 4 - Value of c is ", c
 
c  = 2
c %= a
print "Line 5 - Value of c is ", c
 
c **= a
print "Line 6 - Value of c is ", c
 
c //= a
print "Line 7 - Value of c is ", c
   
以上实例输出结果:
Line 1 - Value of c is 31
Line 2 - Value of c is 52
Line 3 - Value of c is 1092
Line 4 - Value of c is 52
Line 5 - Value of c is 2
Line 6 - Value of c is 2097152
Line 7 - Value of c is 99864
Copy after login

Python Bitwise operators

Bitwise operators treat numbers as binary to perform calculations. The bitwise operation rules in Python are as follows:

Operator

Description

Example

& Bitwise AND operator (a & b) Output result 12, binary interpretation: 0000 1100

| Bitwise Or operator (a | b) Output result 61, binary interpretation: 0011 1101

^ Bitwise XOR operator (a ^ b) Output result 49, binary interpretation: 0011 0001

~ Bitwise negation operator ( ~a) The output result is -61, binary interpretation: 1100 0011, in the two's complement form of a signed binary number.

<< Left shift operator a << 2 Output result 240, binary interpretation: 1111 0000

>> 右移动运算符 a >> 2 输出结果 15 ,二进制解释: 0000 1111

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

#!/usr/bin/python
 
a = 60            # 60 = 0011 1100
b = 13            # 13 = 0000 1101
c = 0
 
c = a & b;        # 12 = 0000 1100
print "Line 1 - Value of c is ", c
 
c = a | b;        # 61 = 0011 1101
print "Line 2 - Value of c is ", c
 
c = a ^ b;        # 49 = 0011 0001
print "Line 3 - Value of c is ", c
 
c = ~a;           # -61 = 1100 0011
print "Line 4 - Value of c is ", c
 
c = a << 2;       # 240 = 1111 0000
print "Line 5 - Value of c is ", c
 
c = a >> 2;       # 15 = 0000 1111
print "Line 6 - Value of c is ", c
Copy after login

以上实例输出结果:

Line 1 - Value of c is 12

Line 2 - Value of c is 61

Line 3 - Value of c is 49

Line 4 - Value of c is -61

Line 5 - Value of c is 240

Line 6 - Value of c is 15

Python逻辑运算符

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

运算符

描述

实例

and 布尔"与" - 如果x为False,x and y返回False,否则它返回y的计算值。 (a and b) 返回 true。

or 布尔"或" - 如果x是True,它返回True,否则它返回y的计算值。 (a or b) 返回 true。

not 布尔"非" - 如果x为True,返回False。如果x为False,它返回True。 not(a and b) 返回 false。

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

#!/usr/bin/python
 
a = 10
b = 20
c = 0
 
if ( a and b ):
   print "Line 1 - a and b are true"
else:
   print "Line 1 - Either a is not true or b is not true"
 
if ( a or b ):
   print "Line 2 - Either a is true or b is true or both are true"
else:
   print "Line 2 - Neither a is true nor b is true"
 
 
a = 0
if ( a and b ):
   print "Line 3 - a and b are true"
else:
   print "Line 3 - Either a is not true or b is not true"
 
if ( a or b ):
   print "Line 4 - Either a is true or b is true or both are true"
else:
   print "Line 4 - Neither a is true nor b is true"
 
if not( a and b ):
   print "Line 5 - a and b are true"
else:
   print "Line 5 - Either a is not true or b is not true"
   
以上实例输出结果:
Line 1 - a and b are true
Line 2 - Either a is true or b is true or both are true
Line 3 - Either a is not true or b is not true
Line 4 - Either a is true or b is true or both are true
Line 5 - a and b are true
Copy after login

Python成员运算符

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

运算符

描述

实例

in 如果在指定的序列中找到值返回True,否则返回False。 x 在 y序列中 , 如果x在y序列中返回True。

not in 如果在指定的序列中没有找到值返回True,否则返回False。 x 不在 y序列中 , 如果x不在y序列中返回True。

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

#!/usr/bin/python
 
a = 10
b = 20
list = [1, 2, 3, 4, 5 ];
 
if ( a in list ):
   print "Line 1 - a is available in the given list"
else:
   print "Line 1 - a is not available in the given list"
 
if ( b not in list ):
   print "Line 2 - b is not available in the given list"
else:
   print "Line 2 - b is available in the given list"
 
a = 2
if ( a in list ):
   print "Line 3 - a is available in the given list"
else:
   print "Line 3 - a is not available in the given list"
Copy after login

以上实例输出结果:

Line 1 - a is not available in the given list

Line 2 - b is not available in the given list

Line 3 - a is available in the given list

Python身份运算符

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

运算符

描述

实例

is is是判断两个标识符是不是引用自一个对象 x is y, 如果 id(x) 等于 id(y) , is 返回结果 1

is not is not是判断两个标识符是不是引用自不同对象 x is not y, 如果 id(x) 不等于 id(y). is not返回结果 1

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

#!/usr/bin/python
 
a = 20
b = 20
 
if ( a is b ):
   print "Line 1 - a and b have same identity"
else:
   print "Line 1 - a and b do not have same identity"
 
if ( id(a) == id(b) ):
   print "Line 2 - a and b have same identity"
else:
   print "Line 2 - a and b do not have same identity"
 
b = 30
if ( a is b ):
   print "Line 3 - a and b have same identity"
else:
   print "Line 3 - a and b do not have same identity"
 
if ( a is not b ):
   print "Line 4 - a and b do not have same identity"
else:
   print "Line 4 - a and b have same identity"
Copy after login

以上实例输出结果:

Line 1 - a and b have same identity

Line 2 - a and b have same identity

Line 3 - a and b do not have same identity

Line 4 - a and b do not have same identity

Python运算符优先级

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

运算符

描述

** 指数 (最高优先级)

~ + - 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)

* / % // 乘,除,取模和取整除

+ - 加法减法

>> << 右移,左移运算符

& 位 'AND'

^ | 位运算符

<= < > >= 比较运算符

<> == != 等于运算符

= %= /= //= -= += *= **= 赋值运算符

is is not 身份运算符

in not in 成员运算符

not or and 逻辑运算符

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

#!/usr/bin/python
 
a = 20
b = 10
c = 15
d = 5
e = 0
 
e = (a + b) * c / d       #( 30 * 15 ) / 5
print "Value of (a + b) * c / d is ",  e
 
e = ((a + b) * c) / d     # (30 * 15 ) / 5
print "Value of ((a + b) * c) / d is ",  e
 
e = (a + b) * (c / d);    # (30) * (15/5)
print "Value of (a + b) * (c / d) is ",  e
 
e = a + (b * c) / d;      #  20 + (150/5)
print "Value of a + (b * c) / d is ",  e
Copy after login

   

以上实例输出结果:

Value of (a + b) * c / d is 90

Value of ((a + b) * c) / d is 90

Value of (a + b) * (c / d) is 90

Value of a + (b * c) / d is 50


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template