Concise Python Tutorials

Source: Internet
Author: User
Tags bitwise expression integer integer division

Brief introduction

Most of the statements you write (logical rows) contain an expression. A simple expression example is 2 + 3. An expression can be decomposed into operators and operands.

The function of an operator is to accomplish something that is represented by symbols such as + or other specific keywords. Operators require data for operations, and such data is called operands. In this example, 2 and 3 are operands.

Operator

We'll take a quick look at the operators and their usage:

Skills

You can interactively use an interpreter to compute the expression given in the example. For example, to test expression 2 + 3, use an interactive Python interpreter with a prompt:

>>> 2 + 3
5
>>> 3 * 5
15
>>>

Table 5.1 operators and their 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.

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.