Table Of Contents

Previous topic

Just Enough Python for Numerical Analysis

Next topic

Modeling Polynomials

Day 1: A First Look at Python


We will be using a small subset of Python as our language of choice for Math 400.

Starting Python (IDLE)

When you first run IDLE you get a shell which allows you to directly interact with Python. This is where we typed print(“Hello, World”) at the shell prompt


The shell is a place where you interact directly with the Python interpreter.


Python as a Calculator

We started by using Python as a calculator – simple arithmetic. Type 2+3 and the shell responds with a 5 and a new shell prompt.

>>> 2+3

From now on we show the last shell prompt.

Subtraction, multiplication, and exponentiation are done as expected. Anything typed after a hash mark #, is comment – text that is ignored by the shell, but useful for humans, who also read the code.

>>> 5-3
>>> # This is a comment
>>> 5*3
>>> 5**3  # exponent are indicated by ** not ^

We can also assign values to variables. For example:

>>> a=5
>>> b=7
>>> c=a*b
>>> c

Division is not quite so straight forward.

>>> 7/3

This response make sense in that 3 does go into 7, 2 times. Perhaps you expected different response.

In some computer languages (e.g. C++) programmers need to explicitly declare the data type of each variable. Python has data type, but they tend to be implicit. For example,

>>> type(7)
<type 'int'>
>>> a=7
>>> type(a)
<type 'int'>
>>> type(7.0)
<type 'float'>
>>> f=7.0
>>> type(f)
<type 'float'>

We can get a different (though not necessarily better response) by letting the shell know that we expect a floating point response.

>>> 7.0/3

That last 5 is something to consider.

Using Libraries

At some point, we will start using libraries of functions written for Python. For example if we type pi

>>> pi
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
NameError: name 'pi' is not defined

The shell barks back at us with an error message. Python does not know about \(\pi\). By importing the math library we can get a useful approximation of \(\pi\) and access to other math functions.

>>> import math
>>> math.pi
>>> math.sin(math.pi/2)

Notice that each object from the math library needs to be called by its full name. You can bring the math objects into the shell’s namespace so that the math library objects do not need to be called by their full names.

>>> from math import *
>>> pi
>>> sin(pi/3)

Modeling Sigma Notation

Say we would like to compute:

\[\displaystyle s=\sum_{k=0}^6 k^2\]

We start by setting k and s equal to 0.

>>> s=0
>>> k=0

Now we can use a while loop by entering while k<7: at the prompt.

>>> while k<7:   # don't forget the full colon

When you do this in IDLE, you will not see the three dots. In a different Python shell, the ... is a next level prompt that says, “I’m ready for you to give me more.” Unfortuately IDLE has a blank prompt at the second level. In this document, we’ll use ... for the second level (“give me more”) prompt.

Before each time through the loop, IDLE checks to if k is less than 7. If so, we want to add k to our sum and increment k. Both tasks are done by assigning a new value to each variable based on the current value of the variable. The = is used to assign a new value to a variable. (The = is not an indication of equality.)

>>> while k<7:  # if k < 7 do the following
...   s=s+k*k   # add k squared to existing value of s
...   k=k+1     # add 1 to k

Putting it all together:

>>> s=0
>>> k=0
>>> while  k<7:

>>> s

Let’s switch two statements. What will this produce when you push enter?

>>> s=0
>>> k=0
>>> while  k<7:

>>> s

Using a for Statement

As (almost) always on Python, there is more than one way to accomplish a give task. A for loop works this way.

>>> s=0
>>> for i in range(0,7):

>>> s

Here i plays the role of the dummy variable is initialized to in the range(0,7) syntax and automatically incremented.

Notice what happens if you just enter range(0,7)

>>> range(0,7)
[0, 1, 2, 3, 4, 5, 6]

Exercise: See what happens when you enter:

  • range(3,10)
  • range(3,10,2)
  • range(10,3,-1)

Introducing Lists

A Python list is very useful builtin data type. You can think of a list as a container of items placed in a specified order.

We could have built the range(7) list “by hand”

>>> L=[0,1,2,3,4,5,6]
>>> L
[0, 1, 2, 3, 4, 5, 6]
>>> type(L)
<type 'list'>

In fact, Python tells us they are the same.

>>> L==range(0,7)

Note that == asks the question as to whether the left and right hand sides are indeed equal in value. Recall that = means assign. Take a look at this assignment.

>>> L
[0, 1, 2, 3, 4, 5, 6]
>>> K=L
>>> K
[0, 1, 2, 3, 4, 5, 6]

List K matches L. Now let’s add another element to K

>>> K.append(9)
>>> K
[0, 1, 2, 3, 4, 5, 6, 9]

What will be Python’s response when you hit enter?

>>> L


Thu Jan 28 10:18:38 PST 2016