Download Workshop2v0.8 - Durham University Community

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Mathematics of radio engineering wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
PHYS1101 Discovery Skills in Physics
Introduction to Programming
Workshop 2
Dr. Nigel Dipper
Room 125d
[email protected]
Part 1
• Sequences – arrays, lists etc
• ‘for’ loops – repeating code in a loop
• Indexing – into sequences
• Exercises
Sequences - 1
•
•
•
•
•
We need to hold sequences of numbers
The main sequences in Python are:
Strings
EG theName = ‘Monty Python’
Lists
EG [1, 2.34, ‘fred’]
numpy arrays EG (1.1, 2.2, 3.3) – These can be n-dimensional
Type
Index?
Modify?
Add
elements?
Mixed
types?
Tuple
Yes
No
No
Yes
String
Yes
Yes
Yes
No
List
Yes
Yes
Yes
Yes
Array
Yes
Yes
No
No
Dictionary
No
Yes
Yes
Yes
Examples of sequences
>>> myList = [1,2,3,4,5]
>>> print myList[2]
[1,2,3,4]
>>> myArray = numpy.array(myList)
>>> print myArray
[1 2 3 4]
>>> myTuple = (1,2,3)
>>> print myTuple
(1,2,3)
>>> myString = “Hello world”
>>> print myString
Hello world
‘for’ loops - 1
• If we want to do something many times, we write the code once and
put it in a loop. There are two types of loop:
• ‘while’ loops terminate on a condition.
• ‘for’ loops are told how many times to loop.
• A very common way to build a for loop is to use the built-in ‘range()’
function.
• Try this out at the python prompt. Type:
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
• It returns a list of integers starting at zero and ending at 10-1.
• The parameter sent to the range function is one more than the max.
• range() has other parameters; use help(range) to understand it.
• Exercise: Make a list starting at 1, ending at 9 in steps of 2. It should
contain: [1,3,5,7,9].
‘for’ loops – 2
• A for loop needs a list or other sequence to iterate over.
• We can use the built-in function range() to make a list.
• The for loop will execute a block of code so remember to start with a
colon and indent all of the block
• Try this from the python prompt:
>>> for i in range(10):
…
print i
…
• It should print the integers from 0 to 9. (Not 0 to 10!)
• If you want to loop over every element in an array, use the length of
the array (len() is a built-in function) as the parameter to range():
>>> a = numpy.array([2,3,4,5])
>>> for i in range(len(a)):
…
print a[i]
…
Indexing - 1
• Before we do an example with a ‘for’ loop…
• How do we put data into and get data out of an array?
• This process is called indexing. Try this:
• Create a small 1-d array called myData with 5 real numbers in it
• Change one of the values then print the array to see the change:
>>> myData[2] = 123.4
>>> print myData
• Copy one of the values to a variable and print it:
>>> x = myData[3]
>>> print x
• Note that indexing always uses square brackets [ ]
• Later we will see how to read or write larger array parts or ‘slices’.
Indexing - 2
• Indexing is very similar for arrays, lists and strings.
• Try it with a string:
>>> theName = ‘Monty Python’
>>> print theName[2]
>>> n
• Remember that indexing starts at zero!
• The data in any type of sequence are like items in pigeon holes.
• The index is the label on the pigeon hole:
theName:
M
o
n
t
y
Index:
0
1
2
3
4
5
P
y
t
h
o
n
6
7
8
9
10
11
Exercises
• Do the following exercises.
• The details are in the course notes.
• Solutions will be on DUO after the workshop
• Exercise 6.3 – Array operations
• Exercise 6.4 – Find mean of a real array
• Exercise 7.1 – Use a for loop to find squares and cubes
•
You must show your solution for exercise 7.1 to a demonstrator before you leave.
• Exercise 7.2 –Find squares and cubes using array operators
Part 2
• for loops without ‘range()’
• while loops
• More exercises…
‘for’ loops - 3
• We often use range() to generate the object for a for loop to iterate
over.
• But we don’t have to… Try this:
>>> values = [1.1, 2.3, 7.1, 4.4]
>>> for value in values:
…
print value,
1.1 2.3 7.1 4.4
• values can actually be any ‘iterable object’!
• Try it again where values is an array; arrays are iterable.
• Note: the comma after value causes print to put the data all on one
line
While loops
• We use for loops when we know how many times we want to go
round the loop.
• When we don’t know this, we use a while loop.
• Exercise: Save this program to a file and run it:
x = range(10)
index = 0
while x[index] <= 5:
index = index +1
print ‘Now x is greater than 5’
• This is a pointless example since we know beforehand when the
condition will be met; it just shows the syntax
• Note that there is no automatic increment of a loop variable as there
is in a for loop; you must increment it yourself
Exercises
• Do the following exercises.
• The details are in the course notes.
• Solutions will be on DUO after the workshop
• Finish these first!!
• Exercises: 6.3, 6.4, 7.1, 7.2
• Finish these now if you can or before the next workshop
• Exercise 7.3 – Generate Fibonacci numbers
• Exercise 7.4 – Find the golden ratio