Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Syntax: Addition

check

Add two numbers together with the Python addition operator.


How to Add Integers in Python

There are numerous operators in Python which are used for performing math calculations. When you want to add two numeric values together, the operator that you want to use is the addition operator. In Python, as in many other languages, the + symbol represents the addition operator.

Typical syntax usage for the addition operator are as follows:

  1. When adding two numbers:

      sum = 60 + 35
    
  2. When adding a variable to a number:

      val = 32
      sum = val + 5
    
  3. When adding two variable values:

      val1 = 5
      val2 = 10
      sum = val1 + val2
    

Practice: Adding Integers

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

  1. Define a variable named days_in_april_and_may and, using the addition operator, assign it the number of days in April plus the number of days in May.

  2. Print the value contained in the days_in_april_and_may 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 from the start of April to the end of May:

    > 61
    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_in_april_and_may = 30 + 31
      print(days_in_april_and_may)
    

The addition operator can also be used multiple times in a single statement:

  sum = 5 + 5 + 10 + 15

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

  1. Define a variable named days_in_january and assign it number of days in the month of January.

  2. Define a variable named days_in_february and assign it number of days in the month of February.

  3. Define a variable named days_in_march and assign it number of days in the month of March.

  4. Define a variable named days_in_january_to_march and, using the addition operator and the previous three variables, assign it the number of days in January through March.

  5. Print the value contained in the days_in_january_to_march 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 from the start of January to the end of March:

    > 90
    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_in_january = 31
      days_in_february = 28
      days_in_march = 31
      days_in_january_to_march = days_in_january + days_in_february + days_in_march
      print(days_in_january_to_march)
    

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!