An introduction to operators and expressions in Python

Source: Internet
Author: User
Tags bitwise comparison integer division in python

1. Operator and its usage

operator name Description Example
+ Add Add two objects 3 + 5 gets 8. ' A ' + ' B ' gets ' ab '.
- Reducing Get negative numbers or a number minus another number -5.2 Gets a negative number. 50-24 get 26.
* By Multiply two numbers or return a string that is repeated several times 2 * 3 get 6. ' La ' * 3 gets ' Lalala '.
** Power

Returns the Y-power of X

3 * * 4 gets 81 (ie 3 * 3 * 3 * 3)
/ Except x divided by Y 4/3 gets 1 (the integer division gets the integer result). 4.0/3 or 4/3.0 get 1.3333333333333333
// Take the Divide The integer part of the return quotient 4//3.0 gets 1.0
% Take the mold Returns the remainder of a division 8%3 got 2. -25.5%2.25 got 1.5.
<< Move left Shifts a number of bits to the left a certain number (each number in memory is represented as a bit or binary number, i.e. 0 and 1) 2 << 2 get 8. --2 is expressed as 10 by bit
>> Move right Move a number of bits to the right by a certain number One >> 1 get 5. --11 is expressed as 1011 by bit, 1 bits to the right and 101, or 5 in decimal.
& Bitwise AND The bitwise of number and 5 & 3 Get 1.
| by bit or Number of bitwise OR 5 | 3 Get 7.
^ Per-bitwise XOR OR The bitwise XOR of the number or 5 ^ 3 Get 6
~ Flip by bit The bit flip of X is-(x+1) ~5 got 6.
< Less than Returns whether x is less than Y. All comparison operators return 1 to represent true, and 0 to represent false. This is equivalent to the special variable true and false respectively. Note that these variable names are capitalized. 5 < 3 returns 0 (that is, false) and 3 < 5 returns 1 (that is, true). Comparisons can be any connection: 3 < 5 < 7 returns TRUE.
> Greater than Returns whether x is greater than Y 5 > 3 returns TRUE. If two operands are numbers, they are first converted to a common type. Otherwise, it always returns false.
<= Less than or equal to Returns whether x is less than or equal to Y x = 3; y = 6; X <= y returns True.
>= Greater than or equal to Returns whether x is greater than or equal to Y x = 4; y = 3; X >= y returns True.
== Equals Compare objects for Equality 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 to Compare two objects for unequal x = 2; y = 3; X!= y returns True.
Not Boolean "Non" Returns False if X is true. If x is False, it returns true. x = True; Not Y returns false.
and Boolean "and" If x is False,x and Y returns false, it returns the calculated value of Y. x = False; y = True; X and Y, which returns false because X is false. Here, Python does not compute y because it knows that the value of the expression must be false (because X is false). This phenomenon is called short-circuit calculation.
Or Boolean "or" If x is true, it returns true, otherwise it returns the calculated value of Y. x = True; y = False; X or Y returns true. Short-circuit calculations are also applicable here.

2. Operator precedence (from low to high)

operator Description
Lambda Lambda expression
Or Boolean "or"
and Boolean "and"
Not X Boolean "Non"
In,not in Member Test
Is,is not Identity test
<,<=,>,>=,!=,== Comparison
| by bit or
^ Per-bitwise XOR OR
& Bitwise AND
<<,>> Shift
+,- Addition and subtraction
*,/,% Multiplication, division and remainder
+x,-x PLUS sign
~x Flip by bit
** Index
X.attribute Attribute reference
X[index] Subscript
X[index:index] Addressing segments
F (Arguments ...) Function call
(Experession,...) Binding or tuple display
[Expression,...] List display
{Key:datum,...} Dictionary display
' expression,... ' string conversion
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.