Explain the precedence of operators when using Python

Source: Internet
Author: User
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 + 2; Here, X is assigned a value of 13 instead of 20 because the operator * has a higher priority than +, so it is first multiplied by 3 * 2 and then added 7.

Here, the highest precedence operator appears above the table, with the lowest displayed 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:

#!/usr/bin/pythona = 20b = 10c = 15d = 5e = 0e = (A + b) * C/D    # (+ *)/5print "Value of (A + B) * C/D is", EE = ((a + b) * c)/d   # ($ *)/5print "Value of ((A + b) * C)/d is", EE = (A + b) * (C/D);  # (15/5) print "Value of (A + B) * (C/D) is", EE = a + (b * c)/D;   # + (150/5) print "Value of A + (b * c)/d is", E

When executing the above program, it produces the following result:

Value of (A + B) * C/D is 90Value of ((A + b) * C)/d are 90Value of (A + B) * (C/D) is 90Value of A + (b * c)/D-is 50
  • 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.