Scientific Computing Using Python - PHYS:4905
Lecture #02 - Prof. Kaaret
Data structures in Python
Objects
When you write x =
17 in Python, you might have thought that you were just
putting the value 17 into a variable called x, but you are
actually creating an integer object and then making the
variable x point to that object. An object in
programming terms, is a collection of data and functions.
The object x is an integer object and has a set of
functions that are common to all integers in Python. For
example, x.bit_length()
is a function that calculates how many binary bits are needed to
write the integer and x.__hex__() is a function that prints out x in hexadecimal
or base 16 (Python uses 0x at the start of a number to indicate
hexadecimal). Floats and strings are also objects, but come
with different sets of functions more appropriate for floats and
strings.
When you execute an expression like x = x+3, Python creates a new integer
object, extracts the value from x and adds 3, assigns that
to be the value of the new integer object, and then makes the
variable x point to the new object. Integers are immutable,
once you make an integer object, you can never change its
value. You have to make a new object everytime you want a
integer with a new value.
You can check the type of an object using the type function, e.g.
type(x) should
return int.
Lists
Python has a built in object for lists. A list is an
ordered set of objects. The objects can be of any type and
mixed types, including lists. You make lists using square
brackets, L = [1.0,
1, x, 'Hi', 1+2j, [2, 1]].
You access the elements of lists using square brackets.
This is call slicing. The first element of a list is
element 0, so L[0]
returns 1.0 and L[3] returns 'Hi'.
You can get ranges or slices of elements using a colon. L[3:5]
returns ['Hi', (1+2j)]. This is a new list with two
elements. When you slice, you start with the first number in
the slice, but end with the one less than the last number.
You can also leave out numbers. L[3:] returns ['Hi', (1+2j), [2, 1]], while L[:5] returns [1.0, 1, 17, 'Hi',
(1+2j)], and L[:] returns the whole list. The latter
doesn't seem that useful for one-dimensional lists, but gets
useful when you get into more dimensions or if you want to make a
copy of a list.
Lists are mutable, meaning that you can change the
contents of the list after you make it. L[1] = 33 changes L to [1.0, 33, 17, 'Hi',
(1+2j), [2, 1]]. Changing an element of a list
doesn't make a whole new list.
Tuples
Tuples are like lists, but are made using parentheses, t = (1.0, 2, 'three').
If you want to return multiple values from a function, you use
tuples. Also, we'll use tuples when we need a few numbers
grouped together, like specifying the size of a two or three
dimensional array.
Arrays
Arrays are objects used to store a set of numbers. Arrays are
not lists. They often look like lists, but there are many
numerically oriented operations that you can do with arrays that you
cannot do with lists. Arrays are not built into Python, rather
they are defined in the NumPy library and extend Python. (One
of the reasons that Python is so popular is that it lends itself to
being extended.) To use arrays, you need to first import
NumPy. I'll assume that you do this via import numpy as np. If you
use from numpy import
*, drop the np.
in front of all the function calls below.
You can make one and two dimensional arrays using
v = np.array([1.0, 2.0,
3.0])
a = np.zeros(3)
b = np.ones((3,3))
c = np.arange(0.4, 1.8, 0.1)
r = np.random.random(10)
Note the use of a tuple in making the 2D arrays. If you
forget the extra parenthesis that make the argument of zeros into a
tuple, you'll get an error. The arange function makes an array
with evenly (linearly) spaced values. Like slicing, it starts
with the first number, but does not reach the second number. The
third number is the spacing. People sometimes prefer np.linspace.
The great thing about how NumPy extends Python to use arrays is that
you can write expressions that work on arrays as a whole without
having to worry about the individual components. For example,
to multiply each element of v by 3, just type 3*v. In more
typical programming languages such as C and C++, one needs to
explicitly write out instructions to multiply each element of the
array. NumPy also has lots (and lots and lots) of functions
that work on arrays, for example sum, mean, std (standard
deviation), min, max, median, sin, cos, tan, ...
You can find the dimensions of an array using shape(b) or b.shape. In the latter, note
that you do not use b.shape().
The shape parameters are data stored in the array object, so you
don't need a function with () to access them. You can change
the shape of an array using the reshape method. The total
number of elements in the new shape must be the same as the total
number of elements in the array.
You can slice arrays just like you do lists and you can modify
individual or groups of elements in arrays just like you do with
lists. You can also use a list to select a disjoint set of
elements in an array.
q = [2, 4, 7]
c[q]
returns array([0.6,
0.8, 1.1]).
You can use a Boolean array to pick out elements of an array that
satisfy some condition. For example
q = (c > 1.0)
c[q]
returns the elements of c with values greater than one.
Strings
Python can manipulate text in the form of strings. However,
this class is mainly devoted to numerical stuff, so the main
function that you need to know is str(), which converts a number to a
string. If you want your numbers to look nice, you need to use
the format method or the % operator, but it is better to pick one of
the two and then learn it when you have an application at hand to
focus your mind.
Assignment
The remainder of today's class will be devoted to learning the
basics of Python by working through the second chapter of the
textbook. As you read the chapter, enter the commands in the
purple highlights into Spyder and see what you get. When you
are done with the chapter, do the second assignment which will be
due at the start of the next class.
HW #2 is due at the beginning of the
next class.