6. Python Basic syntax-operator

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

Pythonoperator

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
Python arithmetic operators

The Python arithmetic operators are the same as those in high-level language C, C + +.

The following hypothetical variables: a=10,b=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
// Rounding-Returns the integer portion of the quotient ( rounded down ) 9//2 output result 4, 9.0//2.0 output 4.0
Python comparison operators

The Python arithmetic operators are basically the same as those for high-level language C, C + +, but have more <> 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. (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 and the binary format is as follows:

 a = 0011 1100b  = 0000 1101-----------------a  &b = 0000 1100a  |b = 0011 1101a  ^b = 0011 0001~a = 1100 0011 
operator description instance
& Bitwise-AND Operator: two values that participate in the operation, if two corresponding bits are 1, the The result of the bit is 1, otherwise 0 (A & B) output 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 61, Binary interpretation: 0011 1101
^ bitwise XOR operator: When two corresponding binary differences, the result is 1 (a ^ b) Output 49, Binary interpretation: 0011 0001
~ bitwise inverse operator: each bits of the data is reversed, which turns 1 to 0 and 0 to 1. ~x similar to -x-1 (~a) output-61, Binary interpretation: 1100 0011, in the complement form of a signed binary number.
<< left move operator: Each binary of the operand all shifts left several bits, by << to the right of the number specified the number of bits moved, high drop, low fill 0. A << 2 output result 240, binary interpretation: 1111 0000
>> Right-shift operator: ">>" left The operands of the edge are all shifted right by several bits, >> the right number specifies the number of bits to move a >> 2 output result 15, binary interpretation: 0000 1111
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 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, 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.

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 and OR logical operators

6, Python basic syntax-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.