Python operator and python operation

Source: Internet
Author: User

Python operator and python operation
What is an operator?

For a simple answer, expressions 4 + 5 equals 9. Here 4 and 5 are called operands and + are called operators. Python supports the following types of operators.

  • Arithmetic Operators

  • Comparison (relational) Operator

  • Value assignment operator

  • Logical operators

  • Bitwise operators

  • Member Operator

  • Identifier Operator

Let's look at all operators one by one.

Python arithmetic operators:

Assume that variable a holds 10 and variable B holds 20, then:

 

Operator Descriptor Example
+ Addition-added value to both sides of the operator A + B = 30
- Subtraction minus the right operand of the left operand A-B =-10
* Multiplication-values on both sides of the multiplication Operator A * B = 200
/ Division-by the right operand divided by the left operand B/a = 2
% Modulo-divide the right operand and the remainder return by the left operand B % a = 0
** Exponential-execute the operation exponent (Power) Calculation A ** B = 10 power 20
// The division of the floor division-operand. The result is the operator that removes the digits after the decimal point. 9 // 2 = 4 and 9.0 // 2.0 = 4.0
Python comparison operator:

Assume that variable a holds 10 and variable B holds 20, then:

 

Operator Description Example
= Check whether the values of the two operands are equal. If yes, the condition changes to true. (A = B) not true.
! = Check whether the values of the two operands are equal. If the values are not equal, the condition becomes true. (! = B) is true.
<> Check whether the values of the two operands are equal. If the values are not equal, the condition becomes true. (A <> B) is true. This is similar! = Operator
> Check whether the value of the left operand is greater than the value of the right operand. If yes, the condition is true. (A> B) not true.
< Check whether the value of the left operand is smaller than the value of the right operand. If yes, the condition is true. (A <B) is true.
> = Check whether the value of the left operand is greater than or equal to the value of the right operand. If yes, the condition is true. (A> = B) not true.
<= Check whether the value of the left operand is smaller than or equal to the value of the right operand. If yes, the condition is true. (A <= B) is true.
Python assignment operator:

Assume that the variable holds 10 and the variable B holds 20, then:

Operator Description Example
= A simple value assignment operator that returns the value from the left side of the right side of the operand. C = a + B: Convert the specified value a + B to c
+ = Addition AND value assignment operator, which adds the left operand of the right operand AND the result to the left operand C + = a is equivalent to c = c +
-= Subtraction AND value assignment operator, which removes the right operand from the left operand AND assigns the result to the left operand C-= a is equivalent to c = c-
* = Multiplication AND value assignment operator, which is multiplied by the right AND left operands, AND the result is assigned to the left operand C * = a is equivalent to c = c *
/= Division AND value assignment operator, which assigns the left operand AND the correct operand AND the result to the left operand C/= a is equivalent to = c/
% = Modulus AND value assignment operator, which requires the modulus of two operands AND the left operand of the Allocation Result C % = a is equivalent to c = c %
** = Returns the exponential AND value assignment operator, executes the exponential (Power) calculation operator, AND assigns a value to the left operand. C ** = a is equivalent to c = c **
// = Divide the floor and assign a value to it. perform the operations and assign the value to the left operand. C // = a is equivalent to c = c //

The following example shows all the value assignment operators available in the Python programming language:

#!/usr/bin/pythona =21b =10c =0c = a + bprint"Line 1 - Value of c is ", cc += aprint"Line 2 - Value of c is ", c c *= aprint"Line 3 - Value of c is ", c c /= a print"Line 4 - Value of c is ", c c  =2c %= aprint"Line 5 - Value of c is ", cc **= aprint"Line 6 - Value of c is ", cc //= aprint"Line 7 - Value of c is ", c

When the above program is executed, it will produce the following results:

Line 1 - Value of c is 31Line 2 - Value of c is 52Line 3 - Value of c is 1092Line 4 - Value of c is 52Line 5 - Value of c is 2Line 6 - Value of c is 2097152Line 7 - Value of c is 99864

 

Python bit operator:

Bitwise operators act on bitwise AND bitwise operation execution bits. Assume that if a = 60; and B = 13; they are now in binary format, they will be as follows:

A = 0011 1100

B = 0000 1101

-----------------

A & B = 0000 1100

A/B = 0011 1101

A ^ B = 0011 0001

~ A = 1100 0011

Http://www.cnblogs.com/roucheng/

Python supports lower-bit Operators

Operator Description Example
& Binary and copy operations are performed. If the result exists in two operands. (A & B) = 12, that is, 0000 1100
| Binary or copy operations on a bit if it exists in an operand. (A | B) = 61 (0011 1101)
^ A copy of the binary XOR operator if it is set in one operand rather than two bits. (A ^ B) = 49 means 0011 0001
~ The complement operator of the binary is unary and has the effect of "flip. (~ A) =-61 means that 1100 0011 is in the form of a 2's complement due to the number of signed binary numbers.
< Binary shift operator. The value of the left operand shifts left by the number of digits specified by the right operand. A <2 = 240 means 1111 0000
> Binary shift operator. The value of the left operand is shifted to the right by the number of digits specified by the right operand. A> 2 = 15, that is, 0000 1111
Python logical operators:

Python supports the following logical operators. Assume that variable a holds 10 and variable B holds 20:

Operator Description Example
And The so-called logic and operators. If both operands are true, the condition is true. (A and B) is true.
Or The so-called logical OR operator. If both operands are non-zero, then the condition becomes true. (A or B) is true.
Not The so-called logical non-operator. Used to reverse the logic status of an operand. If a condition is true, the logical non-operator returns false. Not (a and B) is false.
Python member operators:

In addition to the operators discussed earlier, the Python member operators test the membership in a sequence, such as strings, lists, or tuples. Two member operators are described as follows:

Operator Description Example
In The calculation result is true. If it finds the variable in the specified sequence, otherwise, false is returned. In y, a 1 is generated here, if x is a member of sequence y.
Not in The calculation result is true. If it is not found in the specified variable order, otherwise it is false. If x is not in y, the result is not 1 if x is not a member of y.

Try the following example to understand that all Python programming languages provide member operators:

#!/usr/bin/pythona =10b =20list =[1,2,3,4,5];if( a in list ):print"Line 1 - a is available in the given list"else:print"Line 1 - a is not available in the given list"if( b notin list ):print"Line 2 - b is not available in the given list"else:print"Line 2 - b is available in the given list"a =2if( a in list ):print"Line 3 - a is available in the given list"else:print"Line 3 - a is not available in the given list"

When the above program is executed, it will produce the following results:

Line 1 - a is not available in the given listLine 2 - b is not available in the given listLine 3 - a is available in the given list
Python identifier OPERATOR:

The identifier compares the memory locations of two objects. The two operators are described as follows:

 

Operator Description Example
Is The calculation result is true. If the variables on both sides of the operator point to the same object, otherwise, the value is false. X is y, and here the result is 1, if the value of id (x) is id (y ).
Is not The calculation result is false. If the variable operators on both sides point to the same object, the value is true. X is not y. Here the result is not 1, when id (x) is not equal to id (y ).

The following example shows the identity operators provided by all Python programming languages:

#!/usr/bin/pythona =20b =20if( a is b ):print"Line 1 - a and b have same identity"else:print"Line 1 - a and b do not have same identity"if( id(a)== id(b)):print"Line 2 - a and b have same identity"else:print"Line 2 - a and b do not have same identity"b =30if( a is b ):print"Line 3 - a and b have same identity"else:print"Line 3 - a and b do not have same identity"if( a isnot b ):print"Line 4 - a and b do not have same identity"else:print"Line 4 - a and b have same identity"

When the above program is executed, it will produce the following results:

Line 1 - a and b have same identityLine 2 - a and b have same identityLine 3 - a and b do not have same identityLine 4 - a and b do not have same identity



Python operator priority

The following table lists all operators from highest priority to lowest priority.

 

Operator Description
** Power (raised to the index)
~ +- Complement, one-dollar plus sign and minus sign (the last two + @ and-@ of the method name -@)
*/% // Multiplication, division, modulo and floor Division
+- Addition and subtraction
>>< < Left and right shift by bit
& AND'
^ | By bit or ''and period or'
<=<>>= Comparison Operators
<>==! = Equality Operators
= % =/= // =-= + = * = ** = Value assignment operator
Is not Identifier Operator
In not in Member Operators
Not or and Logical operators


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.