Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python operator

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

The Python language supports the following types of operators:

Arithmetic operator

Comparison (relational) operator

Assignment operator

Logical operator

Bit operator

Member operator

Identity operator

Operator precedence

1. Algorithm operator * *

Note: in Python2.x, integers divided by integers can only get integers. If you want to get the decimal part, change one of the numbers to a floating point number.

The following hypothetical variable: axiom 10, dint 20:

| | operator | description | instance |

| | + | add-add two objects together | a + b output result 30 |

| |-| minus-get a negative number or one number minus another | a-b output result-10 |

| | multiply-multiply two numbers or return a string that is repeated several times | a / b output result 200 |

| | / | divide-x by y | b / an output result 2 |

| |% | Modulus-returns the remainder of division | b% an output result 0 |

| | Power-returns the power of x to the power of y, ab to the 20th power of 10 | output result 1000000000000000000 |

| | / / | Integer division-return the integer part of the quotient (rounded down) | > 9 Universe 2 4 >-9 Plus 2-5 |

two。 Comparison (relational) operator *

The following assumption is that variable an is 10 and variable b is 20:

Operator description instance

= = equals-compares whether 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.

< 小于 - 返回x是否小于y。所有比较运算符返回1表示真,返回0表示假。这分别与特殊的变量True和False等价。 (a < b) 返回 true。 = 大于等于 - 返回x是否大于等于y。 (a >

= b) returns False.

The number on the right specifies the number of digits to move a > > 2 output result 15, binary solution

A > > 2 output result 15, binary explanation: 0000 1111

For example:

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

A = 60 # 60 = 0011 1100

B = 13 # 13 = 0000 1101

C = 0

C = a & b; # 12 = 0000 1100

The value of print "1-c" is: ", c

C = a | b; # 61 = 0011 1101

The value of print "2-c" is: ", c

C = a ^ b; # 49 = 0011 0001

The value of print "3-c" is: ", c

C = ~ a; #-61 = 1100 0011

The value of print "4-c" is: ", c

C = a > 2; # 15 = 0000 1111

The value of print "6-c" is: ", c

Output:

The value of 1-c is: 12

The value of 2-c is: 61

The value of 3-c is: 49

The value of 4-c is:-61

The value of 5-c is: 240

The value of 6-c is: 15

5.Python logical operator

And x and y Boolean "and"-returns False if x is False,x and y, otherwise it returns the calculated value of y. (an and b) returns 20.

Or x or y Boolean "OR"-if x is non-0, it returns the value of x, otherwise it returns the calculated value of y. (an or b) returns 10.

Not not x Boolean "not"-returns False if x is True. If x is False, it returns True. Not (an and b) returns False

For example:

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

A = 10

B = 20

If an and b:

Print "1-variables an and b are both true"

Else:

Print "1-variables an and b have one that is not true"

If an or b:

Print "2-variables an and b are both true, or one of the variables is true"

Else:

Print "2-variables an and b do not modify the value of variable a for true" #

A = 0

If an and b:

Print "3-variables an and b are both true"

Else:

Print "3-variables an and b have one that is not true"

If an or b:

Print "4-variables an and b are both true, or one of the variables is true"

Else:

Print "4-variables an and b are not true"

If not (an and b):

Print "5-variables an and b are both false, or one of the variables is false"

Else:

Print "5-variables an and b are both true"

The output result of the above example:

1-variables an and b are both true

2-variables an and b are both true, or one of them is true

3-one of the variables an and b is not true

4-variables an and b are both true, or one of them is true

5-variables an and b are both false, or one of them is false

Python member operator

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

Operator description instance

In returns True if it finds a value in the specified sequence, otherwise it returns False. X is in the y sequence if x returns True in the y sequence.

Not in returns True if no value is found in the specified sequence, otherwise it returns False. X is not in the y sequence, if x does not return True in the y sequence.

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

Instance (Python 2.0 +)

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

A = 10

B = 20

List = [1,2,3,4,5]

If (an in list):

Print "1-variable an is in list in the given list"

Else:

Print "1-variable an is not in list in the given list"

If (b not in list):

Print "2-variable b is not in list in the given list"

Else:

Print "2-variable b in the given list list" # modifies the value of variable a

A = 2

If (an in list):

Print "3-variable an in the given list list"

Else:

Print "3-variable an is not in the given list list"

The output result of the above example:

1-variable an is not in list in the given list

2-variable b is not in list in the given list

3-variable an is in list in the given list

Python identity operator

The identity operator is used to compare the storage units of two objects

Operator description instance

Is is determines whether two identifiers are referenced from an object x is y, similar to id (x) = = id (y). If the same object is referenced, True is returned, otherwise False is returned.

Is not is not determines whether two identifiers are referenced from different objects x is not y, similar to id (a)! = id (b). The result True is returned if the reference is not the same object, otherwise False is returned.

Note: the id () function is used to get the memory address of the object.

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

Instance (Python 2.0 +)

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

A = 20

B = 20

If (an is b):

Print "1-an and b have the same logo"

Else:

Print "1-an and b do not have the same logo"

If (an is not b):

Print "2-an and b do not have the same logo"

Else:

Print "2-an and b have the same identity" # modify the value of variable b

B = 30

If (an is b):

Print "3-an and b have the same logo"

Else:

Print "3-an and b do not have the same logo"

If (an is not b):

Print "4-an and b do not have the same logo"

Else:

Print "4-an and b have the same logo"

The output result of the above example:

1-an and b have the same logo

2-an and b have the same logo

3-an and b do not have the same logo

4-an and b do not have the same logo

Is differs from = =:

Is is used to determine whether two variable reference objects are the same, and = = is used to determine whether the values of the reference variables are equal.

A = [1, 2, 3]

B = a

B is a

True

B = a

True

B = a [:]

B is a

False

B = a

True

Python operator precedence

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

Operator description

* * Index (highest priority)

~ +-Bitwise flipping, unary plus sign and minus sign (the last two methods are named + @ and-@)

/% / / multiplication, division, modularization and rounding, division, addition and subtraction

= comparison operator

= =! = equals operator

=% = / = / =-= + = * = * = assignment operator

Is is not identity operator

In not in member operator

Not and or logical operator

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

Instance (Python 2.0 +)

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

A = 20

B = 10

C = 15

D = 5

E = 0

E = (a + b) c / d # (30 15) / 5

The result of print "(a + b) * c / d operation is:", e

E = (a + b) c) / d # (30 15) / 5

The result of print "(a + b) * c) / d is:", e

E = (a + b) (c / d); # (30) (15max 5)

The result of print "(a + b) * (c / d) operation is:", e

E = a + (b c) / d; # 20 + (150T)

The result of print "a + (b c) / d operation is:", e

The output result of the above example:

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

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

(a + b) (c / d) the result is: 90

The result of a + (b c) / d is: 50

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 217

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report