Counting and Keeping Track with Edward

Learn about arithmetic operators and data types in Python.

With the ability to memorize, Edward can now tell whether or not he has already potted a plant or removed trash.

What if Edward could also count the number of tasks he performed, like the number of plants he has potted or the number of moves he has made? This could help Edward make elaborate plans and keep track of his progress. With this ability to count, we can even add more features to Edward, such as the amount of energy he consumes.

Counting the placed plants

The ability to count would be a great enhancement for Edward. Let’s interact with the widget below. Edward can now count the number of plants he has placed. What’s more interesting is that his energy reduces with each step he takes. He loses 3% for each move, 5% for when he places a plant and removes trash, and 1% for each turn.

All of this is possible due to mathematical operations such as addition and subtraction.

These basic mathematical operations are fundamental in programming and are used in various contexts to manipulate data and perform mathematical calculations.

Mathematical calculation in Python

In Python, we use arithmetic operators to perform these calculations. These operators work just like when you solve simple math problems. For example, the + symbol adds numbers together, - subtracts them, * multiplies them, and / divides them.

Arithmetic Operator

Operation

Example

+

Addition

10 + 5 results in 15

-

Subtraction

10 - 5 results in 5

*

Multiplication

10 * 5 results in 50

/

Division

10 / 5 results in 2

Adding two numbers

Let’s see how to add two numbers in Python. This should be easy: create two variables to store the numbers we want to add, compute their sum, and print the result.

Python 3.10.4
num1 = 3
num2 = 5
result = num1 + num2
print(result)

The sum can also be directly computed inside the print() function without explicitly storing the result in a variable. So the above code can also be written as:

Python 3.10.4
num1 = 3
num2 = 5
print(num1 + num2)

Similarly, we can perform subtraction, multiplication, and division in Python. Let’s look at an example of each operator in use:

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

After running the program above, you may have noticed that the last output (3.5) is a fractional value, and rightly so. This type of data, numbers that have decimal points (such as 2.33, 4.0, etc.), is known as a float data type in programming. The whole numbers (numbers without decimal points), on the other hand, are called integers with the data type int. The text or words that we’ve been printing in quotations are known as a string data type.

To elaborate, a data type in a computer is like a label that tells the computer what kind of data is stored in a particular variable. We label data in a computer with its data type so the computer knows how to handle it. For example, the data type int is used for whole numbers, while float is used for numbers with decimal points and string is used for text. Each data type has specific rules and operations that can be performed on it

In Python, when we perform division with numbers, the division operation (/) produces a float even if the result is a whole number. For example, 8/2 = 4.0. This behavior is consistent with the idea that a division operation can produce a fractional result.

Now, we’ve seen how the different operators can be used to perform arithmetic operations. We can even write a calculator program using this. When using calculators to calculate something, we enter numbers that we want to add or subtract, etc. Let’s write a program that takes two and adds them.

Adding two inputted numbers

Let's input two numbers and assign them to num1 and num2, and then print num1+num2. Run the program below to see what we get.

# 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 is ", num1 + num2)
Adding two numbers taken from the user

Let's try to enter 8 as the first number and 2 as the second number in the program above.

Wait a minute! We get the output 82 instead of 10. That's strange. We simply entered two numbers from the keyboard, added them, and printed them. Why do you think that is?

Could it be that the computer doesn't know that what we're entering is a number, which is why it fails to add them?

The answer is that whenever we input anything using input() into a program, its stored as a string.

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 information stored in num1 and num2 is treated as text and not as 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 joins 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.

Recall that 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 

Luckily, we can convert the data types. Python’s built-inReady-made functions that we can call and use whenever we want, without worrying how they work or get the job done. . function int() lets us convert a string to an integer, and function str() lets us convert an integer to a string. 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 is", num1 + num2)
Adding two numbers taken from the user

Now, if we enter any number, our program actually calculates the answer rather than just joining the numbers together. This is because int() is a built-in function in Python that can take a number in string format as input and output an integer version of it by changing its data type.

A quick challenge

Imagine you are given two numbers:

num1 = 7 # an integer
num2 = 5.5 # a float

Task

  1. When you add them together using the expression result = num1 + num2, what type of data would result be? Explain why!

  2. Why would you prefer to use a float data type instead of an int in a program that calculates the average of numbers?

Saved
A quick knowledge check

Recap

Key takeaways:

  1. Arithmetic operators: We have arithmetic operators that let us perform mathematical operations.

  2. Built-in functions: Just like print(), input(), len() Python has ready-made functions int() and str() that make it possible for us to compute correct sums by letting us convert data types.

  3. Converting string to int or float: It’s always a good idea to convert the str input from the keyboard to int or float in the case of numbers.

  4. Converting int to string: It’s always a good idea to convert the int to str in case text output needs to be formatted.