Please enable JavaScript to use CodeHS

Basic Math in Python

By Ryan Hart

Being able to manipulate numbers and perform mathematical operations is an essential skill in Python. In fact, it’s almost unavoidable! Whether it’s converting ingredients from cups to tablespoons in a baking program, or having a counter in a video game that keeps track of a user’s high score, or placing graphic elements on a webpage, math is everywhere.

The nice thing about the math in Python is that we approach it exactly the same way we would if we were doing it by hand or with a calculator. For example, in the program below, we are performing very simple calculations. Run the program to see their results.

In the example above, try changing the calculations to something more complex. You can use any of the below mathematical operation symbols in Python.

+ for Addition
- for Subtraction
/ for Division
* for Multiplication
** for Exponent

Remember PEMDAS from Algebra?? The same rules apply to Python as well: the operations in a calculation will follow the order of Parentheses, then Exponents, then Multiplication and Division, then Addition and Subtraction. Test your PEMDAS skills in the following two questions:

  1. Incorrect Correct No Answer was selected Invalid Answer

    Choose the correct output of the code: print(2 + 8 - 3 * 2)

  2. Incorrect Correct No Answer was selected Invalid Answer

    Choose the correct output of the code: print(66 / 6 - (1 + 2))

Using variables with math

Now in programming, we often use both numbers and variables (that store numbers)! We perform the math operations the same way with variables as we do with numbers. Take a look at the following program that converts a user inputted temperature in Fahrenheit to Celsius. As you run the program, consider the questions below:

  • Why do we need to use variables in this program? Is it possible to write this same program without variables?
  • round(celsius) is a function that rounds the number stored in celsius to the nearest integer, storing the new value in celsius_rounded. Try deleting that function to see how it changes the program’s output (that is, make line 8 read: celsius_rounded = celsius).

The Modulus

There is one other mathematical operation in Python that is super useful:

% for Modulus

If you are new to programming, you probably haven’t seen this before. It can be a little tricky at first. Take a look at the example below and see if you can figure out what the modulus calculation is storing in the variables. Try inputing your own modulus calculations to explore further.

Were you able to figure it out??

Answer: the Modulus operator divides the two numbers, then keeps the remainder. Look again at the above calculations. Do you see it now?

15 / 6 = 2 with a remainder of 3
15 / 5 = 3 with a remainder of 0
15 / 4 = 3 with a remainder of 3
15 / 3 = 5 with a remainder of 0
15 / 2 = 7 with a remainder of 1

The remainder is what’s kept in the modulus calculation. This can be very useful in programs! For example, if you want to determine if a number is even, you can write following code: remainder = number % 2. If the variable remainder is equal to 0, then number is even (since there was no remainder)!

Now it’s your turn!

Imagine a scenario where you are the owner of a specialty bakery called Mini Muffies that sells cute little mini muffins in a decorative container with 3 muffins in it. You charge $5 per container. If at the end of the day there are still muffins remaining, you sell them to a local after school kids organization in little bags that contain 5 muffins each. These little bags are sold at a discounted rate of $2 per bag. Finally, any muffins that didn’t make it into a bag are set outside to feed neighborhood squirrels.

Let’s say today you make 543 mini muffins, sell 160 containers at full price and however many bags you can make at the discounted price (the rest go to the squirrels). In the editor below, write a program that does the following:

  • Calculates and prints how many mini muffins were sold at full price
  • Calculates and prints how many mini muffins did NOT make into bags and went to the squirrels
  • Calculates and prints how many bags are made and sold at the discounted price
  • Calculates and prints how much total money was earned by the end of the day

HINTS:

  • Refer to previous examples as guides in developing your code
  • We encourage you to use variables to store various calculations along the way
  • You need to determine how many go to the squirrels before you can calculate how many total bags are sold to the organization
  • The modulus will help you calculate how many go to the squirrels

TEST CASES:

  • If there are 181 containers sold, there will be 0 mini muffins going to the kids or the squirrels. You will make $905.
  • If there are 100 containers sold, there will be 3 mini muffins going to the squirrels and 48 bags going to the kids. You will make $596.
  1. Incorrect Correct No Answer was selected Invalid Answer

    Choose your answers from the Practice above to see if you got it right!

Can we make it simpler?

There are many situations in which we only want to perform a very simple calculation. For example, an adventure video game might want to keep track of how many items the player is carrying. Every time the player picks up or drops an item, the variable containing the item count needs to increase or decrease by 1.

Instead of writing item_count = item_count + 1 every time, we can actually just write item_count += 1. Both of these do the same thing! Take a look at the example below to see some of the different ways you can use this for simple calculations.

  • Can you think of examples for when you could use the below shortcuts and examples when you couldn’t?