cs1022 computer programming & principles lecture 2.2 a brief introduction to python (2)

22
CS1022 Computer Programming & Principles Lecture 2.2 A brief introduction to Python (2)

Upload: maximillian-montgomery

Post on 23-Dec-2015

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS1022 Computer Programming & Principles Lecture 2.2 A brief introduction to Python (2)

CS1022 Computer Programming &

Principles

Lecture 2.2A brief introduction to Python

(2)

Page 2: CS1022 Computer Programming & Principles Lecture 2.2 A brief introduction to Python (2)

Plan of lecture• Intro to Portable Python• Integrated development environment (IDE)• Edit-run workflow• From maths to pseudo-code to Python• Codecademy• Learning to learn• What next?

2CS1022

Page 3: CS1022 Computer Programming & Principles Lecture 2.2 A brief introduction to Python (2)

Portable Python• A pre-configured Python distribution• Runs directly on Windows from a USB device• Portable, lightweight, hassle-free– No need to fiddle with class paths, access rights

• Large set of libraries bundled together– No need to get these separately

• Drawbacks:– Only for Windows

3CS1022

Page 4: CS1022 Computer Programming & Principles Lecture 2.2 A brief introduction to Python (2)

Portable Python (2)• We’ll cover the basics of the IDE– Editor and edit-run loop– Making sense of mistakes

• Here’s what you should have:

4CS1022

Double-click on“PyScripter-Portable.exe”

Page 5: CS1022 Computer Programming & Principles Lecture 2.2 A brief introduction to Python (2)

Portable Python (3)• Here’s what you should see:

5CS1022

Editor pane

Python interpreter pane

Files pane

Page 6: CS1022 Computer Programming & Principles Lecture 2.2 A brief introduction to Python (2)

Portable Python (4)• Basic edit-run loop:

1. Load a file2. Edit it (editor gives feedback)3. Run it4. If there are problems, go to 25. else stop

• What kinds of problems?– Program aborts (does not run)– Program runs but does not do what we intended

6CS1022

1 2

3

Page 7: CS1022 Computer Programming & Principles Lecture 2.2 A brief introduction to Python (2)

Let’s edit a new Python program:1. Highlight the text in 22. Delete all of it3. Type the following in 2

Editing a Python program

7CS1022

1 2

3

# absolute value

n = -20if n < 0: abs = -nelse: abs = nprint(abs)

Page 8: CS1022 Computer Programming & Principles Lecture 2.2 A brief introduction to Python (2)

To run the program we typed, we can:• Type <ctrl-F9> or click on green arrow above pane 2• Program will be run in pane 3

Running a Python program

8CS1022

Page 9: CS1022 Computer Programming & Principles Lecture 2.2 A brief introduction to Python (2)

• If we wanted to try the previous program with different values we would have to edit line 1

• Instead, we can provide parameters from keyboard

Running a Python program (2)

9CS1022

Page 10: CS1022 Computer Programming & Principles Lecture 2.2 A brief introduction to Python (2)

Let’s edit our Python program and add the following:

from sys import arg# absolute value

n = -20if n < 0: abs = -nelse: abs = nprint(abs)

Editing a Python program (revisited)

10CS1022

from sys import arg# absolute value

n = int(argv[1])if n < 0: abs = -nelse: abs = nprint(abs)

import “argv” (functionality) from module “sys”

Get first parameter and convert it into an integer

Page 11: CS1022 Computer Programming & Principles Lecture 2.2 A brief introduction to Python (2)

• “argv” allows us to read data from keyboard– Data are parameters which we assign to variables

• It is possible to write programs without parameters– We need to initialise variables in the program• “initialise” = “give initial values”

– Every time we want to try different values we need to change these

– This is tedious...

Parameters from keyboard

11CS1022

Page 12: CS1022 Computer Programming & Principles Lecture 2.2 A brief introduction to Python (2)

• We need to tell Python we want to use parameters:Parameters from keyboard (2)

12CS1022

Page 13: CS1022 Computer Programming & Principles Lecture 2.2 A brief introduction to Python (2)

• And provide the actual value(s)Parameters from keyboard (2)

13CS1022

Page 14: CS1022 Computer Programming & Principles Lecture 2.2 A brief introduction to Python (2)

• By defining a function we can pass parameters from the interpreter line:

Defining functions

14CS1022

def absolute(n):if n < 0:

abs = -n else: abs = n return(abs)

Page 15: CS1022 Computer Programming & Principles Lecture 2.2 A brief introduction to Python (2)

• In the IDE:Defining functions

15CS1022

Page 16: CS1022 Computer Programming & Principles Lecture 2.2 A brief introduction to Python (2)

• The editor helps you formatting your program– Indentations are important!

• You still need to confirm what the editor does– For instance, when the “if” command is finished

Formatting your program

16CS1022

Page 17: CS1022 Computer Programming & Principles Lecture 2.2 A brief introduction to Python (2)

Program with a “for” loop

17CS1022

# sum first n integers

n = int(argv[1])sum = 0for i in range(1,n): sum = sum + iprint(sum)

Page 18: CS1022 Computer Programming & Principles Lecture 2.2 A brief introduction to Python (2)

• Let’s write a program to count down from an input n to 0

• We must use a while-loop!

Program with a “while” loop

18CS1022

while condition: statement

Page 19: CS1022 Computer Programming & Principles Lecture 2.2 A brief introduction to Python (2)

• You would have been exposed to it by now...• It combines Python with “expected outcomes”– Compares your program with an expected solution– Errors confusing!

• Plan of topics– Gentle, comprehensive

• Try both:– IDE and Codecademy

Codecademy

19CS1022

Page 20: CS1022 Computer Programming & Principles Lecture 2.2 A brief introduction to Python (2)

• How does one learn to program computers?• How should we teach programming?• Going over syntax, pragmatics and “tricks of trade”

– Tedious, very time-consuming– Different people have different learning pace/speed– Hold back or rush people...

• Route we took:– Expose you to basics of a programming language– Support and guide you to learn it in your own pace– Use existing on-line resources (we cannot compete with them!)

• This is an opportunity to “learn to learn”– A “soft skill” for life– Know what you know; know what you don’t know (AND do something

about it!)– Help is at hand if you have difficulty (demonstrators, lecturers)

Learning to learn

20CS1022

Page 21: CS1022 Computer Programming & Principles Lecture 2.2 A brief introduction to Python (2)

• Work your way through lessons and materials on-line

• Programming is a skill you get by doing– Learn with your mistakes (and successes)– The more mistakes, the more you learn!– Breaking stuff is fun (no-one is hurt, no damages!)

What next?

21CS1022

Page 22: CS1022 Computer Programming & Principles Lecture 2.2 A brief introduction to Python (2)

• Python’s official Web sitehttps://www.python.org/

• Wikipedia’s entry on Pythonhttp://en.wikipedia.org/wiki/Python

• Codecademy on-line Python traininghttp://www.codecademy.com/en/tracks/python

• Python Humour! (who said we are not fun/funny?)https://www.python.org/doc/humor/

• Portable Pythonhttp://portablepython.com/

Further reading

22CS1022