T-Th 9:05 |
CS 1110: Introduction to Computing Using Python Spring 2015 |
|||||||||||||||||||||||||||||||||
Main
About: Announcements Staff Consultants Times & Places Calendar Materials: Texts Python Command Shell Terminology VideoNote Learning: Lectures Assignments Labs Assessment: Grading Exams Resources: CMS Piazza AEWs FAQ Python API Python Tutor Style Guide Academic Integrity |
Precedences of OperatorsYou know that multiplication * takes precedence over addition +. That is, the expression 5 + 4 * 3 is evaluated as if it were parenthesized like this: 5 + (4 * 3). Mathematics has conventions for precedences of operators in order to reduce the number of parentheses required in writing complex expressions. Some of these conventions are standard throughout the world, such as * over +. Others are not. Below is a table of precedences for Python operators. Refer to this table, or the one on p. 15 of the text Think Python, if you forget the precedences. Table of operator precedences
For example, the expression
can be easily rewritten as
because relational operators have precedence over logical Some GuidelinesKeep redundant parentheses to a minimum (but don't sacrifice clarity)Your goal in writing expressions should be to make them a clear and simple-looking as possible, and this can often be done by eliminating redundant expressions —but you can help the reader by putting more space around operators with less precedence. For example, the two assignment statements shown below have the same meaning, but the second is easier to read:
Keep redundant parentheses to a minimum. For example, they are not needed in a return statement within a function body (you will learn about the return statement later). The following two statements are equivalent, but the second is easier to read:
Use parentheses with
|
|||||||||||||||||||||||||||||||||
Course Material Authors: D. Gries, L. Lee, S. Marschner, C. Van Loan & W. White (over the years) |