A Byte of Python notes (3), bytepython

Source: Internet
Author: User
Tags integer division

A Byte of Python notes (3), bytepython

Chapter 3 operators and expressions

Most statements (logical rows) contain expressions. Example: 2 + 3. An expression can be divided into operators and operands.

 

Operator

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 to get 'lala '.

**

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 Division

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 and 1)

2 <2 get 8. -- 2 expressed in BITs as 10

>

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 to 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, False is returned.

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 y 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;

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

 

Operator priority

The following table lists the operator priorities of Python, from the lowest (loose combination) to the highest (most closely combined ). This means that in an expression, Python will first calculate the following operators in the table, and then calculate the operators listed in the upper part of the table.

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

(Experession ,...)

Bind or display tuples

[Expression,...]

List display

{Key: datum ,...}

Dictionary display

'Expression ,...'

String Conversion

 

Calculation Order: The operator priority table determines which operator is computed before another operator. You can use parentheses to change the order of calculation.

Combination rules: Usually from left to right, and the value assignment operator from right to left.

 

Expression

Calculates the area and perimeter of a rectangle.

# !/usr/bin/python# Filename: expression.pylength = 5breadth = 2area = length * breadthprint 'Area is', areaprint 'Perimeter is %d' % (2*(length + breadth))

Output

 

Note the two print modes of print.

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.