Arithmetic Operators and Data Types
Explore how to work with arithmetic operators in Python and understand the difference between string and integer data types. Learn to convert input strings to integers using the int() function and handle floating-point division results. This lesson helps you accurately perform addition, subtraction, multiplication, and division, meeting project requirements in Python programming.
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.
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
# 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)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:
When we use
input(), it takes whatever we type and stores it ...