Python learning notes (3): operators and expressions

Source: Internet
Author: User
1. Operators
Operator Name Description Example
+ Add Add two objects 3 + 5 get 8. "A" + "B" get "AB ".
- Subtraction Returns a negative number or a number minus another number. -5.2 returns a negative number. 50-24 get 26.
* Multiplication Returns a string that is repeated several times. 2*3 get 6. "La" * 3 get "lalala ".
** Power Returns the Power Y of X. 3 ** 4 get 81 (3*3*3*3 ).
/ Division X divided by Y 4/3 to get 1 (integer division to get the integer result ). 4.0/3 or 4/3.0 get 1.3333333333333333
// Integer Remainder Returns the integer part of the operator. 4 // 3.0 get 1.0
% Modulo Returns the remainder of the Division. 8/3 get 2. -25.5% 2.25 get 1.5
< Move left Shifts a certain number of BITs to the left (each number is expressed as a bit or binary number in the memory, that is, 0 or 1) 2 <2 get 8. -- 2 indicates 10 in bits
> Right Shift Shifts a certain number of BITs to the right. 11> 1 to get 5. -- 11 is expressed as 1011 in bits. After moving 1 bit to the right, 101 is obtained, that is, 5 in decimal format.
& Bitwise AND Bitwise AND 5 & 3 get 1
| By bit or Number by bit or 5 | 3 get 7
^ Bitwise OR Bitwise OR 5 ^ 3 get 6
~ Flip by bit The bitwise flip of X is-(x + 1) ~ 5 get-6
< Less Returns whether X is less than Y. All comparison operators return 1 to indicate true, and 0 to indicate false. This is equivalent to the special variables true and false. Note: These variable names are capitalized. 5 <3 returns 0 (false) and 3 <5 returns 1 (true ). Comparison can be connected at any time: 3 <5 <7 returns true.
> Greater Returns whether X is greater than Y. 5> 3 return true. If both operands are numbers, they are first converted to a common type. Otherwise, it always returns false.
<= Less than or equal Returns whether X is less than or equal to y. X = 3; y = 6; x <= y returns true.
> = Greater than or equal Returns whether X is greater than or equal to y. X = 4; y = 3; x> = y returns true.
= Equal Equal comparison object X = 2; y = 2; X = y returns true. X = "str"; y = "str"; X = y returns false. X = "str"; y = "str"; X = y returns true.
! = Not equal Compare whether two objects are not equal X = 2; y = 3; X! = Y returns true.
Not Boolean "Non" If X is true, false is returned. Returns true if X is false. X = true; not X returns false.
And Boolean and If X is false, X and Y returns false. Otherwise, it returns the calculated value of Y. X = false; y = true; X and Y, returns false because X is false. Python does not calculate y here, because it knows that the value of this expression must be false (because X is false ). This phenomenon is called short circuit computation.
Or Boolean OR If X is true, X or Y returns true, otherwise it returns the calculated value of Y. X = true; y = false; X or Y returns true. Short circuit calculation is also applicable here.
2. Operator priority

The following table operators are from the lowest to the highest. Python first calculates the bottom operators in the table, and then calculates the top operators in the table. In fact, we strongly recommend that you use parentheses to group operators and operands so as to clearly point out the order of operations and achieve easy read.

Calculation order: it is determined by the operator priority by default. If you want to change their order, use parentheses, for example: (2 + 3) * 4

Combination rules: operators are combined from left to right (operators of the same level). For example, 2 + 3 + 4 is calculated as (2 + 3) + 4.

The value assignment operator is combined by the right to the left, for example, a = B = C is processed as a = (B = C)

Operator Description
Lambda Lambda expressions
Or Boolean OR
And Boolean and
Not x Boolean "Non"
In, not in Member Testing
Is, is not Identity Test
<, <=,>, >= ,! =, = Comparison
| By bit or
^ Bitwise OR
& Bitwise AND
<,> Shift
+ ,- Addition and subtraction
*,/, % Multiplication, division, and remainder
+ X,-x Plus or minus sign
~ X Flip by bit
** Index
X. Attribute Attribute reference
X [Index] Subscript
X [index: Index] Addressing segment
F (arguments ...) Function call
(Expression ,...) Bind or display tuples
[Expression,...] List display
{Key: datum ,...} Dictionary display
'Expression ,...' String Conversion
3. Expression
# This is a simple expression example code # filename: expression. pylength = 5 breadth = 2 area = length * breadthprint ("area is", Area) print ("Perimeter is", 2 * (Length + breadth ))

The output result is as follows:

Area is 10

Perimeter is 14

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.