Python expression operator "Learn Python must know"

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

operator Description Example
Yield x Generator function Send protocol
Lambda args:expression Generating anonymous functions
X if y else Z Ternary selection Expressions (C-series Some python also have) >>> True if 1>0 else falsetrue
The following excerpt from the Rookie tutorial: http://www.runoob.com/python/python-operators.html People do is still good, but this does not write Python arithmetic operators

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

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

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.
<> Not equal-compares two objects for unequal (a <> B) returns True. This operator is similar to! =.
> 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.

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

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

Python logical operators
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 non-0, 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

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.

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, 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

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

Python expression operator "Learn Python must know"

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.