Python Arithmetic – Solve Your Arithmetic Assignments using Python
Python Arithmetic Operators do addition, subtraction, multiplication, and division on operands. These operations are called Arithmetic Operations that can also do string concatenation and string multiplication.
Arithmetic Operations are an essential part of what you do every day in your school math class. On this page, we see how Python can solve arithmetic problems.
On this page, you’ll learn:
- What Python Operators and Operands are
- How you can use Python to perform arithmetic operations on numbers and variables
- The concept of precedence of arithmetic operators
- How you can perform string addition (concatenation) and string multiplication
What Are Python Operators and Operands
Let us first introduce the concept of operators and operands. Operators are the add (+), subtract (-), multiply (*), and divide ( /) operators. The operands are the quantities, these operators work on.
As an example, in the expression, 2 + 3, the symbol + is the operator, and 2 and 3 are the operands.
You know that 2 plus 3 equals 5. We write this in an equation as below.
2 + 3 = 5
In the above equation, 2 and 3 are called operands. The plus symbol (+) is called the operator.
Similarly, in the below equation
2 * 3 = 6
2 and 3 are operands. The multiply symbol (*) is the operator.
There are many different types of operators in Python such as:
- Arithmetic operators
- Logical operators
- Comparison operators, and
- Assignment operators.
In this section, we will introduce a limited set of arithmetic operators and one assignment operator.
Logical operators and Comparison Operators are discussed in a Operators and Operands chapter.
Python Arithmetic Operators
For the Table below, let us assume the two operands X and Y have the values as below.
Operand X = 6
Operand Y = 3
OPERATOR | DESCRIPTION | EXAMPLE |
---|---|---|
Addition (+) | Ass two numbers on either side of the + operator | X + Y = 9 |
Subtraction (-) | Subtract right-hand operand from the left-hand operand | X – Y = 3 |
Multiplication (*) | Multiply the operands on either side of the * operand. | X*Y = 18 |
Division (/) | Divide the left-hand operand by t he right-hand operand | X/Y = 2 |
Modulus (%) | Divide the left-hand operand by the right-hand operand and find the remainder. | X%y = 0 |
Division (//) | Divide the left-hand operand by the right-hand operand. Ignore the remainder. | x//y =2 |
Exponent (**) | Raise the left-hand operator by the power of right-hand operator. | X**Y = 216 |
Below is one assignment operator.
Assignment Operator
Example: Y = 2; Z = 3
OPERATOR | DESCRIPTION | EXAMPLE |
---|---|---|
= | This operator solves the expression on the right side of the = sign and gives (assigns) the calculated value from the right side to the left side operand. | X = Y + Z Calculate Y+Z and assign it to X. X = 2 + 3 will assign value 5 to X. |
Simple Python Arithmetic
Now that you understand variables and arithmetic operators, let us see how we can have Python do a little math using Python as a handy calculator using operators (+, -, *, /, etc.)
The following Python program illustrates the use of the arithmetic operators you learned in the previous section. In the example below, we do arithmetic using numbers. Later in this chapter, we will use variables to do arithmetic.
More information on Python Arithmetic is at this link.
PROGRAM EXAMPLE: SIMPLE ARITHMETIC USING NUMBERS
>>> # Example of doing simple arithmetic using Python.
>>> 2 + 5
7
>>> 5 - 3
2
>>> 7*2
14
>>> 9/3 # Division always gives the result in data type float.
3.0
>>> 9%2 # % operator gives the remainder.
1
>>> 9//5 # // operator gives the quotient and discards the remainder.
1
>>> # ** operator raises the power of left operand to the right operand.
>>> 2**3
8
>>>
Precedence of Arithmetic Operators
What do we mean by the term Precedence of Operators? Python has certain rules regarding which operators have higher priority than the other operators. This is referred to Precedence of Operators. We will explain what the term “Precedence of Operators” means in the next few sections.
When you solve math problems, you have to follow a few rules regarding the precedence of operators. For example, multiply and divide have precedence over add and subtract. This means that if an expression contains multiply, divide, as well as add or subtract, you have to evaluate multiply and divide operations before you can do add or subtract operations.
If expressions have parenthesis, then parenthesis has the highest precedence and has to be evaluated before any other operations. The table below explains the priority (precedence) of operators.
OPERATOR | PRECEDENCE |
---|---|
Parenthesis () | 1 (Highest) |
Exponent (**) | 2 |
Multiply and Divide | 3 |
Addition and Subtraction | 4 |
According to the precedence rule, we solve the expression within the parenthesis first, followed by the exponent. Then, we solve the expression for divide / multiply operators. Lastly, we do the addition/subtraction operations.
The following Python program examples will clarify the operator precedence priority.
PROGRAM EXAMPLE: PRECEDENCE OF ARITHMETIC OPERATORS
>>> # Example of precedence of operators.
>>> 2*3+5
>>> # Multiply has higher precedence than add.
>>> # First solve 2*3, and then add 5 to the result.
>>> # It would be wrong to do 3+5 first, and then multiply the result by 2.
11
>>> 4*3/2+7 # Multiply and divide have higher precedence than add.
13.0
>>> (2+7)*4 # Parenthesis has higher precedence than multiply.
36
>>> 2**3*4 # Exponent has higher precedence than multiply.
32
>>> (2*3)**2 # Parenthesis has higher precedence than exponent.
36
>>>
Python Arithmetic Using Variables
In this section, we will write programs to do arithmetic using variables instead of numbers.
Our programs will use the conversion factors related to measurement units.
There are two measurement systems used in the world for measuring mass, length, and temperature. United States uses the Foot-Pound-Second system, abbreviated FPS (Feet for length, Pounds for mass, and Seconds for time). Most of the rest of the world uses what is called Meter-Kilogram-Second (abbreviated MKS) system. It is also called SI units. (Meters for length, Kilograms for mass, and Seconds for time).
These measurement units are explained in the Enrichment section at the end of this chapter. In this section, we will write a program to convert the lengths and weights from one measurement system to the other.
Continuing with our Dinosaur facts, let us write a program to convert Tyrannosaurus Rex length from feet to meters and its weight from pounds to kilograms.
To write this program, we will define four variables as below:
VARIABLE NAME | MEANING OF VARIABLE |
---|---|
TRex_length_feet | Tyrannosaurus length in feet |
TRex_length_meters | Tyrannosaurus length in meters |
TRex_weight_pounds | Tyrannosaurus weight in pounds |
TRex_weight_kilograms | Tyrannosaurus weight in kilograms |
The program multiplies the tyrannosaurus length in feet by the feet-to-meter conversion factor (0.3038).
To convert the weight, the program multiplies the Tyrannosaurus weight in pounds by the pounds-to-kilograms conversion factor (0.4536).
For conversion factors between the two systems, look up the conversion table in the Enrichment: US and Metric Units section at the end of this page.
PROGRAM EXAMPLE: PYTHON ARITHMETIC USING VARIABLES
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> # Example to convert from feet to meters
>>> # and from pounds to kilograms.
>>> TRex_length_feet = 40
>>> TRex_weight_pounds = 20000
>>> TRex_length_meters = TRex_length_feet*0.3038
>>> Trex_weight_in_kilograms = TRex_weight_pounds * 0.4536
>>> print(TRex_length_meters)
12.152000000000001
>>> print(Trex_weight_in_kilograms)
9072.0
>>>
Notice that Python always gives results in floating points, if one of the operands is a floating-point number. Since One feet = 0.3038 meters is a floating point number and One pound = 0.4536 kilogram is a floating-point number, the calculation results are given in floating-point data type.
Formatting the Output
The Python program in the previous section converted the weight and length from one measurement system to the other system using Python Arithmetic. As a programmer, you would like to present the results of the computations that are meaningful for anyone who reads your program. Here is an example of how to use the print()
function to present the output with a text message so that the result is easy for the reader to understand.
PROGRAM EXAMPLE: FORMATING THE OUTPUT
>>> Example on how to format the output with a text string.
>>> Trex_length_in_feet = 40
>>> Trex_weight_in_pounds = 20000
>>> Trex_length_in_meters = Trex_length_in_feet*0.3038
>>> Trex_weight_in_kilograms = Trex_weight_in_pounds*0.4536
>>> print('Trex length in meters =',Trex_length_in_meters)
Trex length in meters = 12.152000000000001
>>> print('Trex weight in kilograms = ',Trex_weight_in_kilograms)
Trex weight in kilograms = 9072.0
>>>
In the above example, a string within quotes in the print()
function is printed as is along with the computed result. This tells the reader what the computed result means.