Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d"— Presentation transcript:

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

2 The Workshops Term WeekDateLectureLecture ContentsWorkshopWorkshop Activity 611-Nov1 Introduction; Python as a calculator; Editing files None 718-Nov2 Loops and Arrays; debugging 1 Exercises: First programs 825-Nov3 Array slicing, functions and namespaces 2 Exercises: Loops and arrays 92-Dec4 Program Testing, NumPy, FileI/O 3 Exercises: Array slicing and functions 109-Dec5 Advanced techniques and Plotting graphs 4 Mini Project (Mini project due) 120-Jan- - 5 Project (Mini project marked) 227-Jan--6 Plotting graphs 33-Feb - -7 Project 410-Feb--8 Project 517-Feb--9 Project. (Project due)

3 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:

4 Running Python To run Python, type python at the command prompt: You will see the python version (2.7.5) and the python prompt: –>>>

5 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

6 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 = ax 2 + bx + c with the above values and c = 5.0 –Hint: We use x**2 for x 2

7 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

8 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:.

9 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

10 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>

11 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:

12 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

13 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 = x 2 – 1 Exercise 5.2 - Evaluate the polynomial: y = ax 2 + 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.


Download ppt "Introduction to Programming Workshop 1 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d"

Similar presentations


Ads by Google