4.1 Arithmetic operators
4.1a What are arithmetic operators?
Arithmetic operators are symbols in programming languages (like Python) that perform mathematical operations on numbers. They are used to carry out basic math functions like adding, subtracting, multiplying, and dividing. Think of them like the buttons on a calculator that help you do math!
4.1b Why do we need arithmetic operators?
Arithmetic operators are essential in programming for several reasons:
-
Performing Calculations - They allow you to do mathematical calculations, which are necessary for many tasks, such as calculating totals, averages, and other important values.
-
Building Logic - Many programming tasks involve comparing numbers, checking conditions, and making decisions based on calculations. Arithmetic operators help build the logic needed for these tasks.
-
Manipulating Data - They enable you to manipulate data, like changing the value of variables, creating new values from existing ones, and transforming data into different formats.
-
Solving Problems - Whether you’re coding a game, a scientific application, or a simple program, solving problems often involves arithmetic. For example, figuring out how much money you have left after buying something, or calculating how many pixels to move a character in a game.
4.1c Main arithmetic operators.
The main arithmetic operators that we will use in this course are:
|
|
=
Equals
This is called the assignment operator. It is used to assign a value to a variable.
Example 4.1.1
# It means the variable x now holds the value 5.
+
Addition
This operator adds two numbers together.
Example 4.1.2
Example 4.1.2 - Output
5
-
Subtraction
This operator subtracts one number from another.
Example 4.1.3
Example 4.1.3 - Output
2
*
Multiplication
This operator multiplies two numbers together.
Example 4.1.4
Example 4.1.4 - Output
8
/
Division
This operator divides one number by another.
Example 4.1.5
Example 4.1.5 - Output
5.0
Note that the result is a decimal (even if it’s a whole number).
//
Floor Division
This operator divides one number by another and then rounds down to the nearest whole number.
Example 4.1.6
Example 4.1.6 - Output3
(because 10 divided by 3 is 3.333..., and floor division rounds it down to 3).
%
Modulus
This operator gives the remainder when one number is divided by another.
Example 4.1.7
Example 4.1.7 - Output
1
(because 10 divided by 3 is 3 with a remainder of 1).
**
Power of
This operator raises one number to the power of another number.
Example 4.1.8
Example 4.1.8 - Output8 # because 2 raised to the power of 3 means: # 2 x 2 x 2, which is 8.
4.1d Interpret a program snippet that includes arithmetic operations.
Example 4.1.9 - The subtraction operator (-
) is used to find out how many apples are left after eating some. This is a simple way to see how arithmetic operators help us solve everyday problems through programming!
# Let's say you have 10 apples and you eat 3
apples = 10
eaten = 3
remaining_apples = apples - eaten # Subtract the eaten apples from the total
print("Remaining apples:", remaining_apples)
Example 4.1.9 - Output
Remaining apples: 7
4.1e Develop a program using arithmetic operations.
Exercise 4.1.1 - Create a Python program that calculates the total cost of a shopping trip.
Let's say that you went to the store and bought the following items:
- 3 candy bars at €1.50 each
- 2 bags of chips at €2.75 each
- 4 sodas at €1.25 each
- You have a €20 note to pay for these items.
Your program should:
- Calculate the total cost of all the items.
- Calculate how much money you will have left after paying with the €20 note.
- Calculate how many items you bought in total.
- Print out the total cost, the remaining money, and the total number of items.
Exercise 4.1.1 - Model Answer - Make sure to work out the exercise before checking the model answer.