
Practice: Combining and Grouping Operators
Python order-of-operations and grouping operators with parentheses.
Combining and Grouping Operators in Python
In Python, Like in mathematics, operators have a built-in order-of-operations:
3 + 5 * 2
> 13
In the example above, even though the addition operator comes first, multiplication has a higher operator precedence, so 5 times 2 happens first, then 3 is added to the result of that multiplication.
The operator precedence for the operators you have learned so far from strongest to weakest are as follows:
*(multiplication),//(floor division), and%(modulo)+(addition) and-(subtraction)
It is important to remember the operator precedence of the operators you are using when doing calculations to ensure you get the correct result!
Another feature of operator precedence is that also, like in math, parentheses can be used to change the order of operations, causing operations inside the parentheses to be evaluated first:
Note the two examples below:
(3 + 5) * 2
> 16
3 + 5 * 2
> 13
In the first example above, parentheses are added around the expression 3 + 5 which means that expression will occur first, and then the sum 8 will be multiplied by 2.
In the second example, no parentheses are used, so the expression 5 * 2 occurs first and the product 10 will have 3 added to it.
Parentheses are extremely useful for make sure your calculations will occur in the correct order by giving you enhanced control of the operator precedence.
Practice: Integer Arithmetic
- Define a variable named
months_with_28_dayswith the value1. - Define a variable named
months_with_30_dayswith the value4. - Define a variable named
months_with_31_dayswith the value7. - Define a variable named
days_per_yearand, using the variables above and arithmetic operators, assign it the total number of days in a year. - Print the value contained in the
days_per_yearvariable.
HINT*: Click here for a hint
You should see the total number of days in a year:
> 365
SOLUTION*: Click here for an example solution
months_with_28_days = 1
months_with_30_days = 4
months_with_31_days = 7
days_per_year = months_with_28_days * 28 + months_with_30_days * 30 + months_with_31_days * 31
print(days_per_year)
- Define a variable named
paid_hoursand, using hours-of-the-day (in 24-hour time) arithmetic operators and parentheses, calculate the number of hours someone should be paid for if they work from6(6am) to17(5pm), with two (unpaid) hour-long lunch breaks at10and13.- If you are not familiar with telling time on the 24-hour clock, visit this link here.
- Print the value contained in the
paid_hoursvariable.
HINT*: Click here for a hint
You should see the total paid hours worked:
> 9
SOLUTION*: Click here for an example solution
There are a couple ways to approach this. One is to add the individual paid time segments:
paid_hours = (10 - 6) + (13 - 11) + (17 - 14)
print(paid_hours)
Another possibility is to start with the total hours from start-to-finish (paid and unpaid), then subtract out the unpaid hours.
paid_hours = (17 - 6) - (11 - 10) - (14 - 13)
print(paid_hours)
Once you have completed the above, you are ready to move on to the next exercise!