Basic Math Ops in Python

Basic Math Ops in Python

This is a brief overview of basic math operations using python covering addition, subtraction, multiplication, division, integer division, exponents, and modulus / remainder division.

Basic Math Ops in Python

We’ll start with addition and  subtraction in python. Adding and subtracting in python is the same as it was in math class when you were in school. For instance to add 5 + 5 or subtract 5 – 5 in python you enter:

							
							
					5 + 5
#for addition
---------------
#python would return 10

5 - 5
#for subtraction
---------------
#python would return 0				
			

Similarly for multiplication and division in python, however notice that the “x” (multiplication sign) in python is an asterisk “*”. 

							
							
					5 * 5
#to multiply in python
---------------
#python would return 25

5 / 5
#division in python
---------------
#python would return 1				
			

Exponents are likewise, just replace the carrot symbol “^” with double asterisks “**”.

							
							
					5 ** 5
#exponential powers in python
---------------
#python would return 3125				
			

Floor division (dividing and rounding down to the nearest whole number) is accomplished via: 

							
							
					5 // 2
#floor division in python
---------------
#python would return 2				
			

And “remainder division” is accomplished via: 

							
							
					5 % 2
#remainder division in python
---------------
#python would return 1				
			

Note that I have separated the characters with spaces throughout this tutorial for instance 5 + 5 rather than 5+5, it’s just personal style, python doesn’t care whether or not you separate the characters with spaces.

Order of Ops in Python

Order of operations (the order in which mathematical operations occur) are similar to the order of ops you learned in middle school. Exponents are evaluated first, followed by multiplication and the division operators, and finally followed by addition and subtraction, always read left to right.

That’s a wrap! You now know how to perform basic math operations in python. So why not just install python and delete that calc app? 😉 If you enjoyed this post, like it and subscribe!

Walter Miely is a tech entrepreneur and CEO of Phoenix Ignited Tech You can find him on Linkedin. This material is licensed under the CC BY 4.0 License LEGAL DISCLAIMER: The content provided here is provided AS IS, and part of, or the entirety of this content may be incorrect. Please read the entireLegal Disclaimer here.