Arithmetic Operators and Data Types

Learn about the arithmetic operators and data types in Python.

Back to our project

So far, we have covered good ground in learning how to code for some of the project requirements. Let’s take a look at our client’s requirements once again.

Project requirements that require printing on the screen
Project requirements that require printing on the screen

According to requirements 4 and 6, our app should be able to:

  • Perform calculations of addition, subtraction, multiplication, and division

  • Compute the actual answer to each question

So, let’s dive right in!

Adding two numbers

This should be easy: input two numbers and assign them to num1 and num2, then print num1+num2. Run the program below and see if we’re getting the expected outputInput 8 and 2. What should be the expected output of 8 + 2?.

# Taking two numbers as input and storing inside two variables respectively
num1 = input("First number: ")
num2 = input("Second number: ")

# Displaying the result
print("The answer to ", num1, "+", num2, "is ", num1 + num2)
Program to add two numbers

Let’s say we enter 8 as the first number and 2 as the second number in the program above. But wait a minute! We get the output 82 instead of 10. Why do you think that is?

The data types and their conversion

In our code:

  1. When we use input(), it takes whatever we type and stores it as a string. So, the numbers stored in num1 and num2 are treated as text and not as mathematical numbers.

In Python, str is a basic data type used to represent text or sequences of characters (string). It is employed for storing and manipulating words, sentences, or any textual information in a program. Strings are enclosed in double quotes " ".

  1. When we try to add num1 and num2 using the + operator, it concatenatesThis is the action of joining or linking strings together to create a longer string. the two strings rather than adding the numbers. That is why we got 82 instead of 10.

To solve this problem, we need to convert these strings to numbers (to another basic data type called integer), so the computer understands that we want to do math with them.

An integer is a whole number, either positive or negative, without any decimal or fractional parts.

If there was a way to convert the input strings (str) to integers (int), perhaps we could add the two numbers easily using a +.

The int() function

Python’s built-in function int() lets us do just that. It can take a number in string format as input, and output an integer version of it. Here’s an updated version of our code that converts the strings to integers using int():

# Taking two numbers as input and storing inside two variables respectively
num1 = input("First number: ")
num2 = input("Second number: ")

# Converting input strings to integers
num1 = int(num1)
num2 = int(num2)

# Displaying the result
print("The answer to", num1, "+", num2, "is", num1 + num2)
Adding two numbers

Running the code above gives us the expected result 10. So the int() function helps us convert a string to an integer!

It’s always a good idea to convert the str input from the keyboard to int in case of numbers.

The str() function

Conversely, we could encounter a situation where we may want to convert an integer to a string. We can do that using Python’s str() function which can take an integer as an input and output a string version of it.

For example, let's look at the program below.

Arithmetic operations

Now that we’ve covered the concepts of basic data types, like int and str, and how to convert each data type to another, let’s get back to our project requirements. This brings us to the arithmetic operators.

The arithmetic operators in Python

In Python, arithmetic operators are special symbols that perform operations on variables and values, just like when solving simple arithmetic equations. For numbers, we have already seen how the + operator adds two numbers together. Other arithmetic operators available in Python are - for subtraction, * for multiplication, and / for division.

Let’s look at an example of each operator in use.

Python 3.10.4
num1 = 7
num2 = 3
# addition
print("7 + 3 is", num1 + num2)
# subtraction
print("7 - 3 is", num1 - num2)
# multiplication
print("7 * 3 is", num1 * num2)
# division
print("7 / 3 is", num1 / num2)

Now, after running the program above, you may have noticed that we’re getting the last output as a fractional value, and rightly so. Just like we have an int data type for whole numbers, we also have a basic data type for numbers with decimal values (such as 2.333) known as float.

In Python, the division operation (/) between two integers results in a float value, even if the division should logically yield an integer. This behavior is consistent with the idea that a division operation can produce a fractional result. For example:

Python 3.10.4
# division
print(6 / 3)

In this case, the division 6 / 3 results in a floating-point value (2.0). When we perform division with numbers in Python, the division operation produces a float even if the result is a whole number. The .0 at the end indicates that it’s a floating-point number.

Now, if we want to ensure that the result is an integer, then we can use the int() function to make sure the result is an integer:

Python 3.10.4
# Converting the result to an integer by truncating the decimal part
print(int(6 / 3))
print(int(7 / 3))

So now, the division 7 / 3 is performed, resulting in a floating-point value (2.3333333333333335). The int() function is then applied to this value. This function converts the number to an integer by truncating the decimal part. As a result, we get 2.

Now that we know we have operators available in Python, we can easily compute the correct answer to each question.

In the program below, let’s take the user’s answer and then print its correct answer (calculated by our program).

n1 = 33
n2 = 4

question = "What is " + str(n1) + "+" + str(n2) + "? "
user_answer = input(question)
print("Your answer is:", user_answer)
# Computing the correct answer
print("The correct answer is:", n1 + n2)
Printing the user’s answer and the correct answer

We can do the same for all operators as below:

n1 = 33
n2 = 4

question = "What is " + str(n1) + "+" + str(n2) + "? "
user_answer = input(question)
print("Your answer is:", user_answer)
# Computing the correct answer
print("The correct answer is:", n1 + n2)

question = "What is " + str(n1) + "-" + str(n2) + "? "
user_answer = input(question)
print("Your answer is:", user_answer)
# Computing the correct answer
print("The correct answer is:", n1 - n2)

question = "What is " + str(n1) + "*" + str(n2) + "? "
user_answer = input(question)
print("Your answer is:", user_answer)
# Computing the correct answer
print("The correct answer is:", n1 * n2)

question = "What is " + str(n1) + "/" + str(n2) + "? "
user_answer = input(question)
print("Your answer is:", user_answer)
# Computing the correct answer
print("The correct answer is:", n1 / n2)
Printing the user’s answer and the correct answer for different arithmetic operations

Since our project is for elementary school students, let’s not expect them to compute the fractional parts of the division questions. How about we convert the actual answers to keep only the integer part of the answers?

Go ahead and update the code above to change the floating point result to an integer.

Show Solution

That’s it! We’ve learned to incorporate requirements 4 and 6.

Review of the lesson

To sum up, we learned some of the built-in functions int() and str() in Python, which made it possible for us to compute correct sums. You might begin to think that Python has a built-in function for every situation in the world. However, that is not true, and it’s a good thing too.