Introduction to Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room...

13
Introduction to Programming Workshop 1 1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d [email protected] c.uk

Transcript of Introduction to Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room...

Page 1: Introduction to Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d n.a.dipper@durham.ac.uk.

Introduction to ProgrammingWorkshop 1

PHYS1101 Discovery Skills in Physics

Dr. Nigel DipperRoom [email protected]

Page 2: Introduction to Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d n.a.dipper@durham.ac.uk.

The Workshops

Term Week Date Lecture Lecture Contents Workshop Workshop Activity

6 11-Nov 1 Introduction; Python as a calculator; Editing files

None None

7 18-Nov 2 Loops and Arrays; debugging 1 Exercises: First programs

8 25-Nov 3 Array slicing, functions and namespaces

2 Exercises: Loops andarrays

9 2-Dec 4 Program Testing, NumPy, FileI/O 3 Exercises: Array slicing andfunctions

10 9-Dec 5 Advanced techniques and Plotting graphs

4 Mini Project(Mini project due)

1 20-Jan - - 5 Project(Mini project marked)

2 27-Jan - - 6 Plotting graphs

3 3-Feb - - 7 Project

4 10-Feb - - 8 Project

5 17-Feb - - 9 Project.(Project due)

Page 3: Introduction to Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d n.a.dipper@durham.ac.uk.

The Windows Terminal

• We use the Windows 7 OS – so boot into windows and log on.• We run Python from the windows terminal command line so:

– Open a terminal window– Start -> All Programmes -> Accessories -> Command Prompt– You should see this window:

Page 4: Introduction to Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d n.a.dipper@durham.ac.uk.

Running Python

• To run Python, type python at the command prompt:• You will see the python version (2.7.5) and the python prompt:

– >>>

Page 5: Introduction to Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d n.a.dipper@durham.ac.uk.

Python as a fancy calculator

• We use the command print to write results to the screen• Try these:

>>> print 1+2

3

>>> print 2*5

10

>>> print 1 / 2

0• Beware! This is integer arithmetic!

>>> print 1.0 / 2.0

0.5• This is real arithmetic

• To exit python, type CTRL d (Hold down CTRL and press d)

– Or type exit() - The brackets are because exit() is a function

– We will meet calling, and writing, functions in a later workshop

Page 6: Introduction to Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d n.a.dipper@durham.ac.uk.

Variables – Reals and Integers

• Give a variable a value and print it:

>>> length = 2.54

>>> print length

2.54• Now do some arithmetic:

>>> a = 2.0

>>> b = 3.0

>>> x = 4.0

>>> y = a*x + b

>>> print y

11.0• Exercise: Calculate y = ax2 + bx + c with the above values and c = 5.0

– Hint: We use x**2 for x2

Page 7: Introduction to Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d n.a.dipper@durham.ac.uk.

Variables – Strings

• A string or text is represented using quotes – either single or double• Thus we can hold some text in a string variable thus:

>>> name = “Nigel”

>>> print name

Nigel

Page 8: Introduction to Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d n.a.dipper@durham.ac.uk.

Calling functions

• There are some ‘built-in’ functions in Python:

>>> a = 123

>>> b = float(a)

>>> print b

123.0• However, Maths functions are NOT built-in. To add these we ‘import’

a library or module called numpy:• Try print sin(b). This will give an error! Use numpy:

>>> import numpy

>>> print numpy.sin(b)

-0.45990349069• Note the syntax: <library name>.<function name>

Page 9: Introduction to Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d n.a.dipper@durham.ac.uk.

Blocks and indentation

• Python uses indentation to define blocks of code.• The block of code starts after a line ending in a :• To demonstrate this, try some conditional tests:

>>> a = 1.23

>>> if a > 0:

… print ‘a is positive’

a is positive

>>>• Every line of the conditional block must be indented with the same

amount of whitespace – normally a single ‘tab’• The … is asking for more input• If you just hit return then Python will try to execute the code

Page 10: Introduction to Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d n.a.dipper@durham.ac.uk.

Make a new folder*

• First decide on a folder to put your work in• The current folder in your terminal window will be your ‘home’ folder• On the networked PCs, this will be in your J drive• The name of the folder is in the system prompt (EG J:\>)• Create a new folder to put your work in (call it whatever you like)

J:\> mkdir mywork• You can use the command ‘dir’ to list the files in a directory:

J:\> dir

mywork• Now move to that directory (cd is ‘change directory’)

J:\> cd mywork

J:\mywork>

Page 11: Introduction to Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d n.a.dipper@durham.ac.uk.

Using an editor - 1

• It is not practical to type more than a few lines of code at the Python prompt.

• Instead, we write many lines of code in an editor, save the code to a file then give this file to Python to execute

• CIS have installed notepad++ on Windows 7 on all networked PCs– Use this editor when using the CIS machines

• To run this, make sure you are in the folder where you want to save your work then type notepad++ at the system prompt (NOT at the Python prompt)

• The editor will by default save your work in this folder.• The editor window looks like this:

Page 12: Introduction to Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d n.a.dipper@durham.ac.uk.

Using an editor - 2

• Create your first program. This should just print the words ‘Hello world’ to the screen. This can be done in 1 line in Python

• Save the program to a file with the extension .py. EG myprog.py• Now run your program:

J:\> python myprog.py

Hello world

Page 13: Introduction to Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d n.a.dipper@durham.ac.uk.

Exercises

• Do the following exercises.• The details are in the course notes.• Solutions will be on DUO after the workshop

• Exercise 5.1 – Evaluate the polynomial y = x2 – 1• Exercise 5.2 - Evaluate the polynomial: y = ax2 + bx + c– You must show your solution for exercise 5.2 to a demonstrator before you leave.

• Exercise 5.3 – Find the square root of a number if it is positive• Exercise 5.4 – Find the sin and cos of an angle in degrees

• Homework:– Read the course notes and continue with the exercises at the end of each chapter.– Work on any CIS PC or put python on your laptop and work at home.– We recommend downloading Enthought Canopy.– This is an integrated development environment (IDE) with python, many libraries

and an editor.