
Syntax: Addition
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:
-
When adding two numbers:
sum = 60 + 35 -
When adding a variable to a number:
val = 32 sum = val + 5 -
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:
-
Define a variable named
days_in_april_and_mayand, using the addition operator, assign it the number of days in April plus the number of days in May. -
Print the value contained in the
days_in_april_and_mayvariable.HINT*: Click here for a hint
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
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:
-
Define a variable named
days_in_januaryand assign it number of days in the month of January. -
Define a variable named
days_in_februaryand assign it number of days in the month of February. -
Define a variable named
days_in_marchand assign it number of days in the month of March. -
Define a variable named
days_in_january_to_marchand, using the addition operator and the previous three variables, assign it the number of days in January through March. -
Print the value contained in the
days_in_january_to_marchvariable.HINT*: Click here for a hint
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
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!