Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Syntax: Subtraction

check

Subtracting one number from another with the Python subtraction operator.


Subtracting Integers in Python

The next operator you will be learning about is the subtraction operator. When you want to subtract a numeric value from another numeric value, the subtraction operator is the one to use. In Python, as in many other languages, the - symbol represents the subtraction operator.

Typical syntax usage for the subtraction operator are as follows:

  1. When subtracting one number from another:

       difference = 60 - 22
    
  2. When subtracting a variable value from a number:

      val = 32
      difference = val - 5
    
  3. When subtracting one variable value from another:

      val1 = 50
      val2 = 25
      difference = val1 - val2
    

Practice: Subtracting Integers

Based on the above information, please complete the following steps:

  1. Define a variable named days_until_end_of_month and, using the subtraction operator, assign it the total number of days in the current month minus the current day of the month.

  2. Print the value contained in the days_until_end_of_month 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 number of days remaining in the current month (on the last day, it should be 0):

    > 26
    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.
      days_until_end_of_month = 31 - 5
      print(days_until_end_of_month)
    

Like the addition operator, the subtraction operator can also be used multiple times in a single statement:

  difference = 55 - 5 - 10 - 15
> 25

Based on the above information, please complete the following steps:

  1. Define a variable named hours_per_day and assign it number of hours in a day.

  2. Define a variable named sleep_hours and assign it the number of hours you sleep each day.

  3. Define a variable named work_hours and assign it the number of hours you work each day.

  4. Define a variable named hours_left and assign it the difference that remains after subtracting sleep_hours and work_hours from hours_per_day.

  5. Print the value contained in the hours_left 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 hours left once work and sleep hours are subtracted:

    > 8
    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.
      hours_per_day = 24
      sleep_hours = 8
      work_hours = 8
      hours_left = hours_per_day - sleep_hours - work_hours
      print(hours_left)
    

Once you have completed the above steps and you are getting the correct output in the interpreter. You are ready to move on to the next exercise!