The beginning of the Python operator _ life if only as the initial

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise bitwise operators

This series of articles in the process of learning Python encountered a bottleneck, go to the Novice tutorial to experience the fun of Python, improve the overall knowledge framework. Enhance your understanding of the Python language and grasp the knowledge points of Python. This article is from: Rookie tutorial (study record, copyright http://www.runoob.com)

Python 3 What is an operator?

This section mainly describes the Python operators. To give a simple example 4 +5 = 9 . In the example,4 and 5 are called operands , and "+" is called an operator.

The Python language supports the following types of operators:

    • Arithmetic operators
    • Compare (relational) operators
    • Assignment operators
    • logical operators
    • Bitwise operators
    • Member operators
    • Identity operator
    • Operator Precedence

Next, let's learn about Python's operators.

Python arithmetic operators

The following assumes that the variable A is 10 and the variable B is 21:

operator Description Example
+ Add-Two objects added A + B output result 31
- Minus-get negative numbers or one number minus the other -B Output Result-11
* Multiply by two number or return a string that is repeated several times A * b output result 210
/ Except-X divided by Y B/A Output Results 2.1
% Modulo-Returns the remainder of the division B% A output result 1
** Power-Returns the Y power of X A**b is 10 of the 21-time Square
// Divide-Returns the integer part of the quotient 9//2 output result 4, 9.0//2.0 output 4.0

The following example shows the operation of all the arithmetic operators of Python:

#!/usr/bin/python3 a = 21b = 10c = 0 c = a + bprint ("1-c value is:", c) C = A-bprint ("2-c value is:", c) c = A * Bprint ("3  -The value of C is: ", c) C = A/bprint (" 4-c value is: ", c) C = a% bprint (" 5-c value is: ", c) # Modify variable A, B, CA = 2b = 3c = A**b Print (" 6 -The value of C is: ", c) A = 10b = 5c = a//b Print (" The value of the 7-c is: ", c)

The result of the above example output:

The value of 1-C is: The value of 312-C is: The value of 113-C is: The value of 2104-C is: 2.15-c value is: 16-c value is: 2
Python comparison operators

The following assumes that the variable A is 10 and the variable B is 20:

operator Description Example
== Equals-compares objects for equality (A = = B) returns FALSE.
!= Not equal-compares two objects for unequal (A! = b) returns TRUE.
> Greater than-returns whether x is greater than Y (A > B) returns FALSE.
< Less-Returns whether x is less than Y. All comparison operators return 1 for true, and return 0 for false. This distinction is equivalent to the special variable true and false. Note that these variable names are capitalized. (A < b) returns TRUE.
>= Greater than or equal-returns whether X is greater than or equal to Y. (a >= B) returns FALSE.
<= Less than or equal-returns whether X is less than or equal to Y. (a <= B) returns True.

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

#!/usr/bin/python3 a = 21b = 10c = 0 if (a = = B):   print ("1-a equals B") Else:   print ("1-a not equal to B") if (a! = b):   print ("2-a not equal to B") Else:   print ("2-a equals B") if (a < b):   print ("3-a less than B") Else:   print ("3-a Greater than or equal to B ") if (a > B):   print (" 4-a is greater than B ") Else:   print (" 4-a is less than or equal to B ") # Modify the values of variables A and b a = 5;b = 20;if (A < = b):   print ("5-a less than equals B") Else:   print ("5-a greater than  B") if (b >= a):   print ("6-b greater than equals a") Else:
   print ("6-b less than a")

The result of the above example output:

1-a not equal to b2-a not equal to b3-a greater than or equal to b4-a greater than b5-a less than equals b6-b greater than or equal to a
Python assignment operator

The following assumes that the variable A is 10 and the variable B is 20:

operator Description Example
= Simple assignment operator c = A + B assigns the result of the operation 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 is equivalent to C = c/a
%= Modulo assignment operator C%= A is equivalent to C = c% A
**= Power assignment operator C **= A is equivalent to C = c * * A
//= Take the divisible assignment operator C//= A is equivalent to C = c//A

The following example shows the operation of all Python assignment operators:

#!/usr/bin/python3 a = 21b = 10c = 0 c = a + bprint ("1-c value is:", c) c + = Aprint ("2-c value is:", c) C *= aprint ("3-c value For: ", c) C/= a print (" 4-c value is: ", c) C = 2c%= Aprint (" 5-c value is: ", c) C **= aprint (" 6-c value is: ", c) C//= aprint (" 7 -The value of C is: ", c)

The result of the above example output:

The value of 1-C is: The value of 312-C is: The value of 523-C is: The value of 10924-C is: 52.05-c value is: 26-c value is: 99864
Python bitwise operators

A bitwise operator computes a number as a binary. The bitwise algorithms in Python are as follows:

In the following table, the variable A is 60,b as 132 in the following format:

A = 0011 1100b = 0000 1101-----------------A&b = 0000 1100a|b = 0011 1101a^b = 0011 0001~a  = 1100 0011
operator Description Example
& Bitwise AND Operator: Two values that participate in the operation, if two corresponding bits are 1, the result of that bit is 1, otherwise 0 (A & B) Output result 12, binary interpretation: 0000 1100
| Bitwise OR operator: As long as the corresponding two binary has one for 1 o'clock, the result bit is 1. (A | b) output result 61, Binary interpretation: 0011 1101
^ Bitwise XOR Operator: When two corresponding binary differences, the result is 1 (a ^ b) output result 49, binary interpretation: 0011 0001
~ Bitwise inverse operator: each bits of the data is reversed, that is, 1 is changed to 0, and 0 to 1. ~x similar to -x-1 (~a) Output result-61, Binary interpretation: 1100 0011, in the complement form of a signed binary number.
<< Left move operator: The operands of the operands all shift left several bits, the number of "<<" to the right to specify the number of bits moved, high drop, low 0. A << 2 output results 240, binary interpretation: 1111 0000
>> Right move operator: Move all the binary of the left operand of ">>" to the right of several bits, and the number to the right of ">>" to specify the number of bits to move A >> 2 output results 15, binary interpretation: 0000 1111

The following example shows the operation of all Python bitwise operators:

#!/usr/bin/python3 A = $ =            0011 1100 b =            # 0 = 0000 1101 c = + =-c = A & B;        # = 0000 1100print ("1-c value:", c) c = A | b;        # 0011 = 1101 Print ("The value of 2-c is:", c) C = a ^ B;        # 0011 = 0001print ("3-c value:", c) c = ~a;           # -61 = 1100 0011print ("4-c value:", c) C = a << 2;       # 1111 = 0000print ("5-c value:", c) C = a >> 2;       # = 0000 1111print ("6-c value:", c)

The result of the above example output:

The value of the 1-C is: The value of 122-C is: The value of 613-C is: The value of 494-C is: -615-c value is: 15
Python logical operators

The Python language supports logical operators, with the following assumption that the variable A is ten and B is 20:

operator Logical Expressions Description Example
and X and Y Boolean "and"-if x is False,x and y returns FALSE, otherwise it returns the computed value of Y. (A and B) returns 20.
Or X or Y Boolean "or"-if x is True, it returns the value of x, otherwise it returns the computed value of Y. (A or B) returns 10.
Not Not X Boolean "Non"-returns False if X is True. If X is False, it returns TRUE. Not (A and B) returns False

The following example shows the operation of all the logical operators of Python:

#!/usr/bin/python3 A = 10b = if (A and B):   print ("1-variables A and B are true") Else:   print ("1-variables A and B have one not for Tru E ") if (A or B):   print (" 2-variables A and B are true, or one of the variables is true ") Else:   print (" 2-variables A and B are not true ") # Modify the value of variable a = 0if (A and B):   Print ("3-variables A and B are true") Else:   print ("3-variables A and B have one not true") if (A or B):   print ("4-variables A and B are true, or one of the variables is true") Else:   print ("4-variables A and B are not true") if not (A and B):   print ("5-Variable A and B is false, or one of the variables is false ") Else:   print (" 5-variables A and B are true ")

The result of the above example output:

1-Variables A and B are both true2-variables A and B are true, or one of the variables is TRUE3-variables A and B have a not true4-variables A and B are true, or one of the variables is true5-variable A and B is false, or one of the variables is false
Python member operators

In addition to some of the above operators, Python also supports member operators, which contain a series of members, including strings, lists, or tuples.

operator Description Example
Inch Returns False if the value found in the specified sequence returns True. x 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 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:

#!/usr/bin/python3 A = 10b = 20list = [1, 2, 3, 4, 5]; if (A in list):   print ("1-variable A in the list of given lists") Else:   print ("1-variable A is not listed in the given list") if (b not in list): C6/>print ("2-variable B is not in the list given") Else:   print ("2-variable B in the given list") # modifies the value of variable a = 2if (A in list):   PR Int ("3-variable A is in list in a given listing") Else:   print ("3-variable A is not in the list given")

The result of the above example output:

1-Variable A is not in the given list 2-variable B is not in the given list 3-variable A in the given list
Python identity operator

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

operator Description Example
Is is to determine whether two identifiers are referenced from an object x is y, similar to ID (x) = = ID (y) , returns True if the same object is referenced, otherwise False
is not Is does not determine whether two identifiers are referenced from different objects x is not y, similar to ID (a)! = ID (b). Returns True if the reference is not the same object, otherwise False is returned.

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

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

#!/usr/bin/python3 A = 20b = if (A is B):   print ("1-a and B have the same identity") Else:   print ("1-a and b do not have the same identity") if (ID (a) = = ID (b)):   print ("2-a and B have the same identity") Else:   print ("2-a and b do not have the same identity") # Modify variable B's value B = 30if (A is b):   PR Int ("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 identity") Else:   Print ("4-a and B have the same identity")

The result of the above example output:

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

IS and = = difference:

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

IS and! = differ from the above, one compares the reference object and the other compares the value of the two.

>>>a = [1, 2, 3]>>> B = a>>> B is a true>>> b = = atrue>>> B = A[:]>>> ; B is afalse>>> b = = Atrue
Python operator Precedence

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

operator Description
** Index (highest priority)
~ + - Bitwise flip, unary Plus and minus (the last two methods are called [email protected] and [email protected])
* / % // Multiply, divide, modulo, and divide
+ - Addition subtraction
>> << Shift right, left shift operator
& Bit ' and '
^ | Bitwise operators
<= < > >= Comparison operators
<> = = = equals operator
= %= /= //= -= += *= **= Assignment operators
Is isn't Identity operator
In No in Member operators
Not OR and logical operators

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

#!/usr/bin/python3 a = 20b = 10c = 15d = 5e = 0 E = (A + b) * C/D       # (+ *)/5print ("(A + B) * The result of the C/D operation is:", 
   e) E = ((A + b) * c)/d     # (+ *)/5print ("((A + b) * c)/d The result of the operation is:",  e) e = (A + b) * (C/D);    # (+) * (15/5) print ("(A + B) * (C/D) operation result is:",  e) e = a + (b * c)/D;      #  + (150/5) print ("A + (b * c)/d operation result is:",  e)

The result of the above example output:

(A + B) * The result of the C/D operation is: 90.0 ((A + b) * c)/d operation result is: 90.0 (A + B) * (C/D) operation result is: 90.0a + (b * c)/d results are: 50.0

Extended:

And the expression is evaluated from left to right in Python, and if all values are true, the last value is returned, and if False, the first false value is returned;

The or is also from left to has a calculated expression, returning the first true value;

Where the number 0 is false, others are true;

The characters "" are false, others are true;

The beginning of the Python operator _ life if only as the initial

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.