Precedence in Python when operators are used

Source: Internet
Author: User

This article mainly introduces the use of Python operators in the priority, is the basic knowledge of Python learning, the need for friends can refer to the

Operator precedence to determine the grouping in an expression of a condition. This affects how an expression is evaluated. Some operators have precedence over others; For example, the multiplication operator has a higher precedence than the addition operation.

For example x=7 + 3* 2; here, X is assigned 13, not 20, because the operator * has a higher precedence than +, so it is multiplied by 3 * 2 First, then plus 7.

Here, the highest precedence operators appear above the table, those with the lowest display at the bottom. In an expression, the higher precedence operator is evaluated first.

For example:

Try the following example to understand the operator precedence that the Python programming language can choose from:


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #!/usr/bin/python A = b = ten c = d = 5 E = 0 E = (A + b) * C/D # (m)/5 print "Value of (A + b) * C/ D is ", e E = ((A + b) * c)/D # (m)/5 print" Value of ((A + b) * C)/d is ", e E = (A + b) * (C/D); # (15/5) print "Value of (A + B) * (C/D) is", e E = a + (b * c)/D; # + (150/5) print "Value of A + (b * c)/d is", E

When the above program is executed, it produces the following results:


1 2 3 4 Value of (A + B) * C/D is-value of ((A + b) * C)/d is-value of (A + B) * (C/D) is value of a + (b * c)/d is 50

        Note < : More Wonderful tutorials please focus on the triple Programming

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.