The Python operator

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

Arithmetic operators

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

Instance:

#!/usr/bin/python3a = 21b = 10c = 0c = a + bprint (the value of "1-c is:", c) c = a-bprint ("2-c value is:", c) c = A * Bprint ("3-c The value 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-c value As: ", C) A = 10b = 5c = a//b Print (" 7-c value: ", 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/python3a = 21b = 10c = 0if (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 is greater than or equal b ") if (a > b):   print (" 4-a greater than B ") else:   print (" 4-a is less than or equal to B ") # Modify the values of variables A and b = 5;b = 20;if (a <= b):   print ("5-a is less than or equal to B") else:   print ("5-a greater than  B") if (b >= a):   print ("6-b is greater than or equal to B") else:   prin T ("6-b less than B")

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 B

Python assignment operator

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

Operator Description Instance = Simple assignment operator C = A + B assigns the result of the operation of A + B to the 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 equivalent to C = c * * a//= take divisible assignment operator c//= a equivalent to C = c//a

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

#!/usr/bin/python3a = 21b = 10c = 0c = a + bprint (the value of "1-c is:", c) c + = Aprint ("the value of 2-c is:", c) C *= aprint ("the value of 3-c is:", 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 ("the value of 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 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:

The variable a in the following table is 60,b 13.

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, which turns 1 to 0 and 0 to 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/python3a = $ =            0011 1100 b =            # = 0000 1101 c = 0c = 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 true, 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 result of the above example Output:

#!/usr/bin/python3a = 10b = 20if (a and b):   print ("1-variables A and B are True") else:   print ("1-variables A and B have a not true" 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 = 0i  F (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- Both 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-variables A and B are all F alse, 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
In 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/python3a = 10b = 20list = [1, 2, 3, 4, 5];if (a in list):   print ("1-variable A in list of given Lists") else:   pri NT ("1-variable A does not exist in the given List") if (b not in list):   print ("2-variable B does not exist in the given List") else:   print ("2-variable b" in the given List ") # Modify the value of the variable a = 2if (a in list):   print (" 3-variable A in the list of given lists ") else:   print (" 3-variable A is not in the given list Lis T Medium ")

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

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, if ID (x) equals ID (y), is returns result 1
is not Is does not determine whether two identifiers are referenced from different objects X is not y if the ID (x) does not equal the ID (y). is not returning result 1

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

#!/usr/bin/python3a = 20b = 20if (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):   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 Identity") else:   p Rint ("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

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/python3a = 20b = 10c = 15d = 5e = 0e = (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

The Python operator

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.