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

What are the Python3 operators

2025-03-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "what are the Python3 operators", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "what are the Python3 operators"?

1. Operators supported by the Python language

(1) arithmetic operator

(2) comparison (relational) operator

(3) assignment operator

(4) logical operator

(5) bit operator

(6) member operator

(7) identity operator

two。 Arithmetic operator

(1) + (plus): the addition of two objects

(2)-(minus): subtract two numbers to get a negative or positive number

(3) * (multiplication): multiply two numbers or return a string that is repeated several times

(4) / (division): division of two numbers

(5)% (modulo): divide two numbers and return the remainder of the division.

(6) * (Power): returns the y power of x

(7) / / (take integral division): take down the integer close to the divisor

3. Comparison (relational) operator: returns True or False

(1) = (equal to): compare whether two objects are equal

(2)! = (not equal): compare whether two objects are not equal

(3) > (greater than): returns whether x is greater than y

(4)

< (小于): 返回x是否小于y (5) >

= (greater than or equal to): returns whether x is greater than or equal to y

(6) (right shift operator): each binary of the Operand is shifted several bits to the right, and the number to the right of "> >" specifies the number of digits to be moved.

7. Member operator

(1) in: (x in y) determines whether x is in sequence y, and if x returns True in sequence y, it no longer returns False.

(2) not in: (x not in y) determines that x is not in sequence y, and if x does not return True in sequence y, return False in sequence y.

8. Identity operator

(1) is: is determines whether two identifiers are referenced from the same object, and returns True if the same object is referenced, otherwise False.

(2) is not: is not determines whether two identifiers are referenced from different objects. If they are not referenced to the same object, the result returns True, otherwise, False.

9. Precedence of operator (from high to low)

Operator description

(1) * * Index (highest priority)

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

(3) * /% / / multiplication, division, modularization and integral division

(4) +-addition and subtraction

(5) > = comparison operator

(9) = =! = equals operator

(10) =% = /-= + = * = * * = assignment operator

(11) is is not identity operator

(12) in not in member operator

(13) not and or logical operator

10. The difference between is and =:

Is is used to determine whether two variable reference objects are the same.

= = is used to determine whether the values of the reference variables are equal.

Sample code:

#! / usr/bin/python3#-*-coding: utf-8-*-# author: liuhefei# Time: 20119 Python 11 desc: Python operator if _ _ name__ = = "_ _ main__": a = 33 b = 24 # arithmetic operator C1 = a + b # addition c2 = a-b # subtraction c3 = a * b # multiplication c4 = a / b # division c5 = a% b # modulo c6 = a * * 2 # Power c7 = a / / b # take and divide print ("addition C1 =" C1) print ("subtraction c2 =", c2) print ("multiplication c3 =", c3) print ("division c4 =", c4) print ("modulo c5 =", c5) print ("power c6 =", c6) print ("integer division c7 =" C7) print ("\ n") # comparison operator if (a = = b): print ("1-an equals b") else: print ("1-an is not equal to b") if (a! = b): print ("2-an is not equal to b") else: print ("2-an equals b") if (a

< b): print("3 - a 小于 b") else: print("3 - a 大于等于 b") if (a >

B): print ("4-an is greater than b") else: print ("4-an is less than or equal to b") # modify variable a The value of b is a = 14 b = 8 if (a = a): print ("6-b is greater than or equal to a") else: print ("6-b less than a") print ("\ n") # assignment operator a = 17 b = 25 c = 0 c = a + b print ("1-c value is:", c) c + = a print ("2-c value is:", c) c * = a print ("3-c value is:" C) c / = a print ("4-c values are:", c) c = 2 c% = a print ("5-c values are:", c) c * * = a print ("6-c values are:", c) c / / = a print ("7-c values are:" C) print ("\ n") # bit operator x1 = 77 # 1101 x2 = 29 # 1 1101 y1 = x1 & x2 # 0000 1101 y2 = x1 | x2 # 0101 1101 y3 = x1 ^ x2 # 0101 0000 y4 = x1 #-0b1001110 y5 = x1 > 3 # 00000011 print ("bitwise and operation (&) y1 =", y1) print ("bitwise or operation (|) y2 =", y2) print ("bitwise or operation (^) y3 =" Y3) print ("inverse bitwise operation (~) y4 =", y4) print ("left shift operation () y1 =", y6) print ("\ n") # logical operation n = 13 m = 4 if n and m: print ("1-variable n and m are true") else: print ("1-variable n and m have a False") if n or m: print ("2-variable n and m are both True") Or one of them is True ") else: print (" 2-variable n and m are both False ") if not n: print (" 3-variable n is False ") else: print (" 3-variable n is True ") if not (n and m): print (" 4-variable n and m are False ") Or one of them is False ") else: print (" 4-variables n and m are True ") print ("\ n ") # member operator A1 = 12 b1 = 3 list = [1, 2, 3, 4 5] if A1 in list: print ("1-variable A1 is in the given list list") else: print ("1-variable A1 is not in the given list list") if b1 not in list: print ("2-variable b1 is not in the given list list") else: print ("2-variable b1 in the given list list") print ("\ n") # identity operator S1 = 100s2 ='a' if S1 is S2: print ("1-S1 and S2 are referenced from one object") else: print ("1-S1 and S2 come from different objects") if S1 is not S2: print ("2-S1 and S2 come from different objects") else: print ("2-S1 and S2 are referenced from the same object") to this point I believe that you have a deeper understanding of "what are the Python3 operators"? you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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: 0

*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

Development

Wechat

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

12
Report