Basic Python Arithmetic Operators Video

Basic Python Arithmetic Operators

Jeremiah Taguta (BSc in Computer Science, Masters in Information Systems)
Published on 07-04-2023
Learn how to perform basic arithmetic operations in Python, such as addition, subtraction, multiplication, division, floor division, modulus, and exponentiation.


The Rationale

At times, real-world problems we solve require the application of mathematics. Basic mathematical operations become vital, without which we may not solve the problems. Thus, we must discuss how basic mathematical operations may be implemented in Python.

The Python programming language is feature-rich and powerful, supporting a dozen mathematical operations. These include addition, subtraction, multiplication, division, floor division, modulus, exponentiation, trigonometric functions, logarithms, and more.

NB: These are mathematical operations; as such, we will use floats and integers in our examples. This doesn't mean that addition is only used for numbers; no, it may be used for other data types, e.g., string concatenation due to polymorphism or method overloading, in particular. This is a great Python feature we shall talk about later.

It is also important to note that we may use variables in our code as well as values on the fly for arithmetic operations.

In this article, I will cover only the basics. I will also include a simple temperature converter project since we would have covered enough to do the project. Do this project, as knowing the Python syntax and applying it in a real-world scenario is essential. 

Throughout this program, we shall mainly use the following variables and label the outputs:

1 num1 = 10
2 num2 = 20

Addition

To add two or more numbers in Python, you can use the "+" operator, e.g.

1 sum = num1 + num2
2 print(num1, " + ", num2, " = ", sum)  # Output: 10  +  20 = 30

Subtraction

To subtract one number from another, you can use the "-" operator, e.g.

1 difference = num2 - num1
2 print(num2, " - ", num1, " = ", difference)  # Output: 20  -  10  =  10

Multiplication

To multiply two or more numbers, you can use the "*" operator, e.g.

1 product = num1 * num2 * 2
2 print(num1, " * ", num2, " * ", 2, " = ", product)  # Output: 10  *  20  *  2  =  400

Division

To divide one number by another, you can use the "/" operator, e.g.

1 quotient = num2 / num1
2 print(num2, " / ", num1, " = ", quotient)  # Output: 20  /  10  =  2.0

Floor or Integer Division

The floor or integer division operator returns the quotient of a division as an integer, discarding any fractional or decimal part. It can be performed using the "//" operator, e.g.

1 int_quotient = num1 // 3
2 print(num1, " // ", 3, " = ", int_quotient)  # Output: 10  //  3  =  3

Modulus

The modulus operator returns the remainder of a division operation. It can be performed using the "%" operator, e.g.

1 num3 = 3
2 remainder = num1 % num3
3 print(num1, " % ", num3, " = ", remainder)  # Output: 10  %  3  =  1

Exponentiation

The exponentiation operator is "**". It raises the first operand to the power of the second operand, e.g.

1 expo = num1 ** num3
2 print(num1, " ** ", num3, " = ", expo)  # Output: 10  **  3  =  1000

Printing results on the fly

It's not every time that we need a variable. In some situations, all we need is to display the result of an arithmetic operation. The print function may thus print the evaluation of the operation, as seen below.

The arithmetic operations may be between variable values, line 1, variable and non-variable values, line 2, or non-variable values, line 3, as shown below.

1 print("Sum of ", num1,",", num2,",", num3, " is", num1 + num2 + num3 )  # Output: Sum of  10 , 20 , 3  is 33
2 print("2 + ", num1, "=", 2 + num1 )  # Output: 2 +  10 = 12
3 print("10 * 20 = ", 10 * 20) # Output: 10 * 20 = 200

Temperature Converter Programs

Description

This simple program converts temperatures from Fahrenheit (°F) to Celsius (°C). The program should print the temperature in Celsius. We shall hard code the Fahrenheit value to be 72°F and 120°F in the first and second programs, respectively.

Problem-Solving: Don't think in terms of programming

How do we convert a temperature from Fahrenheit to Celsius? Which domain does our program come from? Your guess is as good as mine; it's from physics. So, in physics, how do we do the temperature conversion? Is there an existing mathematical formula? The formula will serve as our algorithm.

Formula

°C = (°F - 32) x 5/9 

To convert a temperature from Fahrenheit to Celsius, we need to subtract 32 from the Fahrenheit temperature and then multiply the result by 5/9.

Code

Choosing variables 

We should choose to store a value in a variable when we need to access or modify that value multiple times in our code or when the value is complex and difficult to calculate or remember. In our case, the 5/9, Fahrenheit, and calculated Celsius values will be stored in variables. Parenthesis is also used for the order of operation (BODMAS) as applied in mathematics.  See the solution below and don't worry much about the number of decimal places for now.

1 # Temperature convertor 1 : Convert 72°F to °C
2 multiplier = 5/9
3 temp_fahren1 = 72
4 temp_celc1 = (temp_fahren1 - 32) * multiplier
5 print(temp_fahren1,"°F = ", temp_celc1,"°C") # Output : 72 °F =  22.22222222222222 °C

6 # Temperature convertor 2 : Convert 120°F to °C
7 temp_fahren2 = 120
8 temp_celc2 = (temp_fahren2 - 32) * multiplier # use already stored multiplier value
9 print(temp_fahren2,"°F = ", temp_celc2,"°C")  # Output : 120 °F =  48.88888888888889 °C
Thanks for reading. If you have any questions, feel free to ask in the comments section, I'll gladly help. Check out my programming tutorials on YouTube and don't forget to subscribe, comment, like, and share on both YouTube and on this Blog. Lets code together!!!

190 Like

Drop your comment

© Copyright 2023 Jeremiah Taguta. All Rights Reserved.

Product of ‌

Jeremiah Taguta T/A Charis Technologies