Tuesday 24 May 2011

Python Arithmetic Operators

Okay, so we're up and running and we're ready to get into it! But don't get ahead of yourself just yet, we need to cover some basics first. It won't take long, but it's good to know what we're using and what it does.

The first thing we are going to cover is the Python Arithmetic Operators. The following table will show the different operators used with Python and what they do:



OperatorUse
+Addition - Used to add values. If using a floating point and an integer, the result will be a floating point. (Discussed Later)
-Subtraction - Same as + (addition)
*Multiplication Same as + (addition)
/Division - Integer and floating point division will always result in a floating point.
//'Floor' Operator - Returns the largest integer value less than the result if you use the Division Operator (Above).
%Modulo - Returns the remainder after division.
**'Power' Operator - Works for integer and floating point values, returning the initial value to the power of the second value.

 Hopefully that will explain what everything does clearly enough. Play around with the different operators and see what kind of answers you get. You can see some examples below of what to expect:



One thing to keep in mind when doing problems as seen above, is the order of operations. Python follows the standard algebraic rules for the order of operation:
  • Parentheses (innermost to outermost)
  • Exponentiation operations
  • Multiplication and division operations (left to right)
  • Addition and subtraction operations (left to right)
Following are some examples to try out to help you get used to the order of operations:
5 * (2+2) = ?
5 * 2 + 2 = ?
4 * 2 / 2 * 4 = ?
4 * 2 / (2 * 4) = ?



Now that we have that covered, you should have a grasp on the basic arithmetic operators and we can move on to the Assignment Operator.

The Assignment Operator, is used to assign a value to a variable. This is particularly useful when we need to store and access values from memory. The assignment operator is a single equals sign ( = ). In the following example you will see one of the many ways it can be used.


In the above example, we use 3 variables (num1, num2, num3) to hold the values using the assignment operator. To use this, you type the name of the variable (using the appropriate naming conventions), the equal sign, the value, and press [Enter]. The value can then be accessed by typing in the variable name again.

Using the above information, what would the following produce:
num = 6
num = num + 1
num = ??



The same method can be used to store string values (as below):


As you can see, outputting the value is different to the numerical value. When using strings, it is best to output the value using the print function which will be covered later on.

Once you have gotten used to using arithmetic operators and assignment operators, you can move on to the next (short) lesson: Python Files

-Nathan (everythingPython)

1 comment:

  1. Hiiii....Thanks for sharing Great information...Nice post...Keep move on...
    Python Training in Hyderabad

    ReplyDelete