Home Python Page 4 - Introduction to Python Programming

# Python Mathematical Operators - Python

Python has very little to do with Eric Idle or John Cleese. It's an interpreted, object-oriented scripting language. Designed for clarity and simplicity, it's useful for creating large programs and for gluing pieces of other programs together. Keep reading to add Python to your personal programmer's toolkit.

By: James Payne
Rating:  / 10
November 12, 2007

SEARCH DEV SHED

TOOLS YOU CAN USE

Here is a pretty table listing the Mathematical Operators in Python:

 Operator Symbol What it Does Addition + 2+2 == 4 Subtraction - 4-4 == 0 Division / 12 / 3 == 4 Multiplication * 2 * 4 == 8 Exponentiation ** 10 ** 2 == 100 (Basically you are saying 10 times 10; if you wrote 10 ** 3 it would be 10 times 10 times 10 or 1000) Remainder or Modulus % Returns the remainder from a division. 17 % 4 == 1. If there is no remainder, the value would be 0. Ie; 16 % 4 == 0.

Also, when working with math note that if you do not enter an equation in as a decimal, the result will not have a decimal. So if I said 14 / 3, it would give me the result of 4. If I used the expression 14.0 / 3.0 it would return 4.66666666667. In addition, you give mathematical equations precedence by using parentheses(). So: 3 * 2 + 2 would equal 8, whereas 3 * (2+2) would equal 12. This is because Python sees the parentheses and knows to calculate that portion first, then multiply by 3.

Commenting

You may have noticed earlier that I used the # symbol followed by some text. This is known as a comment. When Python sees the # it gets scared and runs away to the next line. It won't even read what you have written. Comments are used to leave notes for yourself or other programmers who may eventually view your code. They are typically used to describe what a particular snippet of code does. That way when you view your code six years later at four A.M. you don't rip your arm hair out trying to remember what the heck that function was supposed to do.

Well that is it for this episode. In our next action-packed show, we will discuss how to get data from the user and how to use it, and my good buddy the variable. As always, thanks for dropping in and hope to see you soon.

Till then...

 >>> More Python Articles          >>> More By James Payne