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
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

Google Analytics Cookies

This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

Keeping this cookie enabled helps us to improve our website.

HotJar Cookies

We use Hotjar in order to better understand our users’ needs and to optimize this service and experience. Hotjar is a technology service that helps us better understand our users’ experience (e.g. how much time they spend on which pages, which links they choose to click, what users do and don’t like, etc.) and this enables us to build and maintain our service with user feedback. Hotjar uses cookies and other technologies to collect data on our users’ behavior and their devices. This includes a device's IP address (processed during your session and stored in a de-identified form), device screen size, device type (unique device identifiers), browser information, geographic location (country only), and the preferred language used to display our website. Hotjar stores this information on our behalf in a pseudonymized user profile. Hotjar is contractually forbidden to sell any of the data collected on our behalf.

For further details, please see the ‘about Hotjar’ section of Hotjar’s support site.