Python basic in...login
Python basic introductory tutorial
author:php.cn  update time:2022-04-18 16:14:50

Python operators



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 "+" is called the operator.

The Python language supports the following types of operators:

  • Arithmetic operators

  • Comparison (relational) operators

  • Assignment operator

  • Logical operator

  • Bitwise operator

  • Member operator

  • Identity operator

  • Operator precedence

Next let us learn Python operators one by one.


Python arithmetic operators

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

OperatorDescriptionExample
+Add - Add two objectsa + b Output result 30
-Subtract - Get a negative number or one number minus another numbera - b Output result-10
*Multiplication - Multiply two numbers or return a string repeated several timesa * b Output result 200
/Divide - x divided by yb / a Output result 2
% Modulo - Returns the remainder of division b % a Output result 0
**Power - Returns x raised to the yth power a**b is 10 raised to the 20th power, and the output result is 100000000000000000000
//Return the integer part of the quotient9//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
# -*- coding: UTF-8 -*-

a = 21
b = 10
c = 0

c = a + b
print "1 - The value of c is:", c

c = a - b
print "2 - The value of c is: ", c

c = a * b
print "3 - The value of c is: ", c

c = a / b
print "4 - The value of c is: ", c

c = a % b
print "5 - The value of c is: ", c

# Modify variables a, b, c
a = 2
b = 3
c = a**b
print "6 - The value of c is:", c

a = 10
b = 5
c = a//b
print "7 - The value of c is:", c

Try it »

The above example output result:

1 - The value of c is: 31
2 - The value of c is: 11
3 - The value of c is: 210
4 - The value of c is: 2
5 - The value of c is: 1
6 - The value of c is: 8
7 - The value of c is: 2

Python comparison operator

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

OperatorDescriptionExample
==Equal - compare whether the objects are equal(a == b) Return False.
!=Not equal to - Compares whether two objects are not equal(a != b) Returns true.
<>Not equal to - 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
# -*- coding: UTF-8 -*-

a = 21
b = 10
c = 0

if ( a == b ):
print "1 - a equals b"
else:
print "1 - a is not equal to b"

if (a != b):
print "2 - a is not equal to b"
else:
print "2 - a is equal to b"

if ( a <> b ):
print "3 - a is not equal to b"
else:
print "3 - a is equal to b"

if ( a < b ):
print "4 - a is less than b"
else:
print "4 - a is greater than or equal to b"

if ( a > b ):
print "5 - a is greater than b"
else:
print "5 - a is less than or equal to b"

# Modify the values ​​of variables a and b
a = 5 ;
b = 20;
if ( a <= b ):
print "6 - a is less than or equal to b"
else:
print "6 - a is greater than b"

if ( b >= a ):
print "7 - b is greater than or equal to b"
else:
print "7 - b is less than b"

The output result of the above example:

1 - a is not equal to b
2 - a is not equal to b
3 - a is not equal to b
4 - a is greater than or equal to b
5 - a is greater than b
6 - a is less than or equal to b
7 - b is greater than or equal to b

Python assignment operator

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

##%=modulo assignment operatorc % = a is equivalent to c = c % a##**=//=

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

#!/usr/bin/python
# -*- coding: UTF-8 -*-

a = 21
b = 10
c = 0

c = a + b
print "1 - The value of c is: ", c

c + = a
print "2 - The value of c is: ", c

c *= a
print "3 - The value of c is: ", c

c /= a
print "4 - The value of c is: ", c

c = 2
c %= a
print "5 - The value of c is: ", c

c **= a
print "6 - The value of c is:", c

c //= a
print "7 - The value of c is:", c

Output result of the above example:

1 - The value of c is: 31
2 - The value of c is: 52
3 - The value of c is: 1092
4 - The value of c is: 52
5 - The value of c is: 2
6 - The value of c is: 2097152
7 - The value of c is: 99864

Python bitwise operators

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

In the following table, variable a is 60 and b is 13.

OperatorDescriptionExample
=Simple assignment operatorc = a + b The operation of a + b The resulting assignment is c
+=Additional assignment operatorc += a is equivalent to c = c + a
-=Subtractive assignment operatorc -= a is equivalent to c = c - a
*=Multiplication assignment operatorc *= a is equivalent to c = c * a
/=Division assignment operator c /= a is equivalent to c = c / a
Power assignment operatorc **= a is equivalent to c = c ** a
Take integer division assignment operatorc //= a is equivalent to c = c // a
##|Bitwise OR operator: As long as one of the two corresponding binary bits is 1, the result bit will be 1. (a | b) The output result is 61, binary interpretation: 0011 1101##^~##<<Left shift operator: Each binary digit of the operand is shifted to the left by a certain number of bits, specified by the number on the right of "<<" The number of digits to move, the high bits are discarded, and the low bits are filled with 0s. a << 2 The output result is 240, binary interpretation: 1111 0000>>Right shift operator: " >>"All binary digits of the operand on the left are shifted to the right by a certain number of digits,">>"The number on the right specifies the number of digits to movea >> 2 The output result is 15, binary Explanation: 0000 1111

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

#!/usr/bin/python
# -*- coding: UTF-8 -*-

a = 60             # 60                                                                                                                                                                                       because 00
print " 1 - The value of c is: ", c

c = a | b; # 61 = 0011 1101
print "2 - The value of c is: ", c

c = a ^ B;#49 = 0011 0001
PRINT "3-C The value is:", C

C = ~ A;#-61 = 1100 0011
PRINT "4-C The value is: ", c

c = a << 2; # 240 = 1111 0000
print "5 - The value of c is: ", c

c = a >> 2 ;      # 15 = 0000 1111
print "6 - The value of c is:", c


The above example output result:
1 - The value of c is: 12

2 - The value of c is: 61

3 - The value of c is: 49
4 - The value of c is: -61
5 - The value of c is: 240
6 - The value of c is: 15


##Python logical operators
Python language supports logical operators. The following assumes that variable a is 10 and b is 20:

Operator

OperatorDescriptionInstance
& Bitwise AND operator: Two values ​​participating in the operation, if the two corresponding bits are 1, the result of the bit is 1, otherwise it is 0(a & b) The output result is 12, binary interpretation : 0000 1100
Bitwise XOR operator: when two corresponding When the binary bits are different, the result is 1(a ^ b) and the output result is 49. Binary interpretation: 0011 0001
bitwise Negation operator: Negate each binary bit of the data, that is, change 1 to 0, and change 0 to 1(~a) The output result is -61, binary interpretation: 1100 0011, in a The two's complement form of a signed binary number.
Logical expressionDescriptionInstanceand x and yBoolean AND - if x is False, x and y returns False, otherwise it returns the calculated value of y. (a and b) returns 20. orx or yBoolean "or" - if x is non-zero, it returns the value of x, otherwise it returns the calculation of y value. (a or b) returns 10. notnot xBoolean "not" - If x is True, returns False. If x is False, it returns True. not(a and b) returns False

Output result of the above example:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

a = 10
b = 20

if ( a and b ):
print "1 - Variables a and b are both true"
else:
print "1 - Variables a and b are true One is not true"

if ( a or b ):
print "2 - Both variables a and b are true, or one of the variables is true"
else:
print " 2 - Neither variables a nor b are true"

# Modify the value of variable a
a = 0
if (a and b):
print "3 - Variables a and b Both are true"
else:
print "3 - Variables a and b One of them is not true"

if (a or b):
print "4 - Variables a and b Both are true, or one of the variables is true"
else:
print "4 - Neither variables a nor b are true"

if not( a and b ):
print "5 - Variables a and b are both false, or one of the variables is false"
else:
print "5 - Variables a and b are both true"

The above example output Result:

1 - Variables a and b are both true
2 - Variables a and b are both true, or one of the variables is true
3 - Variables a and b have one Not true
4 - Variables a and b are both true, or one of the variables is true
5 - Variables a and b are both false, or one of the variables is false

Python Member Operator

In addition to some of the above operators, Python also supports member operators. The test instance contains a series of members, including strings, lists or tuples. .

OperatorDescriptionInstance
in Returns True if a value is found in the specified sequence, False otherwise. x is in the y sequence, returns True if x is in the y sequence.
not in Returns True if the value is not found in the specified sequence, False otherwise. x is not in the y sequence, returns True if x is not in the y sequence.

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

#!/usr/bin/python
# -* - coding: UTF-8 -*-

a = 10
b = 20
list = [1, 2, 3, 4, 5 ];

if ( a in list ):
print "1 - Variable a is in the given list list"
else:
print "1 - Variable a is not in the given list list"

if (b not in list):
print "2 - Variable b is not in the given list list"
else:
print "2 - Variable b is in the given list list"

# Modify the value of variable a
a = 2
if ( a in list ):
print "3 - Variable a is in the given list list "
else :
print "3 - Variable a is not in the given list list"

The above example output result:

1 - Variable a is not in the given list in list list
2 - variable b is not in the given list list
3 - variable a is in the given list list

Python identity operator

Identity operator used to compare storage locations of two objects

##is notis not is to determine whether the two identifiers refer to different objectsx is not y, if id(x) is not equal to id(y).

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

#!/usr/bin/python
# -*- coding: UTF-8 -*-

a = 20
b = 20

if ( a is b ):
print "1 - a and b have the same identity"
else:
print " 1 - a and b do not have the same identifier"

if ( id(a) == id(b) ):
print "2 - a and b have the same identifier"
else:
print "2 - a and b do not have the same identifier"

# Modify the value of variable b
b = 30
if ( a is b ):
print "3 - a and b have the same identity"
else:
print "3 - a and b do not have the same identity"

if (a is not b):
print "4 - a and b do not have the same identifier"
else:
print "4 - a and b have the same identifier"

The output result of the above example:

1 - a and b have the same identity
2 - a and b have the same identity
3 - a and b do not have the same identity
4 - a and b do not have the same identity

Python operator precedence

The following table lists all operators from highest to lowest precedence:

OperatorDescriptionInstance
isis is used to determine whether two identifiers refer to an object. x is y, if id(x) is equal to id(y ), is returns the result 1
is not Return result 1
##<= < > >=Comparison operators<> == !=Equal operator##= %= /= //= -= += *= ** =is is notin not innot or and

The following example demonstrates the operation of all operator precedence in Python:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

a = 20
b = 10
c = 15
d = 5
e = 0

e = (a + b) * c / d #( 30 * 15 ) / 5
print "(a + b) * c / d The result of the operation is: ", e

e = ((a + b) * c) / d # (30 * 15 ) / 5
print "((a + b) * c) / d The operation result is: ", e

e = (a + b) * (c / d); # (30) * (15/5)
print "(a + b) * (c / d) The operation result is: ", e

e = a + (b * c) / d; # 20 + (150 /5)
print "a + (b * c) / d The operation result is: ", e

The output result of the above example is:

(a + b ) * c / d The operation result is: 90
((a + b) * c) / d The operation result is: 90
(a + b) * (c / d) The operation result is: 90
a + (b * c) / d The result of the operation is: 50


Operations SymbolDescription
**Index (highest priority)
~ + -Bitwise flip, unary plus sign and minus sign (the last two methods are named +@ and -@)
* / % //Multiplication, division, modulo and integer division
+ -Addition and subtraction
>> < ;<Right shift, left shift operator
&bit'AND'
^ |Bitwise operators
Assignment operator
Identity operator
Member operator
Logical operator