Python3 operator (8) and python3 Operator

Source: Internet
Author: User

Python3 operator (8) and python3 Operator
What is an operator?

A simple example4 + 5 = 9. In the example,4And5CalledOperands,"+"Is called an operator.

Python supports the following types of operators:

1. Arithmetic Operators

Assume that variable a is 10 and variable B is 20:

Example:

1 #! /Usr/bin/python3 2 3 a = 21 4 B = 10 5 c = 0 6 7 c = a + B 8 print ("1-c value :", c) 9 10 c = a-b11 print ("2-c value:", c) 12 13 c = a * b14 print ("3-c value: ", c) 15 16 c = a/b17 print (" 4-c value: ", c) 18 19 c = a % b20 print ("5-c value:", c) 21 22 # modify variables a, B, c23 a = 224 B = 325 c = a ** B 26 print ("6-c value:", c) 27 28 a = 1029 B = 530 c = a // B 31 print ("7-c value:", c)
View Code

Output result:

1 1-c value: 312 2-c value: 113 3-c value: 2104 4-c value: 2.15 5-c value: 16 6-c: 87 7-c: 2
View Code2. comparison operator

Assume that variable a is 10 and variable B is 20:

Example:

1 #! /Usr/bin/python3 2 3 a = 21 4 B = 10 5 c = 0 6 7 if (a = B): 8 print ("1-a = B ") 9 else: 10 print ("1-a is not equal to B") 11 12 if (! = B): 13 print ("2-a is not equal to B") 14 else: 15 print ("2-a is equal to B") 16 17 if (a <B ): 18 print ("3-a less than B") 19 else: 20 print ("3-a greater than or equal to B") 21 22 if (a> B ): 23 print ("4-a greater than B") 24 else: 25 print ("4-a less than or equal to B ") 26 27 # modify the values of variables a and B 28 a = 5; 29 B = 20; 30 if (a <= B ): 31 print ("5-a less than or equal to B") 32 else: 33 print ("5-a greater than B") 34 35 if (B> = ): 36 print ("6-B greater than or equal to a") 37 else: 38 print ("6-B less than ")
View Code

Output result of the above instance:

1 1-a is not equal to b2 2-a is not equal to b3 3-a is greater than or equal to b4 4-a is greater than b5 5-a is less than or equal to b6 6-B is greater than or equal to
View Code3. value assignment operator

Example:

1 #! /Usr/bin/python3 2 3 a = 21 4 B = 10 5 c = 0 6 7 c = a + B 8 print ("1-c value :", c) 9 10 c + = a11 print ("2-c value:", c) 12 13 c * = a14 print ("3-c value :", c) 15 16 c/= a 17 print ("4-c value:", c) 18 19 c = 220 c % = a21 print ("5-c value:", c) 22 23 c ** = a24 print ("6-c value: ", c) 25 26 c // = a27 print (" 7-c value: ", c)
View Code

Output result of the above instance:

1 1-c value: 312 2-c value: 523 3-c value: 10924 4-c value: 52.05 5-c value: 26 6-c: 20971527 7-c: 99864
View Code 4. bitwise operators

Bitwise operators regard numbers as binary values for calculation.

The binary format of variable a is 60 and variable B is 13 in the following table:

 1 a = 0011 1100 2  3 b = 0000 1101 4  5 ----------------- 6  7 a&b = 0000 1100 8  9 a|b = 0011 110110 11 a^b = 0011 000112 13 ~a  = 1100 0011

 

Example:

1 #! /Usr/bin/python3 2 3 a = 60 #60 = 0011 1100 4 B = 13 #13 = 0000 1101 5 c = 0 6 7 c = a & B; #12 = 0000 1100 8 print ("1-c:", c) 9 10 c = a | B; #61 = 0011 1101 11 print ("2-c:", c) 12 13 c = a ^ B; #49 = 0011 000114 print ("3-c:", c) 15 16 c = ~ A; #-61 = 1100 001117 print ("4-c value:", c) 18 19 c = a <2; #240 = 1111 000020 print ("5-c:", c) 21 22 c = a> 2; #15 = 0000 111123 print ("6-c value:", c)
View Code

Output result of the above instance:

1 1-c value: 122 2-c value: 613 3-c value: 494 4-c value: -615 5-c: 2406 6-c: 15
View Code5. logical operators

 

Example:

1 #! /Usr/bin/python3 2 3 a = 10 4 B = 20 5 6 if (a and B): 7 print ("1-variables a and B are both true ") 8 else: 9 print ("1-variables a and B have a not true") 10 11 if (a or B ): 12 print ("2-variables a and B are both true, or one of the variables is true") 13 else: 14 print ("2-variables a and B are not true ") 15 16 # modify the value of variable a 17 a = 018 if (a and B): 19 print ("3-variables a and B are both true") 20 else: 21 print ("3-variables a and B have a not true") 22 23 if (a or B): 24 print ("4-variables a and B are both true, or one of the variables is true ") 25 else: 26 print (" 4-variables a and B are not true ") 27 28 if not (a and B ): 29 print ("5-variables a and B are both false, or one of the variables is false") 30 else: 31 print ("5-variables a and B are both true ")
View Code

Output result of the above instance:

1 1-variables a and B are both true2 2-variables a and B are both true, or if one of the variables is true3 3-variables a and B, either of them is true if they are not true4 4-variables a and B, or one of the variables is true5-variables a and B are both false, or one of the variables is false.
View Code

 

6. member operators

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

Example:

1 #! /Usr/bin/python3 2 3 a = 10 4 B = 20 5 list = [1, 2, 3, 4, 5]; 6 7 if (a in list ): 8 print ("1-variable a in the list given") 9 else: 10 print ("1-variable a is not in the list given ") 11 12 if (B not in list): 13 print ("2-variable B is not in list in the given list") 14 else: 15 print ("2-variable B in the given list") 16 17 # modify the value of variable a 18 a = 219 if (a in list ): 20 print ("3-variable a in the list given") 21 else: 22 print ("3-variable a is not in the list given ")
View Code

 

Output result of the above instance:

1 1-variable a is not in the given list. list 2 2-variable B is not in the given list. list 3-variable a is in the list of the given list.
View Code

 

7. Identity Operators

Identity operators are used to compare the storage units of two objects.

Note:The id () function is used to obtain the object memory address.

Example:

1 #! /Usr/bin/python3 2 3 a = 20 4 B = 20 5 6 if (a is B): 7 print ("1-a and B have the same ID ") 8 else: 9 print ("1-a and B do not have the same identifier") 10 11 if (id (a) = id (B )): 12 print ("2-a and B have the same identifier") 13 else: 14 print ("2-a and B do not have the same identifier ") 15 16 # modify the value of variable B 17 B = 3018 if (a is B): 19 print ("3-a and B have the same identifier") 20 else: 21 print ("3-a and B do not have the same identifier") 22 23 if (a is not B): 24 print ("4-a and B do not have the same identifier ") 25 else: 26 print ("4-a and B have the same ID ")
View Code

 

Output result of the above instance:

1 1-a and B have the same ID 2 2-a and B have the same ID 3 3-a and B do not have the same ID 4-a and B do not have the same ID
View Code

 

Note: is and = are different

Is used to determine whether two variables reference the same object, = is used to determine whether the value of the referenced variables is equal.

For example:

 1 >>>a = [1, 2, 3] 2 >>> b = a 3 >>> b is a  4 True 5 >>> b == a 6 True 7 >>> b = a[:] 8 >>> b is a 9 False10 >>> b == a11 True

 

8. Operator priority

The following table lists all operators with the highest to lowest priority:

Example:

1 #! /Usr/bin/python3 2 3 a = 20 4 B = 10 5 c = 15 6 d = 5 7 e = 0 8 9 e = (a + B) * c/d #(30*15)/510 print ("(a + B) * c/d:", e) 11 12 e = (a + B) * c)/d #(30*15)/513 print ("(a + B) * c) /d: ", e) 14 15 e = (a + B) * (c/d); # (30) * (15/5) 16 print ("(a + B) * (c/d) Calculation Result:", e) 17 18 e = a + (B * c)/d; #20 + (150/5) 19 print ("a + (B * c)/d:", e)
View Code

Output result of the above instance:

1 (a + B) * c/d Calculation Result: 90.02 (a + B) * c)/d Calculation Result: 90.03 (a + B) * (c/d) Calculation Result: 90.04 a + (B * c)/d Calculation Result: 50.0
View Code

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.