Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Practice: Combining and Grouping Operators

check

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:

  1. * (multiplication), // (floor division), and % (modulo)
  2. + (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

  1. Define a variable named months_with_28_days with the value 1.
  2. Define a variable named months_with_30_days with the value 4.
  3. Define a variable named months_with_31_days with the value 7.
  4. Define a variable named days_per_year and, using the variables above and arithmetic operators, assign it the total number of days in a year.
  5. Print the value contained in the days_per_year variable.
HINT*: Click here for a hint
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.

You should see the total number of days in a year:

> 365
SOLUTION*: Click here for an example solution
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
  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)
  1. Define a variable named paid_hours and, 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 from 6 (6am) to 17 (5pm), with two (unpaid) hour-long lunch breaks at 10 and 13.
    • If you are not familiar with telling time on the 24-hour clock, visit this link here.
  2. Print the value contained in the paid_hours variable.
HINT*: Click here for a hint
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.

You should see the total paid hours worked:

> 9
SOLUTION*: Click here for an example solution
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.

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!