Arithmetic Operations in Python

Python can work like a calculator. You can use it for math operations right off the bat, just like you would use a calculator. And yes, you would need to have a program that will calculate some data in the script.

For example, when you are coding a game, you need math operations to add scores, to multiply actors or characters on the screen, or kill the actor. When coding bots, you need to use math operators for almost everything.

Let’s do some basic math:

Mbappe and Ronaldo walk into a bar. And Mbappe kicks the rebound! Eww, that joke was dry.

Suppose Mbappe and Ronaldo walked into a real bar and ordered drinks. In that case, we could use Python to calculate how many they had. Say the barman came and said Ronaldo’s bill is 7 dollars and Mbappe’s is 4. How much did they pay?

You could simply do it like this:

print (7 + 4)

However, what if the prices are variables? They had more drinks, and the prices were not the same. We may need to use variables then and add them.

Ronaldo = 7
Mbappe = 4

print (Ronaldo + Mbappe)

Well, if you start typing the variables out for every addition, you may run out of time. Here is a shortcut:

You can use a new variable to hold the addition.

Ronaldo = 7
Mbappe = 4

total = (Ronaldo + Mbappe)

print (total)

You can see that the addition sign works just like in school maths.

So, let us talk about basic Python math operators:

OperatorJobExample
+ AddSums up the values on both sides of the operator10 + 20 = 30
– SubtractSubtracts the value on the right side from the value on left30 – 20 = 10
* MultiplyMultiplies the values on both sides of the operator10 * 20 = 200
/ DivideDivides the value on the left by the value on the right200 / 20 = 10
% ModulusDivides value on the left by value on the right and gives the remainder201 % 20 = 1

I can swear you are familiar with all the other operators except the modulus. Maybe the star sign representing the multiplication may feel new too. Anyway, I am not going to teach you multiplication.

So, let us talk about the Modulus.

The modulus is what programmers call the Remainder value.

Say, a father ‘hen’ loves his kids so much that whatever he gets from hunting, he shares equally among his family. He brought home some delicious worms and gave worms to every one of his kids and his wife. And he only eats whatever is left after sharing.

Now, the fowl got 7 worms. And there are two chicks left there opening their hungry beaks. He can give 3 worms to the first and the second, with only one worm remaining.

That is 7 worms = 3 * 2 + 1

Let’s imagine that the chicks are 3, and there are 17 worms to go around. The father will not divide 17 equally among the three babies. That is because 17/3 cannot give you a whole number (int) but a decimal or fraction.

That means there is a whole number and a remainder. That is what the modulus operator does. When you use the slash or divide, you get the result from the division. In math, that is called the quotient. However, equations like 17/3 will result in a whole number and a remainder. When you use the modulus, you get the rest.

That is

17%3
print (17 % 3)

What did you get? 2? Great!

So, in simple terms, modulus calculates the remaining amount after a division operation of two indivisible values. If you use the divide sign(/), you will get the number with a fraction as an output. However, suppose you don’t want to get the result (quotient) and only care about the remainder. In that case, you want to see what will be remaining after the division operation. You will use the percent sign (%) instead of the slash.

You don’t care how many worms each chick will get. You only care how many worms will be remaining with the father ‘hen’. This means you care about the remainder, not the result.

Python uses the modulus (the percent sign) to get the remainder from a division operation.

So, in summary, if you want to get the result (quotient), you will use the slash (/), and if you’re going to get the remainder, you will use the percentage sign.

Using Variables with Modulus

Remember that we use variables to store math values so that we can use them in calculations in a program.

For this math operation to be useful in any Python program, you must assign it to a variable. Let us use an example. Type the following code in your Visual Studio Code IDE and run it:

a = 48
b = 7

ans = a % b

print (ans)

You should get the answer as 6.

Adding Strings

There are many cool math operations that you can do with Python. Now, for learning Python for work, we will discuss another sweet math. You would agree that Python typically adds numbers that are floats or integers. However, you can use Python to join sentences and words in a script. Let us use an example. I will write a program where the user must input their name.

This time, we will use the plus sign to add two strings:

first_name = "Big"
last_name = "Mac"

full_name = first_name + last_name

print (full_name)

What is the result? For instance, my full_name, after adding the variables of first_name and last_name, came as BigMac. There was no space between “Big” and “Mac”.

However, if you want to add a space in your name, you can create a white space with Python or simply add the space in the values when you make them. But the safest way is to create a blank space.

You can create a blank space by writing the space as a string. Like this:

first_name = "Big"
last_name = "Mac"

full_name = first_name + " " + last_name

print (full_name)

Type that code and run it. You see! Well done. Let us review what you have learned so far:

Variables

Input

int ()

print ()

String

Can you write anything about these terms in your book? Please, thank you.

Now, let us talk about Decision Making in Python.

Do you want to become an expert programmer? See books helping our students, interns, and beginners in software companies today (Click here)!

Leave a Comment

Python Tutorials