Using arithmetic operations in Python

problem

You would like to perform arithmetic operations in Python. That includes simple mathematics like addition +, subtraction -, division /, and multiplication *. In this post we will see simple examples on how to use these operators.

SOLUTION

Same with mathematics we have 4 operators for the 4 classic actions:

Addition

Open the Python interpreter and type a simple addition

				
					3 + 5
				
			

Hit enter and the answer will appear below

The + operator is binary meaning it takes two arguments, in this case 3 and 5 and adds them together. It can take variables also or the result of function and add it.

For example:

				
					x = 10
x + 8
				
			
Addition of variable and a number in Python

Or, a variable plus itself (or other variable)

Add a variable to itself in Python

The + operator can also be used as a unary operator meaning taking only 1 parameter. In this case, it makes the number positive.

For example:

				
					+9
				
			
Make a number positive in Python

Note: No space between the + symbol and the number.

Also, it can be used on a variable:

				
					+x
				
			
Make a variable positive in Python

This way you put a positive sign in front of the number, same with mathematics.

Note: Numbers are positive by default, so in this case it won’t change anything.

Adding two (positive) numbers together:

				
					+60 + +9
				
			
Subtraction

The operator is used for subtraction and is also a binary operator.

It takes 2 parameters and subtracts the one to the right from the one to the left.

In this case, let’s see how to subtract 5 from 8.

				
					8 - 5
				
			
Subtract numbers in Python

Simple, like in mathematics.

As we saw in the addition, the operator can also be used as a unary operator to negate the number.

For example, to turn a number into its negative representation, we can do the following:

				
					-7
				
			
Negate a number in Python

The number 7 is negative now, -7

Note: There is no space between the – symbol and the number.

It can be applied on variables too. For example:

				
					x = 10
-x
				
			

If we have a negative number already and we negate it again, we get its positive representation. For example:

				
					--5
				
			

Or by using parentheses to make it look clearer:

				
					-(-5)
				
			
Double negation with parenthesis in Python

Having a negative number stored in a variable:

				
					n = -6
-n
				
			

If we negate it, it turns positive.

We can also subtract negative and positive numbers:

				
					-5 - 7
				
			

We have -5 and 7

Let’s subtract 7 (positive) from -5 (negative)

Subtract positive from negative number in Python

Answer: -12. The same with mathematics, the 7 will turn -7 and we have -5-7 = -12

Multiplication

To multiply numbers we use the *. Again as a binary operator, we use two numbers with the sign in between them and it will multiply them. 

For example, let’s multiply 3 and 5

				
					3 * 5
				
			

Simple, like in mathematics. 

Let’s multiply a negative number. For example -6 and 6

				
					-6 * 6
				
			
Multiply a negative with a positive number in Python

Multiply two negative numbers:

				
					-5 * -7
				
			
Multiply two negative numbers in Python

If we multiply two negative numbers they turn positive.

Did you know the * symbol can also be used on other data types like strings and lists?

Multiplication

To divide two numbers we use the /. It’s also a binary operator, and we put a number then the / and then the other number. The number to the left is divided by the number to the right side of the symbol. For example let’s divide 60 by 12:

				
					60 / 12
				
			
Divide two numbers in Python

It’s giving us a double result. That’s necessary for representing the remainder of the division.

For example, 4 divided by 3

				
					4 / 3
				
			
Division with remainder in Python
Integer division

At times we want the remainder of the division. This is called an integer division, meaning we get only a whole number back without any floating point. In this case we use the // operator.

Simply we put two forward slashes instead of one.

For example:

				
					4 // 3
				
			
Integer division in Python

It’s giving us 1 instead of 1.333

Note: There is no rounding here. It will simply drop the numbers after the decimal point.

conclusion

In this post we saw how to use the classic arithmetic operators in Python. In particular, the Plus( + ) for addition, Minus (-) for subtraction, asterisk (*) for multiplication, forward slash (/) for division and double forward slashes (//) for integer division.

Share it!

Facebook
Twitter
LinkedIn
Reddit
Picture of Ellion

Ellion

Professional IT consultant, writer, programmer enthusiast interested in all sorts of coding.
Eats all cookies 🍪

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x