Python
Math Operations
Assignment
Assignment is the process of storing
information in a memory location with an assigned name
It is not limited to numerical data
In the following expression:
a = 2 + 3
+ is the addition operator
(specifically, an infix operator)
2 and 3 are operands (constants here, but they could be variables)
= is the assignment
operator
a is the named
memory location where the result of the addition operator is stored
Unlike most other modern programming languages,
you do not need to �declare� a variable before you use it
How assignment works
The right hand side (RHS) of the assignment
operator is �read only�
The left hand side (LHS) is �write only�
All the operations on the RHS are performed
before assigning the result to the LHS
Now, an expression like this makes sense:
a = a + 1
Don�t think algebraically!
Instead, follow the rules:
Perform the operations on the RHS first
Look up the value of a and add 1
Assign the result of the RHS to the LHS
Now a has an integer
value that is 1 larger than its previous value
Precision
Computers have a finite amount of memory
Answers have to fit into the available memory
An answer with infinite precision has to be
�rounded off� to fit into available memory
We have tried floating point division already:
>>> 5.0/3.0
1.6666666666666667
Which is a rounded approximation of the exact
value 1 2/3
Remember�the type of division performed depends on the context
If both operands are integers, then integer
division is performed!
Most modern programming languages let you
express answers with varying degrees of precision
Here is a good
description of how this works in Python
You can control the printed precision
like this:
>>> 'the answer is %5.2f' % (5.0 / 3.0)
'the answer is� 1.67'
We will talk about print formatting in another
lecture
In this case, the precision is limited to a
number that will take up 5 spaces with 2 reserved for values to the right of
the decimal point
Common math operators
+, -, *, /
Addition, subtraction, multiplication,
division arithmetic operators
Please note that Python �overloads� operators
If a + sign is surrounded by integers, integer
arithmetic is performed
>>> 1 + 2
3
If it is surrounded by strings, string
concatenation is performed
>>> 'I am ' + 'Jim'
'I am Jim'
Python does what it can to figure out what you
want but there are limits to its patience
>>> 'the answer is ' + 7
Traceback (most recent call
last):
� File
"<stdin>", line 1, in <module>
TypeError: cannot
concatenate 'str' and 'int'
objects
In other words, the types of entities on
either side of the arithmetic operators must match
This works, though:
>>> 'the answer is ' + str(7)
'the answer is 7'
because str() provides a string interpretation of its
�argument� (the value in parentheses)
Other math operators
a ** b
Raises base a to some integer
or fractional power b
>>> 100 ** .5
10.0
�� a % b
finds the remainder when a is divided by b
Normally used with integers
>>> 3 % 2
1
But can be used with floats
>>> 3.1 % 2.0
1.1
Convenient operators for �compound� assignment
We will often need to update the value of a
numerical variable, e.g.
a = a + 1
Since these operations are so common, Python
has �shortcut� operators that make typing and understanding easier
+=
These 2 expressions produce the same result:
a = a + 5
a += 5
Other operators that work the same way include
-=, *=, /=
Precedence
Most modern programming languages interpret
compound math operations in the same way
They generally follow accepted mathematical
practice in formal expressions
In the following expression:
>>> 3 + 4 * 5
23
The rules of precedence require that a
multiplication in an expression be performed before an addition
We can change precedence by using parentheses:
>>> (3 + 4) * 5
35
The
specific Python rules of precedence are here
The numpy module
The Anaconda distribution includes numpy, a general purpose �scientific computing� collection
of useful functions
numpy contains many good
analytical functions as well as simpler trig and utility functions
To use numpy, your
module must import it (I�m assuming we�re working in a text editor here):
import numpy
sinOneHalf = numpy.sin(.5)
myAngleInDegrees = 180
myAngleInRadians = numpy.deg2rad(myAngleInDegrees)
# and so on�
Here is some code
that exercises trig functions and constants in numpy
In the file I frequently use the \ character
to indicate that a python statement extends onto the next line in the text
I also use the str() command to convert
numerical information to a string
You need to do this if you mix strings and
numerical data in a single print statement