Python basic operators

Source: Internet
Author: User

Python basic operator What is an operator?

A simple answer can be used with an expression 4 + 5 equals 9, where 4 and 5 are called operands, + is called the operator. The Python language supports several types of operators.

• Arithmetic operators

• Compare (that is, relational) operators

• Assignment operators

• logical operators

• Bitwise operators

• Member operators

• Identity operator

Let's take a look at all the operators individually.

Python Arithmetic operators:

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

[View Example]

operator Descriptor Example
+ Addition-adds value to both sides of the operator A + b = 30
- Subtract-subtracts the operand from the right side of the left operand A-B =-10
* Multiply-multiplies the values on both sides of the operator A * b = 200
/ Divide-by right operand divided by left operand b/a = 2
% Modulo-by the right operand and remainder return divided by the left operand B% A = 0
** Exponent-Performs a calculation of the operation Exponent (power) A**b = Power of 10 20
// Floor except-The division of the operand, where the result is the quotient of the number of digits after the decimal point is removed. 9//2 = 4 and 9.0//2.0 = 4.0
Comparison operators for Python:

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

[View Example]

operator Description Example
== Checks whether the values of the two operands are equal, and if so, the condition becomes true. (A = = B) is not true.
!= Checks whether the values of the two operands are equal, and if the values are not equal, the condition becomes true. (A! = B) is true.
<> Checks whether the values of the two operands are equal, and if the values are not equal, the condition becomes true. (a <> B) is true. This is similar to! = operator
> Checks if the value of the left operand is greater than the value of the right operand, and if so, the condition is true. (A > B) is not true.
< Checks if the value of the left operand is less than the value of the right operand, and if so, the condition is true. (A < b) is true.
>= Checks whether the value of the left operand is greater than or equal to the value of the right operand, and if so, the condition is true. (a >= B) is not true.
<= Checks whether the value of the left operand is less than or equal to the value of the right operand, and if so, the condition is true. (a <= B) is true.
Python Assignment operators:

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

Example

operator Description Example
= Simple assignment operator, assignment from left operand of right operand c = A + B will specify the value A + B to c
+= The addition and assignment operator, which increases the left operand of the right operand and assigns the result to the left operand c + = a equals c = C + A
-= Minus and assignment operator, which subtracts the right operand from the left operand and assigns the result to the left operand c-= a equals c = c-a
*= Multiplication and assignment operator, multiplied by the right operand and left operand, and assigns the result to the left operand C *= a equals c = c * A
/= The Division and assignment operator, which puts the left operand with the correct operand and assigns the result to the left operand C/= a equals = C/A
%= Modulus and assignment operator, which needs to use the modulus of two operands and assign the result to the left-hand operand C%= A is equivalent to C = c% A
**= Exponential and assignment operators, performing exponential (power) calculation operators and assigning to left operands C **= a equals c = c * * A
//= The floor is divided and assigned a value that performs the floor in addition to the operation and assignment to the left operand C//= a equals c = c//A
Python bitwise operators:

Bitwise operators Act on bit and bit operation execution bits. Suppose that if a = 60; and B = 13; now in binary format they will look like this:

A = 0011 1100

b = 0000 1101

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

a&b = 0000 1100

a|b = 0011 1101

A^b = 0011 0001

~a = 1100 0011

The Python language supports the lower operator

Example

operator Description Example
& Binary and copy operations have been done, as a result, if it exists in two operands. (A & B) = 12 that is 0000 1100
| A binary or copy operation has a bit, if it exists in an operand. (A | b) = 61 that is 0011 1101
^ A copy of the binary XOR operator, if it is set to an operand instead of two bits. (a ^ b) = 49 that is 0011 0001
~ The binary complement operator is unary and has the effect of a "flip" bit. (~a) = 61 is 1100 0011 in the form of a 2 complement due to the signed binary number.
<< Binary left shift operator. The left operand's value is shifted left by the number of digits specified by the right operand. A << 2 = 240 or 1111 0000
>> Binary right shift operator. The value of the left operand is moved to the right by the number of digits specified by the right operand. A >> 2 = 15 or 0000 1111
Python logical operators:

The following logical operators are supported in the Python language. Assume that variable a holds 10 and variable B holds 20:

Example

operator Description Example
and So-called logic and operators. If the two operands are true, then the condition is set. (A and B) is true.
Or The so-called logical OR operator. If there are two operands that are non-0 then the condition becomes true. (A or B) is true.
Not The so-called logical non-operator. The logical state used to invert the 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 operator, in a sequence, tests membership, such as strings, lists, or tuples. There are two member operators that are interpreted as follows:

Example

operator Description Example
Inch Evaluates to TRUE if it is specified in the order in which the variable is found, otherwise false. X in Y, where a 1 is generated, if X is a member of sequence Y.
Not in Evaluates to TRUE if it is not found in the specified variable order, otherwise false. X is not in Y, where the result is not 1 if X is not a member of sequence Y.
Python identity operator:

An identifier compares the memory location of two objects. The two operator identifiers are interpreted as follows:

[View Example]

operator Description Example
Is Evaluates to TRUE if the variable on either side of the operator points to the same object, false otherwise. X is y, where the result is 1 if the value of ID (x) is ID (y).
is not Evaluates to False if the variable operator on both sides points to the same object, otherwise true. X is not y, where the result is not 1, when ID (x) is not equal to ID (y).
Python operator Precedence

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

[View Example]

operator Description
** Power (raised to exponent)
~ + - Complement, unary Plus and minus (the last two of the method name [email protected] and-@)
* / % // Multiply, divide, take the mold and remove the floor
>> << Left, right-click Shift
& Bit ' and '
^ | Bitwise XOR ' or ' and periodic ' or '
<= < > >= Comparison operators
<> = = = Equality operators
= %= /= //= -= += *= **= Assignment operators
Is isn't Identity operator
In No in Member operators
Not OR and logical operators

Original: http://www.yiibai.com/python/python_basic_operators.htm

Tags: Python basic operator

This site is reproduced in addition to the article, are the original site or compiled
Welcome any form of reprint, but please be sure to indicate the source, respect for the work of others to create excellent examples of the tutorial

Python basic operators

Related Keywords:
Related Article

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.