python: introduction for absolute beginners · 3 variables if…then…else… while… loops...

436
1 Python: Introduction for Absolute Beginners Bob Dowling University Computing Service Scientific computing support email address: [email protected] These course notes: www-uxsup.csx.cam.ac.uk/courses/PythonAB/

Upload: buicong

Post on 13-Feb-2019

269 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

1

Python: Introduction for Absolute Beginners

Bob DowlingUniversity Computing Service

Scientific computing support email address:[email protected]

These course notes:www-uxsup.csx.cam.ac.uk/courses/PythonAB/

Page 2: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

2

Course outline — 1

Who uses Python?What is Python?Launching Python

Types of valueNumbersTextTruth and FalsehoodPython values

Introduction

Using Python likea calculator

Page 3: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

3

Variablesif…then…else…while… loopsCommentsListsfor… loopsFunctionsTuplesModules

Course outline — 2

Using Python likea programminglanguage

We will dolots with lists.

Page 4: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

4

Course outline — 3

Built-in modulesThe “sys” moduleReading inputFiles

Interacting withthe outside world

Storing datain programs

Dictionaries

Page 5: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

5

What is Python used for?

Network services

Web applications

GUI applications

CLI applications

Instrument control

Embedded systems

/usr/bin/command-not-found

Scientific libraries

Page 6: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

6

What is Python?

Compiled Interpreted

Fortran,C, C++

Java,.NET

Python Perl Shell

Page 7: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

7

What is Python?

Source of program?

Typed “live” Read from a file

“Interactive” “Batch” mode

Page 8: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

8

Launching Pythoninteractively ― 1Applications → Unix Shell → GNOME Terminal

Page 9: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

9

Launching Pythoninteractively ― 2

$ python

Unix prompt

Unix command Bold facemeans youtype it.

Python 2.6 …[GCC 4.3.2 …Type "help", …

Introductory blurb

>>> Python prompt

Page 10: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

10

Using Python interactively

>>> print 'Hello, world!'

Hello, world!

>>>

Python function

Function result

Python prompt

( )

Brackets

Function argument

Page 11: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

11

Using Pythoninteractively

>>> print(3)

3

>>> 5

5

Instruct Python to print a 3

Python prints a 3

Give Python a literal 5

Python evaluates and displays a 5

Page 12: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

12

Using Pythoninteractively

>>> 2 + 3

5

Give Python an equivalent to 5

Python evaluates and displays a 5

>>> 5

5

Page 13: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

13

Using Pythoninteractively

>>> print('Hello, world!')

Hello, world!

>>>

'Hello, world!'

'Hello, world!'

No quotes

Quotes

Page 14: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

14

Quitting Pythoninteractively

>>>

$

Python prompt

Unix prompt

[Ctrl]+[D] Unix “end of input”

Page 15: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

15

Exercise

1. Launch a terminal window.2. Launch Python.3. Print out “Hello, world!”4. Run these Python expressions (one per line):

(a) 42(b) 26+18(c) 26<18(d) 26>18

5. Exit Python (but not the terminal window).

2 minutes

Page 16: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

16

Writing Python scripts

Applications → Word and Text Processing → gedit

Page 17: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

17

Launching PythonscriptsRead / edit the script Run the script

gedit Terminal

Page 18: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

18

Launching Pythonscripts

$ python hello.py

Hello, world!

$

Unix prompt

No threelines of blurb

Straight backto the Unixprompt

Page 19: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

19

Launching Pythonscriptsprint(3)5

three.py

$ python three.py

3

$

No “5” !

Page 20: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

20

Interactive vs. Scripting

Source of program?

Typed “live” Read from a file

“Interactive” “Batch” mode

Introductory blurb No blurb

Evaluations printed Only explicit output

Page 21: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

21

Progress

What Python is

Who uses Python

How to run Python interactively

How to run a Python script

Page 22: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

22

Exercise

1. Launch a terminal window.2. Run hello.py as a script.3. Edit hello.py.

Change “Hello” to “Goodbye”.4. Run it again.

2 minutes

Page 23: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

23

Types of values

Numbers Whole numbers

Decimal numbers

Text

“Boolean” True

False

Page 24: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

24

Integers

{ …-2, -1, 0,1, 2, 3, … }

ZZ

Page 25: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

25

>>> 4+2

6

>>> 3

8

Addition behaves asyou might expect it to.

+ 5

Spaces aroundthe “+” are ignored.

Page 26: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

26

>>> 4-2

2

>>> 3

-2

Subtraction also behavesas you might expect it to.

- 5

Page 27: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

27

>>> 4*2

8

>>> 3

15

* 5

Multiplication uses a“*” instead of a “×”.

Page 28: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

28

>>> 4

2

/2Division uses a“/” instead of a “÷”.

>>> 5

1

/ 3

>>> -5

-2

/ 3

Division rounds down.

Strictly down.

Page 29: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

29

>>> 4

16

**2Raising to powers uses“4**2” instead of “42”.

>>> 5 ** 3 Spaces around the “**”allowed, but not within it.

125

Page 30: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

30

>>> 4

0

%2

Remainder uses a “%”.

4 = 2×2 + 0

>>> 5 % 3

2 5 = 1×3 + 2

>>> -5 % 3

1 -5 = -2×3 + 1

Always zero or positive

Page 31: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

31

>>> 2 * 2

4

>>> 4 * 4

16

>>> 16 * 16

256

>>> 256 * 256

65536

How far canintegers go?

So far, so good…

Page 32: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

32

>>> 65536 * 65536

4294967296L Long integer

>>> 4294967296 * 4294967296

18446744073709551616L

>>> 18446744073709551616 *

340282366920938463463374607431768211456L

18446744073709551616

No limit to size ofPython's integers!

Page 33: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

33

18446744073709551616

4294967296

65536

256

16

4

2

3402823669209384634…63374607431768211456

intINTEGER*4

longINTEGER*8

long longINTEGER*16

Out of the reachof C or Fortran!

Page 34: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

34

Progress

Whole numbers

No support for fractions

Unlimited range of values

Mathematical operations

a+b a-b a×b a÷b ab a mod b

a+b a-b a*b a/b a**b a%b

…-2, -1, 0, 1, 2…

1/2 0

Maths:Python:

Page 35: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

35

Exercise

In Python, calculate:

2 minutes

1. 12+4 2. 12+53. 12−4 4. 12−55. 12×4 6. 12×57. 12÷4 7. 12÷59. 124 10. 125

Which of these answers is “wrong”?

Page 36: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

36

Floating point numbers

1.01.251.5

11 ¼1 ½

Page 37: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

37

But… IR1.31 1 3

1.331.3331.3333 ?

Page 38: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

38

>>> 1.0

1.0

>>> 0.5

0.5

>>> 0.25

0.25

>>> 0.1

0.1

1 is OK

½ is OK

¼ is OK

Powersof two.

1/10 is not!

Why?

Page 39: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

39

>>> 0.1

0.1

1/10 is storedinaccurately.

Floating point numbers are…

…printed in decimal

…stored in binary17 significant figures

>>> 0.1 + 0.1 + 0.1

0.30000000000000004

Page 40: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

40

If you are relying on the17th decimal place youare doing it wrong!

!>>> 0.1 + 0.1 + 0.1

0.30000000000000004

Page 41: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

41

>>> 5.0 + 2.0

7.0

>>> 5.0 − 2.0

3.0

>>> 5.0 * 2.0

10.0

>>> 5.0 / 2.0

2.5

>>> 5.0 ** 2.0

25.0

>>> 5.0 % 2.0

1.0

Same basic operations

Gets it right!

Page 42: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

42

>>> 4.0 * 4.0

16.0

>>> 16.0 * 16.0

256.0

>>> 256.0 * 256.0

65536.0

How far canfloating pointnumbers go?

So far, so good…

>>> 65536.0 * 65536.0

4294967296.0

Page 43: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

43

>>> 4294967296.0 ** 2

1.8446744073709552e+19

17 significant figures ×1019

1.8446744073709552×1019 =18,446,744,073,709,552,000Approximate answer

4294967296 × 4294967296 =18,446,744,073,709,551,616Exact answer

384Difference

Page 44: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

44

>>> 4294967296.0 * 4294967296.01.8446744073709552e+19

>>> 1.8446744073709552e+19 *1.8446744073709552e+19

3.4028236692093846e+38

>>> 3.4028236692093846e+38 *3.4028236692093846e+38

1.157920892373162e+77

>>> 1.157920892373162e+77 *1.157920892373162e+77

1.3407807929942597e+154

Page 45: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

45

>>> 1.3407807929942597e+154 *1.3407807929942597e+154

“Overflow errors”

inf Floating point infinity

Page 46: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

46

Floating point limits

1.2345678901234567 x 10N

17 significant figures

-325 < N < 308

4.94065645841e-324 < x < 8.98846567431e+307Positive values:

Page 47: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

47

Progress

Floating Point numbers

Limited accuracy

Limited range of sizes

Mathematical operations

(but typically good enough)

1.25

1.25×105

1.25

1.25e5

a+b a-b a×b a÷b ab

a+b a-b a*b a/b a**b

Page 48: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

48

Exercise

In Python, calculate:

3 minutes

1. 12·0+4.0 2. 12·0-4·03. 12·0÷4.0 4. 12÷40·05. 25·00·5 6. 5·0-1·0

7. 1·0×1020 + 2·0×1010 8. 1·5×1020 + 1·0

Which of these answers is “wrong”?

Page 49: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

49

Strings

“The cat sat on the mat.”

“Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec at purus sed magna aliquet dignissim. In rutrum libero non turpis. Fusce tempor, nulla sit amet pellentesque feugiat, nibh quam dapibus dui, sit amet ultrices enim odio nec ipsum. Etiam luctus purus vehicula erat. Duis tortor lorem, commodo eu, sodales a, semper id, diam. Praesent ...”

Page 50: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

50

>>> '

'Hello, world!'

'Hello, world!

The value ofthe text object

Quotes: Hey,this is text!

>>> How Pythonrepresents thetext object.

Quotes

Page 51: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

51

Why do we need quotes?

3

print

It’s a number

Is it a command?

Is it a string?

'print' It’s a string

?

print It’s a command

Page 52: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

52

>>> '

Hello, world!

'Hello, world!print

Python command

“This is text.”

The text.

>>>

print only outputs thevalue of the text

)(

Page 53: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

53

>>>

'Hello, world!'

Quotes: Hey,this is text!

>>> Single quotes

" "Hello, world!

Double quotes

Page 54: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

54

Singlequotes

Doublequotes

' 'Hello, world! " "Hello, world!

Both define thesame text object.

Page 55: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

55

>>>

Mixed quotes

'He said "Hello" to her.'print

He said "Hello" to her.

>>> "He said 'Hello' to her."print

He said 'Hello' to her.

Page 56: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

56

Joining strings together

>>> 'He said' + 'something.'

'He saidsomething.'

>>> 'He said ' + 'something.'

'He said something.'

Page 57: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

57

Repeated text

>>> 'Bang! '

'Bang! Bang! Bang! '

* 3

>>> 'Bang! '

'Bang! Bang! Bang! '

*3

Page 58: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

58

Progress

Strings

Use quotes to identify

Use print to output just the value

(matching single or double)

String operations

Page 59: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

59

Exercise

Predict what interactive Python will print when youtype the following expressions. Then check.

3 minutes

1. 'Hello, ' + "world!"2. 'Hello!' * 33. "" * 100000000004. '4' + '2'

(That's two adjacent double quote signs.)

Page 60: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

60

Line breaks

Problem: Suppose we want to create astring that spans several lines.

>>> print('Hello,world!')

✘>>> print('Hello,SyntaxError:string literal

EOL while scanning

“end of line”

Page 61: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

61

\n

The line break character

Solution: Provide some other way to mark“line break goes here”.

>>> print('Hello, world!')

Hello,world!

\n new line

Page 62: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

62

The line break character

'Hello,

H ↵

72 101108108111 44 119108109101100 3310

e l l o , w o r l d !

world!'\n

A single character

Page 63: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

63

Special characters

\a

\n

\t

\'

\"

\\

'

"

\

Page 64: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

64

\n

“Long” strings

>>> '''world!

Hello,''' Three single quote signs

'Hello, world!' An ordinary string

An embeddednew line character

>>>

Page 65: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

65

What the string is vs.how the string prints

'Hello,\nworld!' Hello,world!

It's not just quotes vs. no quotes!

Page 66: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

66

Single or double quotes

>>> """world!

Hello,""" Three single quote signs

'Hello,\nworld!' The same string

>>>

Page 67: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

67

'''Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec at purus sed magna aliquet dignissim. In rutrum libero non turpis. Fusce tempor, nulla sit amet pellentesque feugi at, nibhquam dapibus dui, sit amet ultrices enim odio nec ipsum. Etiam luctus purus vehicula erat. Duis tortor lorem, commodo eu, sodales a, semper id, diam.'''

Long strings

Page 68: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

68

Progress

Entering arbitrarily long strings

Dealing with line breaks

Other “special” characters

Triple quotes

″″″…″″″

′′′…′′′

\n \t …

Page 69: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

69

Exercise

Predict the results of the following instructions.Then check.

print('Goodbye, world!')1.

print('Goodbye,\nworld!')2.

print('Goodbye,\tworld!')3.

2 minutes

Page 70: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

70

Comparisons

Are two values the same?

Is one bigger than the other?

Is “bigger” even meaningful?

5+2 7

5+2 8

Page 71: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

71

Comparisons

>>> 5 > 4

>>> 5.0

True

False

A comparison operation

A comparison result

Only two values possible

4.0<

Page 72: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

72

Equality comparison

>>> 5 == 4

False

n.b. double equals

Page 73: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

73

Useful comparisons

>>> (2**2)**2 == 2**(2**2)

True

>>> (3**3)**3 == 3**(3**3)

False

Page 74: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

74

All numerical comparisons

x y==

x y!=

x y<

x y<=

x y>

x y>=

Python

x y=

x y≠

x y<

x y≤

x y>

x y≥

Mathematics

Page 75: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

75

Comparing strings

Alphabetic order…

>>> 'cat' < 'mat'

True

>>> 'bad' < 'bud'

True

>>> 'cat' < 'cathode'

True

Page 76: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

76

Comparing strings

>>> 'Cat' < 'cat'

True

>>> 'Fat' < 'cat'

True

ABCDEFGHIJKLMNOPQRSTUVWXYZ…abcdefghijklmnopqrstuvwxyz

Page 77: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

77

Progress

Six comparisons:

Numbers:numerical order

Strings:alphabetical order

== != < <= > >=

= ≠ < ≤ > ≥

0 1 2 3-1-2-3

ABCDEFGHIJKLMNOPQRSTUVWXYZ…abcdefghijklmnopqrstuvwxyz

Page 78: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

78

Exercise

3 minutes

Predict whether Python will print True or Falsewhen you type the following expressions.Then check.

1. 100 < 1002. 3*45 <= 34*53. 'One' < 'Zero'4. 1 < 2.05. 0 < 1/106. 0.0 < 1.0/10.0

Page 79: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

79

Truth and Falsehood

“Boolean” values

True Falseand

Same status as numbers, strings, etc.

5 + 4 9

5 > 4 True

Whole number

Boolean

Page 80: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

80

Combining booleans

5 < 61 < 2>>>

True True

True

and

Both True

5 > 61 < 2>>>

True False

False

and

Not both True

Page 81: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

81

Combining booleans

5 < 61 < 2>>>

True True

True

or

Either True

5 > 61 < 2>>>

True False

True

or

Either True

Page 82: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

82

Combining booleans

5 > 61 > 2>>>

False False

False

or

Neither True

Page 83: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

83

Negating booleans

1 > 2>>>

False

not>>>

True

1 > 2

True → FalseFalse → True

not>>>

True

False

Page 84: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

84

Not equal to…

1 == 2>>>

False

1 != 2>>>

True

not 1 == 2>>>

True

Page 85: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

85

Progress

“Booleans” True False

Combination and or

Negation not

Page 86: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

86

Exercise

3 minutes

Predict whether Python will print True or Falsewhen you type the following expressions.Then check.

1. 1 > 2 or 2 > 12. 1 > 2 or not 2 > 13. not True4. 1 > 2 or True

Exercise

Page 87: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

87

Ambiguity?

12 + 8 / 4

12 + (8/4) (12 + 8)/4

12 + 2 20/4

14 5

?

Page 88: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

88

Standard interpretation

12 + 8 / 4

12 + (8/4) (12 + 8)/4

12 + 2 20/4

14 5

✓ ✗

Page 89: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

89

Division before addition

12 + 8 / 4 Initial expression

12 + 8 / 4 Do division first

12 + 2

12 + 2 Do addition second

14

Page 90: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

90

Precedence

Division before addition

An order of execution

“Order of precedence”

Page 91: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

91

Precedence

** % / * - +

First

Arithmetic

== != >= > <= < Comparison

not and or Logical

Last

Page 92: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

92

Parentheses

Avoid confusion!

18/3*3 “Check the precedence rules”

18/(3*3) “Ah, yes!”

Page 93: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

93

Exercise

2 minutes

Predict what Python will print whenyou type the following expressions.Then check.

1. 12 / 3 * 42. 3 > 4 and 1 > 2 or 2 > 1

Page 94: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

94

Exercise: √2 by “bisection”

Page 95: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

95

Exercise: √2 by “bisection”

>>> (1.0+2.0)/2.01.5

>>> 1.5**22.25

Page 96: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

96

Exercise: √2 by “bisection”

>>> 2.25 > 2.0True

Page 97: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

97

Exercise: √2 by “bisection”

>>> (1.0+1.5)/2.01.25

>>> 1.25**21.5625

Page 98: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

98

Exercise: √2 by “bisection”

>>> 1.5625 > 2.0False

Page 99: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

99

Exercise: √2 by “bisection”

>>> (1.25+1.5)/2.01.375

>>> 1.375**21.890625

Page 100: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

100

Exercise: √2 by “bisection”

>>> 1.890625 > 2.0False

Page 101: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

101

Exercise: √2 by “bisection”

10 minutes

Three more iterations, please.

Page 102: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

102

So far …

…using Pythonas a calculator.

Page 103: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

103

Now …

…use Pythonas a computer.

Page 104: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

104

int

How Python stores values

42

Identification ofthe value’s type

Identification ofthe specific value

42

Lump of computer memory

Page 105: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

105

How Python stores values42

4·2×101

'Forty two'

int 42

float 4.2 ×101

str F o r t y t w o

True bool ✓

Page 106: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

106

Variables Attaching a name to a value.

>>> 40 + 2

42

An expression

The expression’s value

>>> answer = 42 Attaching the nameanswer to the value 42.

>>> answer The name given

42 The attached value returned

Page 107: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

107

Variables

>>> answer 42=

The name being attached

A single equals sign

The value being named

No quotes

Page 108: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

108

Equals signs

==

=

Comparison:“are these equal?”

Assignment: “attach the name on the left tothe value on the right”

Page 109: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

109

>>> answer 42=

Order of activity

1. Right hand side isevaluated.

2. Left hand side specifiesthe attached name.

int 42answer

variables

Page 110: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

110

Example ― 1

>>> answer 42=

>>> answer

42

>>>

Simple value

Page 111: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

111

Example ― 2

>>> answer 44 - 2=

>>> answer

42

>>>

Calculated value

Page 112: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

112

Example ― 3

>>> answer answer=

>>> answer

40

>>>

>>> answer 42=

>>> answer

42

- 2

“Old” value

“New” value

Reattaching thename to adifferent value.

Page 113: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

113

Example ― 3 in detailanswer answer - 2= R.H.S. processed 1st

answer 42= - 2 Old value used in R.H.S.

answer 40= R.H.S. evaluated

L.H.S. processed 2nd

answer = 40

answer = 40

L.H.S. name attachedto value

Page 114: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

114

Using named variables ― 1

>>> upper = 2.0

>>> lower = 1.0

Page 115: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

115

Using named variables ― 2

>>> middle = (upper+ lower)/2.0

>>> middle

1.5

Page 116: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

116

Using named variables ― 3

>>> middle**2 > 2.0

True

>>> upper = middle

Page 117: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

117

Using named variables ― 4

>>> middle = (upper+ lower)/2.0

>>> middle**2 > 2.0

False

>>> lower = middle

>>> middle

1.25

Page 118: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

118

Using named variables ― 5

>>> middle = (upper+ lower)/2.0

>>> middle**2 > 2.0

False

>>> lower = middle

>>> middle

1.375

Page 119: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

119

upper = 2.0lower = 1.0

middle = (upper + lower)/2.0

middle**2 > 2.0?

lower = middleupper = middle

FalseTrue

print(middle)

Page 120: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

120

Homework: √3 by bisection

5 minutes

Three iterations, please.

Print middle at the end of each stage:

Start withupper = 3.0lower = 1.0

Test withmiddle**2 > 3.0

print(middle)

Page 121: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

121

Still not acomputerprogram!

Page 122: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

122

if … then … else …upper = 2.0lower = 1.0

middle = (upper + lower)/2.0

middle**2 > 2.0?

lower = middleupper = middle

FalseTrue

print(middle)

Page 123: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

123

if … then … else …middle**2 > 2.0

lower = middle

upper = middlethen

if

else

Page 124: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

124

if

else

middle**2 > 2.0

lower = middle

upper = middle

:keyword

condition

colon

“True” action

“False” action

keyword

indentation

indentation

: colon

Page 125: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

125

Example script:lower = 1.0upper = 2.0middle = (lower+upper)/2.0

if middle**2 > 2.0 :

print('Moving upper')upper = middle

print('Moving lower')lower = middle

else :

print(lower)print(upper)

middle1.py

Page 126: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

126

Example script: beforelower = 1.0upper = 2.0middle = (lower+upper)/2.0

if middle**2 > 2.0 :

print('Moving upper')upper = middle

print('Moving lower')lower = middle

Set-up priorto the test.

else :

print(lower)print(upper)

Page 127: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

127

Example script: if…lower = 1.0upper = 2.0middle = (lower+upper)/2.0

if middle**2 > 2.0 :

print('Moving upper')upper = middle

print('Moving lower')lower = middle

colon

condition

keyword: “if”

else :

print(lower)print(upper)

Page 128: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

128

Example script: then…lower = 1.0upper = 2.0middle = (lower+upper)/2.0

if middle**2 > 2.0 :

print('Moving upper')upper = middle

print('Moving lower')lower = middle

Four spaces’indentation

The “True”instructions

else :

print(lower)print(upper)

Page 129: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

129

Example script: else…lower = 1.0upper = 2.0middle = (lower+upper)/2.0

if middle**2 > 2.0 :

print('Moving upper')upper = middle

else

print('Moving lower')lower = middle

keyword: “else”

colon

Four spaces’indentation

The “False”instructions

:

print(lower)print(upper)

Page 130: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

130

Example script: afterlower = 1.0upper = 2.0middle = (lower+upper)/2.0

if middle**2 > 2.0 :

print('Moving upper')upper = middle

print('Moving lower')lower = middle Not indented

Run regardlessof the test result.

else :

print(lower)print(upper)

Page 131: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

131

Example script: running itlower = 1.0upper = 2.0middle = (lower+upper)/2.0

if middle**2 > 2.0 :

print('Moving upper')upper = middle

print('Moving lower')lower = middle

else :

print(lower)print(upper)

$ python middle1.py

Moving upper1.01.5

Unix prompt

$

Page 132: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

132

Progress

Run a test

Do somethingif it succeeds.

Do somethingelse if it fails.

Colon…Indentation

if test :something

else :something else

test

somethingsomething

else

True False

Page 133: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

133

Exercise

Four short Python scripts:

ifthenelse1.py

ifthenelse2.py

ifthenelse3.py

ifthenelse4.py

1. Read the file.

2. Predict what it will do.

3. Run it.

5 minutes

Page 134: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

134

upper = 2.0lower = 1.0

middle = (upper + lower)/2.0

middle**2 > 2.0?

lower = middleupper = middle

FalseTrue

print(middle)

looping

Page 135: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

135

Repeating ourselves

Run the script

Read the results

Edit the scriptNot the way to do it!

Page 136: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

136

Repeating ourselves

Looping for ever?

Keep going while…

…then stop.

while condition :action

1

action2

afterwards

Page 137: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

137

while vs. until

Repeat until…

number == 0 number != 0

Repeat while…

upper - lower < target upper - lower >= target

condition not condition

Page 138: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

138

Example script

number = 1limit = 1000

while :number < limit

print('Finished!')

doubler1.py

print(number)number = number * 2

Page 139: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

139

Example script: before

number = 1limit = 1000

while :number < limit

print('Finished!')

doubler1.py

Set-up priorto the loop.

print(number)number = number * 2

Page 140: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

140

Example script: while…

number = 1limit = 1000

while :number < limit

print('Finished!')

doubler1.py

keyword: “while”

condition

colon

print(number)number = number * 2

Page 141: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

141

Example script: loop body

number = 1limit = 1000

while :number < limit

print(number)number = number * 2

print('Finished!')

doubler1.py

Four spaces’indentation

loop body

Page 142: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

142

Example script: after

number = 1limit = 1000

while :number < limit

print(number)number = number * 2

print('Finished!')

doubler1.py

Not indented

Run after the loopingis finished.

Page 143: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

143

Example script: running it

number = 1limit = 1000

while :number < limit

print(number)number = number * 2

print('Finished!')

> python doubler1.py

1248163264128256512Finished!

Page 144: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

144

Progress

Run a test

Do somethingif it succeeds.

Go back to the test.

Finish ifit fails.

while test :something

test

something

True False

Page 145: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

145

ExerciseFour short Python scripts:

while1.py

while2.py

while3.py

while4.py

1. Read the file.

2. Predict what it will do.

3. Run it.

n.b. [Ctrl]+[C] will killa script that won’tstop on its own.

5 minutes

Page 146: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

146

upper = 2.0lower = 1.0

middle = (upper + lower)/2.0

middle**2 > 2.0?

lower = middleupper = middle

FalseTrue

print(middle)

while…

if…then…else…

Page 147: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

147

Combining while… and if…

if…then…else… inside while…

Each if…then…else… improves the approximation

How many if…then…else… repeats should we do?

What’s the while… test?

Page 148: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

148

Writing the while… test

Each if…then…else… improves the approximation

How much do we want it improved?

How small do we want the interval?

upper - lower

Page 149: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

149

Writing the while… test

What is the interval? upper - lower

Keep going while the interval is too big:

while upper - lower > 1.0e-15 :

How small do we want the interval? 1.0e-15

Page 150: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

150

lower = 1.0upper = 2.0

while upper - lower > 1.0e-15

print(middle)

:

middle = (upper+lower)/2.0

?

approximationis too coarse

Single indentation

if…then…else…

No indentation

Page 151: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

151

lower = 1.0upper = 2.0

while upper - lower > 1.0e-15

print(middle)

:

middle = (upper+lower)/2.0

if middle**2 > 2.0: print('Moving upper') upper = middleelse: print('Moving lower') lower = middle

Double indentation

Double indentation

Page 152: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

152

Running the script> python root2.pylower = 1.0

upper = 2.0

while upper - lower > 1.0e-15

print(middle)

:

middle = (upper+lower)/2.0

if middle**2 > 2.0

else

print('Moving upper')upper = middle

print('Moving lower')lower = middle

:

:

Moving upperMoving lowerMoving lowerMoving upper…Moving upperMoving upperMoving lowerMoving lower1.41421356237

Page 153: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

153

Indentationc.f.

§5(b)(ii)

“legalese”

Other languages…

{…}

IF…END IF

if…fi, do…done

Page 154: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

154

lower = 1.0upper = 2.0

while upper - lower > 1.0e-15

print(middle)

:

middle = (upper+lower)/2.0

Indentation: level 1

Colon startsthe block

Indentationmarks theextent ofthe block.

Unindented lineEnd of block

if middle**2 > 2.0 else

:

:

print('Moving upper')upper = middle

print('Moving lower')lower = middle

Page 155: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

155

lower = 1.0upper = 2.0

while upper - lower > 1.0e-15

print(middle)

:

middle = (upper+lower)/2.0

if middle**2 > 2.0

Indentation: level 2

else

print('Moving upper')upper = middle

print('Moving lower')lower = middle

Colon…indentation

Colon…indentation

“else” unindented

:

:

Page 156: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

156

Arbitrary nesting

if… inside while…

while… inside if…

if… inside if…

while… inside while…

Not just two levels deep

As deep as you want

Any combination

Page 157: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

157

e.g. if… inside if…number = 20

if number % 2 == 0: if number % 3 == 0: print('Number divisible by six') else: print('Number divisible by two but not three')else: if number % 3 == 0: print('Number divisible by three but not two') else: print('Number indivisible by two or three')

Page 158: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

158

Progress

Nested constructs

Levels of indentation

Indented blocks

colon…indentation

Page 159: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

159

ExerciseWrite a script from scratch: collatz.py

1. Start with number set to 7.

2. Repeat until number is 1.

3. Each loop:

3a. If number is even, change it to number/2.

3b. If number is odd, change it to 3*number+1.

3c. Print number.15 minutes

Page 160: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

160

CommentsReading Python syntax

“What does the code do?”

“Why does the code do that?”

middle = (upper + lower)/2.0

Calculate the mid-point.

Need to know the square of themid-point’s value to compare itwith 2.0 whose root we’re after.

Page 161: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

161

Comments

# The “hash” character. “sharp”

“pound”

a.k.a.

“number”

#Lines starting with “#” are ignored

Partial lines too.

Page 162: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

162

Comments — explanation# Set the initial bounds of the interval. Then# refine it by a factor of two each iteration by # looking at the square of the value of the # interval’s mid-point.

# Terminate when the interval is 1.0e-15 wide.

lower = 1.0 # Initial bounds.upper = 2.0

while upper - lower < 1.0e-15 : …

Page 163: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

163

Comments — authorship# (c) Bob Dowling, 2010# Released under the FSF GPL v3

# Set the initial bounds of the interval. Then# refine it by a factor of two each iteration by # looking at the square of the value of the # interval’s mid-point.

# Terminate when the interval is 1.0e-15 wide.

lower = 1.0 # Initial bounds.upper = 2.0 …

Page 164: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

164

Comments — source control# (c) Bob Dowling, 2010# Released under the FSF GPL v3

# $Id: root2.py,v 1.1 2010/05/20 10:43:43 rjd4 $

# Set the initial bounds of the interval. Then# refine it by a factor of two each iteration by # looking at the square of the value of the # interval’s mid-point.

# Terminate when the interval is 1.0e-15 wide. …

Page 165: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

165

Comments — logging# (c) Bob Dowling, 2010# Released under the FSF GPL v3

# $Id: root2.py,v 1.2 2010/05/20 10:46:46 rjd4 $

# $Log: root2.py,v $# Revision 1.2 2010/05/20 10:46:46 rjd4# Removed intermediate print lines.#

# Set the initial bounds of the interval. Then# refine it by a factor of two each iteration by # …

Page 166: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

166

Comments

Reading someoneelse’s code.

Reading your own code six months later.

Writing code forsomeone else.

Writing code youcan come back to.

Page 167: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

167

Exercise1. Comment your script: collatz.py

Purpose

Author

Date

# This script# illustrates …

# Bob Dowling

# 2010-05-20

3 minutes

2. Then check it still works!

Page 168: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

168

Lists

['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

[2, 3, 5, 7, 11, 13, 17, 19]

[0.0, 1.5707963267948966, 3.1415926535897931]

Page 169: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

169

Lists — getting it wrong

A script that prints the names of thechemical elements in atomic number order.

print('hydrogen')print('helium')print('lithium')print('beryllium')print('boron')print('carbon')print('nitrogen')print('oxygen')…

Repetition of “print”

Inflexible

Page 170: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

170

Lists — getting it right

A script that prints the names of thechemical elements in atomic number order.

1. Create a list of the element names

2. Print each entry in the list

Page 171: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

171

Creating a list

>>> [ 1, 2, 3 ]

[1, 2, 3]

>>> numbers = [ 1, 2, 3 ]

>>> numbers

[1, 2, 3]

Here’s a list

Yes, that’s a list

Attaching a nameto a variable.

Using the name

Page 172: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

172

Anatomy of a list

[ ]′delta′,′gamma′,′beta′,′alpha′

Square brackets at end

Elements separated by commas

Individual element

Page 173: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

173

Square brackets in Python

[…] Defining literal lists

primes = [2,3,5,7,11,13,17,19]

e.g.

>>>

Page 174: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

174

Order of elements

No “reordering”

>>> [ 1, 2, 3 ]

[1, 2, 3]

>>> [ 3, 2, 1 ]

[3, 2, 1]

>>> [ ′a′, ′b′ ]

[′a′, ′b′]

>>> [ ′b′, ′a′ ]

[′b′, ′a′]

Page 175: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

175

Repetition

No “uniqueness”

>>> [ 1, 2, 3, 1, 2, 3 ]

[1, 2, 3, 1, 2, 3]

>>> [ ′a′, ′b′, 'b', 'c' ]

[′a′, ′b′, 'b', 'c']

Page 176: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

176

Concatenation — 1

>>> [ 1, 2, 3 ] [ 4, 5, 6, 7 ]+

[1, 2, 3, 4, 5, 6, 7]

“+” used to join lists.

>>> [′alpha′,′beta′] + [′gamma′]>>>>>>

[′alpha′, ′beta′, ′gamma′]

>>>>>>>>>

Page 177: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

177

Concatenation — 2

>>> [ 1, 2,

[1, 2,

3 ] + [ 3, 4, 5, 6, 7 ]

“3” appearstwice

3, 3, 4, 5, 6, 7]

“3” appearstwice

Page 178: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

178

Empty list

>>> []

[]

>>> [2,3,5,7,11,13] + []

[2, 3, 5, 7, 11, 13]

>>> [] + []

[]

Page 179: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

179

ProgressLists

Shown with square brackets

Concatenation

Empty list

[23, 29, 31, 37, 41]

Elements separated by commas

[23, 29]+[ 31, 37, 41]

[ ]

Page 180: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

180

Exercise

2 minutes

Predict what these will create. Then check.

1. [] + ['a', 'b'] + []

2. ['c', 'd'] + ['a', 'b']

3. [2, 3, 5, 7] + [7, 11, 13, 17, 19]

Page 181: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

181

How long is the list?

>>> len )[10, 20, 30](

3

Function name

Round brackets

Page 182: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

182

How long is a string?

>>> len )(′Hello, world!′

13

Quotes say “this is a string”.

They are not part of the string.

Same function

!Recall:

Page 183: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

183

How long is a number?

>>> len )(42

Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError:object of type 'int' has no len()

Error message

Numbers don’thave a “length”.

Page 184: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

184

Our first look at a function

>>> len )[10, 20, 30](

Function name

Round brackets

Function “argument”

One argument

3

“Returned” value

Page 185: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

185

Progress

Length of a list:

Length of a string:

Number of elements

Number of characters

Length function: len()

Page 186: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

186

Exercise: lengths of strings1. Predict what these Python snippets will return.

(a)

(b)

(c)

len(′Goodbye, ′ + ′world!′)

len(′Goodbye, world!′)

2. Then try them.

3 minutesfor both

len(′Goodbye, ′) + len(′world!′)

Page 187: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

187

Exercise: lengths of lists1. Predict what these Python snippets will return.

(d)

(e)

len([′Goodbye, world!′])

len([′Goodbye, ′] + [′world!′])

2. Then try them.

3 minutesfor both

(f) len([′Goodbye, ′]) + len([′world!′])

Page 188: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

188

list

Picking elements from a list>>> letters = [′a′, ′b′, ′c′, ′d′]

str a

str b

str c

str d

letters

variables

Page 189: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

189

list

The first element in a list>>> letters[

str

str a

b

str c

str d

letters

variables

]0 Count from zero

letters[0]

′a′ “Index”

Page 190: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

190

Square brackets in Python

[…] Defining literal lists

numbers[N] Indexing into a list

primes = [2,3,5,7,11,13,17,19]

e.g.

primes[0]

>>>

>>>

2

Page 191: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

191

list

“Element number 2”>>>

str

str a

b

str c

str d

letters

variables

letters[0]

′c′

letters[1]

letters[2]

letters[3]

letters[ ]2

The third element

Page 192: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

192

list

Going off the end>>>

str

str a

b

str c

str d

letters

variables

Traceback (most recent call last): File "<stdin>", line 1, in <module>

letters[4]

letters[ ]4

IndexError: list index out of range

Page 193: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

193

list

Maximum index vs. length>>>

str

str a

b

str c

str d

letters

variables

len(letters)

4

letters[0]

letters[1]

letters[2]

letters[3]

Maximumindex is 3!

4

Page 194: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

194

“Element number -1 !”>>>

str

str a

b

str c

str d

letters[0]

′d′

letters[1]

letters[2]

letters[3]

letters[ ]-1

The final element

letters[-1]

Page 195: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

195

Negative indices>>>

str

str a

b

str c

str d

letters[0]

′b′

letters[1]

letters[2]

letters[3]

letters[ ]-3

letters[-1]

letters[-2]

letters[-3]

letters[-4]

Page 196: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

196

Going off the end>>>Traceback (most recent call last): File "<stdin>", line 1, in <module>

letters[ ]-5

IndexError: list index out of range

Page 197: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

197

Valid range of indices>>> len(letters)

4

str

str a

b

str c

str d

letters[0]

letters[1]

letters[2]

letters[3]letters[-1]

letters[-2]

letters[-3]

letters[-4]

-4 -3 -2 -1 0 1 2 3

Page 198: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

198

Indexing into literal lists>>> letters = [′a′, ′b′, ′c′, ′d′]

>>> letters

'd'

Legal, but rarely useful:

>>> [′a′, ′b′, ′c′, ′d′]

'd'

[3]

[3]

Name of list

Literal list

Index

Index

Page 199: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

199

Assigning list elements>>> letters

[′a′, ′b′, ′c′, ′d′]

>>> letters[2]

>>> letters

[′a′, ′b′,

'X'=

The name attached tothe list as a whole

The name attached toone element of the list

Assign a new value

The new value

, ′d′]′X′

Page 200: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

200

list[ 0]

Progress

Index into a list

Counting from zero

Negative index

Assignment

list[index]

list[-1]

list[ 1] = 'A'

Square brackets for index

['x','y','z']

'x'

'z'

Page 201: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

201

Exercise

3 minutes

data = ['alpha','beta','gamma','delta']

data[-2]

data[2] = 'gimmel'

data[1]

Predict the output from the following five commands.Then try them.

data

Page 202: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

202

Doing something with a list

A script that prints the names of thechemical elements in atomic number order.

1. Create a list of the element names

2. Print each entry in the list

Recall our challenge:

Page 203: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

203

Each list elementGiven a list

Start with the first element of the list

Do something with that element

Are there any elements left?

Move on to the next element

Finish

Page 204: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

204

Do that elementwithsomething

Need to identify“current” element

Need to identify ablock of commands

Another indented block

Each list element

Page 205: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

205

for

print('Have a letter:')print( )letter

:['a','b','c']inletter

keyword keyword colon

Loopvariable

List

Repeatedblock

Using theloop variable

Indentation

The “for loop”

Page 206: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

206

The “for loop”for letter in ['a','b','c']: print('Have a letter:') print(letter)print('Finished!')

for1.py

$ python for1.py

Have a letter:aHave a letter:bHave a letter:cFinished!

Page 207: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

207

Progress

The “for…” loop

Processing each element of a list

for item in items : …item…

Page 208: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

208

Exercise

10 minutes

Complete the script elements1.py

Print out the name of every element.

Page 209: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

209

[1:5]

[1]

“Slices” of a list>>> abc = [′a′,′b′,′c′,′d′,′e′,′f′,′g′]

>>> abc

'b'

>>> abc

['b','c','d','e']

Simple index

Slice index

Single element

A new list “Slice”

Page 210: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

210

✗[

Slice limits

>>> abc ]5:1

“from” index

“to” index

][ 'e','c','d','b'

abc[1] abc[4]

'f'

abc[5] Element 5not in slice

Page 211: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

211

Slice feature

[abc ]3:1 [abc ]5:3

['b','c'] ['d','e']

Same

+

+

['b','c','d','e']

[abc ]5:1

Page 212: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

212

Open-ended slices>>> abc = [′a′,′b′,′c′,′d′,′e′,′f′,′g′]

>>> abc[

[

>>>

[′a′,′b′,′c′,′d′,

]3:

abc[ ]:5

Open ended at the end

Open ended at the start

,′e′,′f′,′g′]′d′abc[3]

]′e′abc[4]

Page 213: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

213

Open-ended slices>>> abc = [′a′,′b′,′c′,′d′,′e′,′f′,′g′]

>>> abc[

[′a′,′b′,′c′,′d′,′e′,′f′,′g′]

]: Open ended at both ends

Page 214: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

214

Progress

Slices

data[m:n] [ data[m], … data[n-1] ]

data[m:n]

data[:n]

data[m:]

data[:]

Page 215: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

215

Square brackets in Python

[…] Defining literal lists

numbers[N] Indexing into a list

numbers[M:N] Slices

Page 216: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

216

Modifying lists ― recap

>>> abc

>>>

[′a′,′b′,

abc

>>> abc[2] 'X'=

,′d′,′e′,′f′,′g′]′c′

[′a′,′b′, ,′d′,′e′,′f′,′g′]′X′

abc[2]

New value

Changed

Page 217: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

217

Modifying vs. replacing ?

>>> xyz = ['x','y']

>>> xyz[0] = 'A'

>>> xyz[1] = 'B'

>>> xyz

['A','B']

Modifying the list

>>> xyz = ['A','B']

Replacing the list

Page 218: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

218

What's the difference? — 1a

liststr

str x

yxyz

variables

>>> xyz = ['x','y']

Page 219: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

219

What's the difference? — 1b

liststr

str x

yxyz

variables

>>> xyz[0] =

str Astr x

'A' Right hand sideevaluated first

Page 220: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

220

What's the difference? — 1c

liststr

str x

yxyz

variables

>>> xyz[0] =

str A

str x

'A' New valueassigned

Old valueunused andcleaned up.

Page 221: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

221

What's the difference? — 1d

liststr

str A

Bxyz

variables

>>> xyz[1] = 'B' Repeat forxyz[1] = 'B'

Page 222: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

222

What's the difference? — 2a

liststr

str x

yxyz

variables

>>> xyz = ['x','y']

Page 223: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

223

What's the difference? — 2b

liststr

str x

yxyz

variables

>>> xyz = ['A','B'] Right hand sideevaluated first

liststr

str A

B

Page 224: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

224

What's the difference? — 2c

liststr

str x

y

xyz

variables

>>> xyz = ['A','B'] New valueassigned

liststr

str A

B

Old valueunused andcleaned up.

Page 225: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

225

What's the difference?

Modification:

Replacement:

Does it matter?

same list, different contents

different list

?

Page 226: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

226

Two names for the same list

>>> xyz = ['x','y']

>>> abc = xyz

liststr

str x

y

abc

variables

xyz

Page 227: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

227

>>> abc[0] = 'A'

liststr

str A

B

abc

variables

xyz

>>> xyz

['A', 'B']

Modification

>>> abc[1] = 'B' Modification

Page 228: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

228

liststr

str x

y

abc

variables

xyz

Same starting point

>>> xyz = ['x','y']

>>> abc = xyz

Page 229: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

229

liststr

str x

y

abc

variables

xyz

liststr

str A

B

>>> xyz

['x', 'y']

>>> abc = ['A','B'] Replacement

Page 230: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

230

One last trick with slices>>> abc = ['a','b',

>>> abc[2:4]

['c','d']

>>> abc[2:4] ['x',y','z']

>>> abc

['a','b', ,'e','f']'x',y','z'

'c','d','e','f']

=

Length 6

New length

Page 231: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

231

ProgressModifying lists values[N] = new_value

Modification ≠ replacement

values[0] = 'alpha'values[1] = 'beta'values[2] = 'gamma'

values = ['alpha', 'beta', 'gamma']

Page 232: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

232

Exercise 1. Predict what these will do.2. Then run the commands.

5 minutes

>>> alpha = [0, 1, 2, 3, 4]>>> beta = alpha>>> gamma = alpha[:]>>> delta = beta[:]

>>> beta[0] = 5

>>> alpha>>> beta>>> gamma>>> delta

Page 233: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

233

Appending to a list>>> abc = ['x','y']

>>> abc.append('z')

>>> abc

['x','y']

>>> abc

['x','y','z']

Add one element tothe end of the list.

Page 234: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

234

List “methods”

abc )'z'(append.

A list

A dot

A built-in function

Round brackets

Argument(s)to the function

Built-in functions: “methods”

Page 235: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

235

.

Methods

object (arguments)method

Privileged access to object

Connected by a dot

“Object-oriented programming”

Page 236: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

236

The append() method>>> abc = ['x','y','z']

>>> abc.append( )'A'

>>> abc.append( )'B'

>>> abc.append( )'C'

>>> abc

['x','y','z','A','B','C']

One element at a time

Page 237: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

237

Beware!>>> abc = ['x','y','z']

>>> abc.append( )['A','B','C']

>>> abc

['x', 'y', 'z', ['A','B','C']]

Appendinga list

Get a list asthe last item!

Page 238: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

238

“Mixed lists”

['x', 'y', 'z', ['A','B','C']]!

['x', 2, 3.0]

['alpha', 5, 'beta', 4, 'gamma', 5]

Page 239: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

239

The extend() method>>> abc = ['x','y','z']

>>> abc.extend( )['A','B','C']

>>> abc

['x','y','z','A','B','C']

All in one go

Utterly unnecessary! !

Page 240: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

240

Avoiding extend()>>> abc = ['x','y','z']

>>> abc = abc + ['A', 'B', 'C']

>>> abc

['x', 'y', 'z', 'A', 'B', 'C']

Page 241: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

241

Changing the list “in place”>>> abc

>>>

.append('w')No value returned

abc['x','y','z','w'] List itself is

changed

>>> abc

>>>

.extend(['A','B'])No value returned

abc['x','y','z','w','A','B'] List itself is

changed

Page 242: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

242

Another list method: sort()>>> abc = ['z','x','y']

>>> abc.

>>> abc

['x','y','z']

()sort

New method

No arguments

Page 243: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

243

Any type of sortable element>>> abc = [3, 1, 2]>>> abc.sort()>>> abc[1, 2, 3]

>>> abc = [3.142, 1.0, 2.718]>>> abc.sort()>>> abc[1.0, 2.718, 3.142]

Page 244: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

244

Another list method: insert()

>>> abc = [ 'z']'y','x','w',

0 1 2 3

>>> abc.insert( )'A',2

>>> abc

[ 'z']'y','x','w', 'A',

“old 2”

Insert just beforeelement number 2

Page 245: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

245

Progress

list.append(item)

list.extend([item1, item

2, item

3])

list.sort()

list.insert(index, item)

Don't return any result

Change the list itselfList methods:

Page 246: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

2465 minutes

Exercise

data = []data.append(8)data.extend([6,3,9])data.sort()data.append(1)data.insert(3,2)data

1. Predict what this will do.2. Then run the commands.

Page 247: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

247

Creating new lists>>> numbers = [0,1,2,3,4,5,6,7,8,9]

>>> copy =

>>> for number in numbers:

... copy.append(number)

...

>>>

[]

copy

[0,1,2,3,4,5,6,7,8,9]

Simplecopying

Page 248: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

248

Creating new lists>>> numbers =

>>> squares = []

>>> for number in numbers:

... squares.append(number**2)

...

>>> squares

[0,1,4,9,16,25,36,49,64,81]

[0,1,2,3,4,5,6,7,8,9]Boring!

Changingthe value

Page 249: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

249

Lists of numbers>>> numbers = range(0,10)

>>> numbers

[0,1,2,3,4,5,6,7,8,9]

range( )10,0

[ ,1,2,3,4,5,6,7,8,9]0 c.f. numbers[0:5]

Page 250: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

250

Creating new lists>>> numbers =

>>> squares = []

>>> for number in numbers:

... squares.append(number**2)

...

>>> squares

[0,1,4,9,16,25,36,49,64,81]

range(0,10)Better!

Page 251: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

251

Lists of words

>>> 'the cat sat on the mat'

['the','cat','sat','on','the','mat']

>>> 'The cat sat on the mat.'.split()

['The','cat','sat','on','the','mat

split().

string method

.']

No special handlingfor punctuation.

Page 252: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

252

Progress

Ways to build lists:

slices

loops appending elements

range(m,n) function

for

data[:]

split() string method

Page 253: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

25310 minutes

Exercise

Write a script from scratch:

1. Run a variable n from 0 to 10 inclusive.2. Create a list with the corresponding values of

n2 + n + 41.3. Print the list.

transform.py

Page 254: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

254

Briefdiversion

Page 255: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

255

Arrays as lists of lists0.0 -1.0 -4.0 -1.0 0.01.0 -0.0 -1.0 -0.0 1.04.0 -1.0 -0.0 -1.0 4.01.0 -0.0 -1.0 -0.0 1.00.0 -1.0 -4.0 -1.0 0.0

[ [0.0, -1.0, -4.0, -1.0, 0.0] ,[1.0, -0.0, -1.0, -0.0, 1.0] ,[4.0, -1.0, -0.0, -1.0, 4.0] ,[1.0, -0.0, -1.0, -0.0, 1.0] ,[0.0, -1.0, -4.0, -1.0, 0.0] ]

Page 256: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

256

0.0 -1.0 -4.0 -1.0 0.01.0 -0.0 -1.0 -0.0 1.04.0 -1.0 -0.0 4.01.0 -0.0 -1.0 -0.0 1.00.0 -1.0 -4.0 -1.0 0.0

Indexing from zero

[ [0.0, -1.0, -4.0, -1.0, 0.0] ,[1.0, -0.0, -1.0, -0.0, 1.0] ,[4.0, -1.0, -0.0, , 4.0] ,[1.0, -0.0, -1.0, -0.0, 1.0] ,[0.0, -1.0, -4.0, -1.0, 0.0] ]

1.0

1.0

a23

a[2][3]

Page 257: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

257

0.0 -1.0 -4.0 -1.0 0.01.0 -0.0 -1.0 -0.0 1.0

1.0 -0.0 -1.0 -0.0 1.00.0 -1.0 -4.0 -1.0 0.0

Referring to a row — easy

[ [0.0, -1.0, -4.0, -1.0, 0.0] ,[1.0, -0.0, -1.0, -0.0, 1.0] ,[4.0, -1.0, -0.0, 1.0, 4.0] ,[1.0, -0.0, -1.0, -0.0, 1.0] ,[0.0, -1.0, -4.0, -1.0, 0.0] ]

4.0 -1.0 -0.0 -1.0 4.0

a[2]

Page 258: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

258

0.0 -1.0 -4.0 -1.0 0.01.0 -0.0 -1.0 -0.0 1.01.0 -1.0 -0.0 -1.0 4.01.0 -0.0 -1.0 -0.0 1.00.0 -1.0 -4.0 -1.0 0.0

Referring to a column

[ [0.0, -1.0, -4.0, -1.0, 0.0] ,[1.0, -0.0, -1.0, -0.0, 1.0] ,[4.0, -1.0, -0.0, 1.0, 4.0] ,[1.0, -0.0, -1.0, -0.0, 1.0] ,[0.0, -1.0, -4.0, -1.0, 0.0] ]

No Pythonconstruct!

Page 259: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

259

Numerical Python?

Hold tight!

Later in this course,powerful support for:

numerical arrays

matrices

“numpy”

Page 260: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

260

End ofdiversion

Page 261: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

261

Files

inputdatafile #2

inputdatafile #1

inputdatafile #3

outputdatafile #1

outputdatafile #2

pythonscript

Page 262: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

262

Reading a file

3. Closing the file

1. Opening a file

2. Reading from the file

Page 263: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

263

Opening a file'data.txt'

line one\nline two\nline three\nline four\n

filesystem nodeposition in file

Python file object

file name

Data on disc

open()

Page 264: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

264

'data.txt'>>> data = open ( )

file name stringPythoncommand

Pythonfile object

file

refers to the file with name 'data.txt'

initial position at start of data

Page 265: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

265

Reading from a file

line one\nline two\nline three\nline four\n

filesystem nodeposition in file

Python file objectData on disc

Pythonscript

Page 266: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

266

>>> data= open('data.txt')

>>> data.readline()

the Python file objecta dota “method”

'line one\n' first line of the filecomplete with “\n”

>>>'line two\n'

data.readline() same command again

second line of file

Page 267: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

267

>>>

line one\nline two\nline three\nline four\n

position:start of file

data = open('data.txt')data

Page 268: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

268

>>>

line one\nline two\nline three\nline four\n

position:after end of first lineat start of second line

data = open('data.txt')data

>>> data.readline()

'line one\n'

Page 269: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

269

>>>

line one\nline two\nline three\nline four\n

after end of second lineat start of third line

data = open('data.txt')data

>>> data.readline()

'line one\n'

>>> data.readline()

'line two\n'

Page 270: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

270

>>>

line one\nline two\nline three\nline four\n

end of file

data = open('data.txt')data

>>> data.readline()

'line one\n'

>>> data.readline()

'line two\n'

>>> data.readlines()

['line three\n', 'line four\n' ]

Page 271: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

271

>>> data.readline()

'line two\n'

>>> data.readlines()

['line three\n', 'line four\n' ]

>>> data.close()

data

disconnect

Page 272: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

272

Common trick

Python “magic”:treat the file likea list and it willbehave like a list

for line in data: stuff

for line in data.readlines(): stuff

Page 273: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

273

Simple example script

count = 0

print(count)data.close() count = count + 1for line in data:data = open('data.txt')

1. Open the file

2. Read the fileOne line at a time

3. Close the file

Page 274: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

274

Progressfilename open() readable file object

data.readline()

for line in data: ...line...

data = open('input.dat')

Page 275: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

275

Exercise Write a script treasure.pyfrom scratch to do this:

Open the file treasure.txt.Set three counters equal to zero:

n_lines, n_words, n_charsRead the file line by line.For each line:

increase n_lines by 1increase n_chars by the length of the linesplit the line into a list of wordsincrease n_words by the length of the list

Close the file.Print the three counters.

15 minutes

Page 276: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

276

Converting the type of inputProblem:

1.02.03.04.05.06.07.08.09.010.011.0

numbers.dat

['1.0\n','2.0\n', '3.0\n','4.0\n', '5.0\n','6.0\n', '7.0\n','8.0\n', '9.0\n','10.0\n', '11.0\n']

List of strings, nota list of numbers.

Page 277: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

277

Type conversions>>> float('1.0\n')

1.0

>>> str(1.0)

'1.0'

String

Float

>>> float(1)

1.0

Int

>>> int(-1.5)

-1

Float

Float

String

Float

Int

No newline

Rounding to zero

Page 278: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

278

Type conversions to lists

>>> list('hello')

['h','e','l','l','o']

String

>>> data = open('data.txt')

>>> list(data)

['line one\n', 'line two\n', 'line three\n', 'line four\n']

File

List

List

Page 279: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

279

Example script

sum = 0.0

print sumdata.close() sum = sum + float(line)for line in data:data = open('numbers.dat')

Page 280: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

280

Pythonscript

Writing to a file

filesystem nodeposition in file

Python file object Data on disc

'output.txt'

file name open()

Page 281: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

281

'r'output = open('output.txt' ),

Writing to a file

Open forreading

output = open('output.txt' )

'w'output = open('output.txt' ), Open forwriting

Default

Equivalent

Page 282: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

282

filesystem nodeposition in file

Opening a file for writing

Startof file

Emptyfile

'output.txt'

open('output.txt','w')

Page 283: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

283

>>> output = open('output.txt','w')

file name open forwriting

Page 284: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

284

>>> output = open('output.txt','w')

alpha\n

>>> output. )'alpha\n'(write

Method towrite a lumpof data

Lump ofdata

“Lump”: neednot be a line.

Currentpositionchanged

Page 285: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

285

>>> output = open('output.txt','w')

alpha\nbet

>>> output. )'alpha\n'(write

>>> output. )'bet'(write

Lumpof data tobe written

Page 286: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

286

>>> output = open('output.txt','w')

alpha\nbeta\n

>>> output. )'alpha\n'(write

>>> output. )'a\n'(write

Remainderof the line

>>> output. )'bet'(write

Page 287: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

287

>>> output = open('output.txt','w')

alpha\nbeta\ngamma\ndelta\n

>>> output. )'alpha\n'(write

>>> output. )'a\n'(write

>>> output.)

['gamma\n',(writelines'delta\n']

Method to writea list of lumps

>>> output. )'bet'(write

Page 288: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

288

>>> output = open('output.txt','w')

>>> output. )'alpha\n'(write

>>> output. )'a\n'(write

>>> output. ['gamma\n',(writelines'delta\n']

>>> output.close()

Python is donewith this file.

Data may not be written to discuntil close()!

Page 289: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

289

!

alpha\nbeta\ngamma\ndelta\n

>>> output.close()

Only on close() is itguaranteed that thedata is on the disc!

Page 290: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

290

Progressfilename open() writable file object

data.write(line)

data = open('input.dat', 'w')

line must include \n

data.close() “flushes” to disc

Page 291: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

291

Example

output = open('output.txt', 'w')output.write('Hello, world!\n')output.close()

Page 292: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

292

Example of a “filter”Reads one file, writes another.

inputdatafile

outputdatafile

pythonscript

Page 293: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

293

output.write('Line ') output.write(str(line_number)) output.write(' has ') output.write(str(len(words))) output.write(' words.\n')

Example of a “filter”input = open('input.dat', 'r')output = open('output.dat', 'w')line_number = 0

input.close()output.close()

for line in input: line_number = line_number + 1 words = line.split()

Setup

Shutdown

Ugly!

filter1.py

Page 294: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

294

Exercise

Read treasure.txt and write treasure.out.For each line write to the output:

line numbernumber of words on the linenumber of characters in the line

separated by TABs.At the end output a summary line

number of linestotal number of wordstotal number of characters

separated by TABs too.15 minutes

Change treasure.pyto do this:

Page 295: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

295

Problem

A snippet ofcode using n

…n…

…n… But what if n wasalready in use?

results = []for n in range(0,11): results.append(n**2 + n + 41)

Page 296: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

296

Want to isolatethis bit of code.

…n…

…n… Keep it away fromthe external n.

results = []for n in range(0,11): results.append(n**2 + n + 41)

Solution in principle

Page 297: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

297

Solution in principle

= []for n in range(0, ): .append(n**2 + n + 41)

11

Pass in the valueof the upper limit

Pass out thecalculatedlist's value.

results

results

The namesused insidenever get out

Page 298: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

298

Solution in practice

11

…results (my_function= )

output

function

input

Need to beable to defineour own functions!

Page 299: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

299

Defining our function

def

:)limit(limitmy_function(

define

function name

input

colon

indentation

Page 300: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

300

Defining our function

):limitanswer = []for in range(0, limit):n

answer.append(n**2 + n + 41)

Names are usedonly in the function

Function definition

def my_function(

Page 301: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

301

Defining our function

):limitanswer = []for in range(0, limit):n

answer.append(n**2 + n + 41)

def my_function(

return answer

Pass back…

…this value

Page 302: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

302

11

Using our function

def my_function(limit): answer = [] for n in range(0, limit): answer.append(n**2 + n + 41) return answer

results )= my_function(

“answer” “limit”

Page 303: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

303

Why usefunctions?

Reuse

ReliabilityClarity

If you use a functionin lots of places andhave to change it, you only have to editit in one place.

Clearly separatedcomponents areeasier to read.

Isolation of variablesleads to fewer accidental clashesof variable names.

Page 304: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

304

A “real” worked example

Write a function to take a list of floating pointnumbers and return the sum of the squares.

(ai) → ∑|a

i|2

Page 305: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

305

Example 1

def norm2(values):

sum = 0.0

for value in values: sum = sum + value**2

return sum

Page 306: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

306

Example 1print norm2([3.0, 4.0, 5.0])

50.0

$ python norm2.py

50.0

169.0

[3.0, 4.0, 5.0]

[12.0, 5.0]

Page 307: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

307

A second worked example

Write a function to pull theminimum value from a list.

(ai) → min(a

i)

Page 308: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

308

Example 2

def minimum(a_list):

a_min = a_list[0] for a in a_list: if a < a_min: a_min = a

return a_min

When will this go wrong??

Page 309: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

309

Example 2

print minimum([2.0, 4.0, 1.0, 3.0])

1.0

$ python minimum.py

3.0

5

[4.0, 3.0, 5.0]

[12, 5]

Page 310: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

310

A third worked example

Write a function to “dot product” two vectors.

(ai,b

j) → ∑a

kb

k

Page 311: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

311

Example 3

def dot(a_vec, b_vec):

sum = 0.0 for n in range(0,len(a_vec)): sum = sum + a_vec[n]*b_vec[n]

return sum

When will this go wrong??

Page 312: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

312

Example 3

print dot([3.0, 4.0], [1.0, 2.0]))

11.0

$ python dot_product.py

11.0

115

Page 313: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

313

Example 3 — version 2

def dot(a_vec, b_vec):

if len(a_vec) != len(b_vec): print 'WARNING: lengths differ!'

sum = 0.0 for n in range(0,len(a_vec)): sum = sum + a_vec[n]*b_vec[n]

return sum

Page 314: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

314

A fourth worked example

Write a function to filter out thepositive numbers from a list.

e.g.

[1, -2, 0, 5, -5, 3, 3, 6] [1, 5, 3, 3, 6]

Page 315: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

315

Example 4

def positive(a_list):

answer = []

for a in a_list: if a > 0: answer.append(a)

return answer

Page 316: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

316

Progress

Functions !

Defining them

Using them

Page 317: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

317

Exercise

Write a function list_max() which takes two listsof the same length and returns a third list whichcontains, item by item the larger item from each list.

list_max([1,5,7], [2,3,6]) [2,5,7]

Hint: There is a built-in function max(x,y)which gives the maximum of two values.

15 minutes

Page 318: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

318

How to return morethan one value?

Write a function to pull theminimum and maximumvalues from a list.

Page 319: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

319

Returning two values

def min_max(a_list): a_min = a_list[0] a_max = a_list[0] for a in a_list: if a < a_min: a_min = a if a > a_max: a_max = a

return (a_min, a_max) Pair ofvalues

Page 320: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

320

Receiving two values

values = [1, 2, 3, 4, 5, 6, 7, 8, 9]

print minvalprint maxval

(minval, maxval) = min_max(values)

Pair ofvariables

Page 321: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

321

Pairs, triplets, …

“tuples”

singlesdoublestriplesquadruplesquintuples…

Page 322: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

322

Tuples ≠ Lists

Lists Tuples

Concept of “next entry” All items at once

Same types Different types

Mutable Immutable

Page 323: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

323

Tuple examples

Pair of measurements of a tree

(7.2, 0.5)(height,width)

(0.5, 7.2)(width,height)

Details about a person

('Bob', 45, 1.91)(name, age, height)

(45, 1.91, 'Bob')(age, height, name)

Page 324: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

324

Progress

Tuples

“not lists”

Multiple values bound together

Functions returning multiple values

Page 325: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

325

Exercise

Copy the min_max() function.Extend it to return a triplet:(minimum, mean, maximum)

10 minutes

Page 326: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

326

Tuples and string substitution

“Hello, my name is Bob and I'm 46 years old.”

Page 327: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

327

Simple string substitution

>>> 'My name is

'My name is Bob.'

'Bob'%.'%s

Substitution marker

Substitution operator

%s Substitute a string.

Page 328: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

328

Simple integer substitution

>>> 'I am

'I am 46 years old.'

46%.'

Substitution marker

%d Substitute an integer.

years old%d

Page 329: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

329

>>> '''My name is %s andI am %d years old.'''... % ('Bob', 46)

'My name is Bob and\nI am 46 years old.'

Two markers

A pair

Tuple substitution

Page 330: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

330

Lists of tuplesdata = [ ('Bob', 46), ('Joe', 9), ('Methuselah', 969) ]

for

List of tuples

print '%s %d' % (person, age)in data:(person, age)

Tuple ofvariablenames

Page 331: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

331

Problem: ugly output

Bob 46Joe 9Methuselah 969

Columns should align

Columns of numbersshould be right aligned

Bob 46Joe 9Methuselah 969

Page 332: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

332

'%-5s' %

'%

Solution: formatting

'%s' % 'Bob'

'Bob'

'Bob'

'Bob'

'␣␣Bob'

'Bob␣␣'

Five characterss' %5

Right aligned'%-

Left aligned

'% 'Charles' 'Charles's' %5

Page 333: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

333

'%d'

Solution: formatting

% 46 '46'

'%5d' % 46 '␣␣␣46'

'%-5d' % 46 '46␣␣␣'

'%05d' % 46 '00046'

Page 334: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

334

Columnar outputdata = [ ('Bob', 46), ('Joe', 9), ('Methuselah', 969) ]

forprint

in data:(person, age)% (person, age)'%-10s %3d'

Properly formatted

Page 335: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

335

Floats

'%f' % 3.141592653589 '3.141593'

'%.4f' % 3.141592653589 '3.1416'

'%.4f' % 3.1 '3.1000'

Page 336: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

336

Progress

Formatting operator

Formatting markers

'%s %d' % ('Bob', 46)

%s %d %f

Formatting modifiers %-4s

Page 337: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

337

ExerciseComplete the script format1.pyto generate this output:

Alfred 46 1.90Bess 24 1.75Craig 9 1.50Diana 100 1.66↑ ↑ ↑1 9 15

5 minutes

Page 338: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

338

Reusing our functions

Want to use the same function in many scripts

Copy? Have to copy any changes.

Singleinstance?

Have to import theset of functions.

Page 339: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

339

How to reuse — 0

def min_max(a_list): … return (a_min,a_max)

vals = [1, 2, 3, 4, 5]

(x, y) = min_max(vals)

print(x, y)

five.py

Page 340: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

340

How to reuse — 1

vals = [1, 2, 3, 4, 5]

(x, y) = min_max(vals)

print(x, y)

five.py

def min_max(a_list): … return (a_min,a_max)

utils.py

Move the definitionof the function to aseparate file.

Page 341: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

341

How to reuse — 2

import utils

vals = [1, 2, 3, 4, 5]

(x, y) = min_max(vals)

print(x, y)

five.py

def min_max(a_list): … return (a_min,a_max)

utils.py

Identify the file withthe functions in it.

Page 342: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

342

How to reuse — 3

import utils

vals = [1, 2, 3, 4, 5]

(x, y) = utils.min_max(vals)

print(x, y)

five.py

def min_max(a_list): … return (a_min,a_max)

utils.py

Indicate that thefunction comes from that import.

Page 343: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

343

A library of our functions

“Module”Functions

Objects

Parameters

Container

Page 344: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

344

System modulesos

sys

re

operating system access

general system functions

regular expressions

subprocess support for child processes

csv read/write comma separated values

math standard mathematical functions

numpy numerical arrays and more

scipy maths, science, engineering

Page 345: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

345

Using a system module

>>> import math

>>> math.sqrt(2.0)

1.4142135623730951

>>>

Keep track ofthe module withthe function.

Page 346: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

346

Don't do this

>>> from math import sqrt

>>> sqrt(2.0)

1.4142135623730951

>>>

!

Page 347: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

347

Really don't do this

>>> from math import *

>>> sqrt(2.0)

1.4142135623730951

>>>

!!

Page 348: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

348

Do do this

>>> import math

>>> help(math)

Help on module math:NAME mathDESCRIPTION This module is always available. It provides access to the mathematical functions defined by the C standard.

Page 349: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

349

Progress

“Modules”

System modules

Personal modules

import module

module.function(...)

Page 350: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

350

Exercise

Edit your utils.py file.1.

Write a function print_list() that printsall the elements of a list, one per line.

2.

Edit the elements2.py script to use this newfunction.

3.

5 minutes

Page 351: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

351

Interacting with the system

>>> import sys

Page 352: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

352

Standard input and output

>>> import sys

sys.stdin

sys.stdout

Treat like anopen(…, 'r') file

Treat like anopen(…, 'w') file

Page 353: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

353

Line-by-line copying — 1

import sys

sys.stdout

for :sys.stdin

.write(

inline

)line

Import module

No need to open() sys.stdin or sys.stdout.The module has done it for you at import.

Page 354: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

354

Line-by-line copying — 2

import sys

sys.stdout

for :sys.stdin

.write(

inline

)line

Standard input

Treat a file like a list Acts like a list of lines

Page 355: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

355

Line-by-line copying — 3

import sys

sys.stdout

for :sys.stdin

.

inline

)line Standard output(write

An open fileThe file'swrite()method

Page 356: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

356

Line-by-line copying — 4

import sys

sys.stdout

for :sys.stdin

.write(

inline

)line

$ python copy.py < out.txt>in.txt

Copy

Lines in…lines out

Page 357: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

357

Line-by-line actions

Copying linesunchanged

Changingthe lines

Only copyingcertain lines

Gatheringstatistics

Page 358: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

358

Line-by-line rewriting

import sys

sys.stdout

for :

.write(

input

)output

$ python process.py < out.txt>in.txt

Process

output

Define orimport afunction here

)input

in sys.stdin

= function(

Page 359: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

359

Line-by-line filtering

import sys

for :sys.stdinininput

$ python filter.py < out.txt>in.txt

Filter

if test(input):

sys.stdout.write( )input

Define orimport a testfunction here

Page 360: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

360

Progress

sys module

sys.stdin

sys.stdout

Standard input

Standard output

“Filter” scripts process line-by-line

only output on certain input lines

Page 361: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

361

Exercise

Write a script that reads from standard input.

If should generate two lines of output:

Number of lines: MMMNumber of blank lines: NNN

Hint: len(line.split()) == 0 for blank lines.

5 minutes

Page 362: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

362

The command line

We are puttingparameters inour scripts.

We want to putthem on the command line.

$

number = 1.25…

python script.py 1.25

Page 363: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

363

Reading the command line

import sys

print(sys.argv)

$ python args.py 1.25

[ ]'1.25','args.py'

sys.argv[0] sys.argv[1]

Script'sname

Firstargument

A string!

Page 364: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

364

import sys

number = sys.argv[1]number = number + 1.0

print(number)

Command line strings

Traceback (most recent call last): File "thing.py", line 3, in <module> number = number + 1.0TypeError: cannot concatenate 'str' and 'float' objects

Page 365: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

365

Using the command line

import sys

number = float(sys.argv[1])number = number + 1.0

print(number)

Enough arguments?

Valid as floats?

Page 366: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

366

Better tools forthe command line

argparse module Very powerful parsingExperienced scripters

Page 367: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

367

General principles

1. Read in the command line

2. Convert to values of the right types

3. Feed those values into calculating functions

4. Output the calculated results

Page 368: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

368

Worked example

Write a script to print points

y=xr x∈[0,1], uniformly spaced(x, y)

Two command line arguments:r (float) powerN (integer) number of points

Page 369: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

369

General approach

1a.

1b.

2a.

2b.

3.

Write a script that tests that function.

Write a function that parses the command line fora float and an integer.

Write a script that tests that function.

Combine the two functions.

Write a function that takes (r, N) as (float, integer)and does the work.

Page 370: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

370

import sys

def parse_args():pow = float(sys.argv[1])num = int(sys.argv[2])

return (pow, num)

1a. Write a function that parses the command line fora float and an integer.

curve.py

Page 371: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

371

import sys

def parse_args(): ...(r, N) = parse_args()print 'Power: %f' % rprint 'Points: %d' % N

1b. Write a script that tests that function.

curve.py

Page 372: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

372

1b. Write a script that tests that function.

$ python curve.py 0.5 5

Power: 0.500000Points: 5

Page 373: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

373

2a. Write a function that takes (r, N) as (float, integer)and does the work.

def power_curve(pow, num_points):

for index in range(0, num_points):x = float(index)/float(num_points-1)y = x**powprint '%f %f' % (x, y)

curve.py

Page 374: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

374

2b. Write a script that tests that function.

def power_curve(pow, num_points): ...

power_curve(0.5, 5)

curve.py

Page 375: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

375

2b. Write a script that tests that function.

$ python curve.py

0.000000 0.0000000.250000 0.5000000.500000 0.7071070.750000 0.8660251.000000 1.000000

Page 376: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

376

3. Combine the two functions.

import sys

def parse_args():pow = float(sys.argv[1])num = int(sys.argv[2])return (pow, num)

def power_curve(pow, num_points):for index in range(0, num_points):

x = float(index)/float(num_points-1)y = x**powprint '%f %f' % (x, y)

(power, number) = parse_args()power_curve(power, number)

curve.py

Page 377: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

377

Progress

Parsing the command line

sys.argv

Convert from strings to useful types

int() float()

Page 378: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

378

Exercise

Write a script that takes a command line ofnumbers and prints their minimum and maximum.

Hint: You have already written a min_max function.Reuse it.

5 minutes

Page 379: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

379

Back to our own module>>> import utils>>> help(utils)

Help on module utils:NAME utilsFILE /home/rjd4/utils.pyFUNCTIONS min_max(numbers)...

We want to dobetter than this.

Page 380: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

380

Function help>>> import utils>>> help(utils.min_max)

Help on function min_max inmodule utils:

min_max(numbers)

Page 381: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

381

Annotating a functiondef min_max(numbers):

Our current file minimum = numbers[0] maximum = numbers[0] for number in numbers: if number < minimum:

minimum = numberif number > maximum: maximum = number

return (minimum, maximum)

Page 382: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

382

A “documentation string”def min_max(numbers):

minimum = numbers[0] maximum = numbers[0] for number in numbers: if number < minimum:

minimum = numberif number > maximum: maximum = number

return (minimum, maximum)

"""This functions takes a list of numbers and returns a pair of their minimum and maximum. """

A string beforethe body of thefunction.

Page 383: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

383

Annotated function>>> import utils>>> help(utils.min_max)

Help on function min_max inmodule utils:

min_max(numbers) This functions takes a list of numbers and returns a pair of their minimum and maximum.

Page 384: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

384

Annotating a module

def min_max(numbers):

minimum = numbers[0] maximum = numbers[0] for number in numbers:...

"""This functions takes a list of numbers and returns a pair of their minimum and maximum. """

"""A personal utility modulefull of all the pythonic goodnessI have ever written."""

A string beforeany active partof the module.

Page 385: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

385

Annotated module>>> import utils>>> help(utils)

Help on module utils:NAME utilsFILE /home/rjd4/utils.pyDESCRIPTION A personal utility module full of all the pythonic goodness I have ever written.

Page 386: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

386

Progress

Annotations

…of functions

…of modules

help()

“Doc strings”

Page 387: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

387

Exercise

Annotate your utils.py and the functions in it.

3 minutes

Page 388: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

388

Simple data processing

input data

output data

Python script

What format?

Page 389: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

389

Comma Separated Values

input dataA101,Joe,45,1.90,100G042,Fred,34,1.80,92H003,Bess,56,1.75,80...

1.0,2.0,3.0,4.02.0,4.0,8.0,16.03.0,8.0,24.0,64.0...

Page 390: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

390

Quick and dirty .csv — 1

CSV: “comma separated values”

>>> line =

>>> line.split(

'1.0, 2.0, 3.0, 4.0\n'

More likely tohave comefrom sys.stdin

)',' Split on commasrather than spaces.

['1.0', ' 2.0', ' 3.0', ]' 4.0\n'

Note the leadingand trailingwhite space.

Page 391: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

391

Quick and dirty .csv — 2>>> line = '1.0, 2.0, 3.0, 4.0\n'

>>> strings = line.split(',')

>>> numbers = []

>>> numbers

[1.0, 2.0, 3.0, 4.0]

>>> for string in strings:

... numbers.append(float(string))

...

Page 392: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

392

Quick and dirty .csv — 3

Why “quick and dirty”?

Can't cope with common cases:

'"1.0","2.0","3.0","4.0"'Quotes

'A,B\,C,D'Commas

csvDedicated module:

Page 393: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

393

Proper .csvcsvDedicated module:

import csvimport sys

input = csv.reader(sys.stdin)output = csv.writer(sys.stdout)

for [id, name, age, height, weight] in input: output.writerow([id, name, float(height)*100])

Much more in the “Python: Further Topics” course

Page 394: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

394

Processing dataStoring data in the program

id name age height weight

A101 Joe 45 1.90 100G042 Fred 34 1.80 92H003 Bess 56 1.75 80...

id → (name, age, height, weight)? ?

Page 395: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

395

Simpler caseStoring data in the program

id name

A101 JoeG042 FredH003 Bess...

id → name? ?

Page 396: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

396

Not the same as a list…

index name

0 Joe1 Fred2 Bess...

['Joe', 'Fred', 'Bess', …]

names[1] = 'Fred'

Page 397: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

397

…but similar: a “dictionary”

{'A101':'Joe', 'G042':'Fred', 'H003':'Bess', …}

names['G042'] = 'Fred'

id name

A101 JoeG042 FredH003 Bess...

Page 398: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

398

Dictionaries Generalized look up

Pythonobject (immutable)

Python object(arbitrary)

'G042' 'Fred' string string

1700045 29347565 int int

'G042' ('Fred', 34) string tuple

(34, 56) 'treasure' tuple string

“key” “value”

(5,6) [5, 6, 10, 12] tuple list

Page 399: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

399

Building a dictionary — 1

data =

A101 → JoeG042 → FredH003 → Bess

'H003':'Bess''G042':'Fred' ,,'A101' }{

Curly brackets

Items

Comma

'Joe':

Key

colon

Value

Page 400: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

400

Building a dictionary — 2

data = {} Empty dictionary

data 'Joe'=]'A101'[

Square brackets

Key

Value

data 'Fred'=]'G042'[

data 'Bess'=]'H003'[A101 → JoeG042 → FredH003 → Bess

Page 401: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

401

Example — 1

>>> data = {'A101':'Joe', 'F042':'Fred'}

>>> data

{'F042': 'Fred', 'A101': 'Joe'}

Order is notpreserved!

Page 402: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

402

Example — 2

>>> data['A101']

'Joe'

>>> data['A101'] = 'James'

>>> data

{'F042': 'Fred', 'A101': 'James'}

Page 403: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

403

Square brackets in Python

[…] Defining literal lists

values[key] Looking up in a dictionary

numbers[M:N] Slices

numbers[N] Indexing into a list

Page 404: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

404

Example — 3

>>> data['X123'] = 'Bob'

>>> data['X123']

'Bob'

>>> data

{'F042': 'Fred', 'X123': 'Bob','A101': 'James'}

Page 405: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

405

Progress Dictionaries

data = {'G042':('Fred',34), 'A101':('Joe',45)}

data['G042'] ('Fred',34)

data['H003'] = ('Bess ', 56)

Page 406: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

406

ExerciseWrite a script that:

1. Creates an empty dictionary, “elements”.

2. Adds an entry 'H'→'Hydrogen'.

3. Adds an entry 'He'→'Helium'.

4. Adds an entry 'Li'→'Lithium'.

5. Prints out the value for key 'He'.

6. Tries to print out the value for key 'Be'.10 minutes

Page 407: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

407

Worked example — 1

Reading a file to populate a dictionary

H HydrogenHe HeliumLi LithiumBe BerylliumB BoronC CarbonN NitrogenO OxygenF Fluorine...

elements.txt

symbol_to_name Dictionary

File

Page 408: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

408

Worked example — 2data = open('elements.txt')

for line in data: [symbol, name] = line.split()

symbol_to_name[symbol] = name

data.close()

Open file

Read data

Populate dictionaryClose file

Now ready to use the dictionary

symbol_to_name = {} Empty dictionary

Page 409: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

409

Worked example — 3

Reading a file to populate a dictionary

A101 JoeF042 FredX123 BobK876 AliceJ000 MaureenA012 StephenX120 PeterK567 AnthonyF041 Sally...

names.txt

key_to_name

Page 410: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

410

Worked example — 4data = open('names.txt')

for line in data: [key, person] = line.split()

key_to_name[key] = person

data.close()

key_to_name = {}

Page 411: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

411

'elements.txt'

Make it a function!

symbol_to_name symbol

symbol_to_name

data = open(

for line in data: [symbol, name] = line.split()

data

name

.close()

= {}

)

[ ] =

Page 412: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

412

'elements.txt'

Make it a function!

symbol_to_name symbol

symbol_to_name

data = open(

for line in data: [symbol, name] = line.split()

data

name

.close()

= {}

)

[ ] =

Input

Page 413: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

413

filename

filename

Make it a function!

symbol_to_name symbol

symbol_to_name

data = open(

for line in data: [symbol, name] = line.split()

data

name

.close()

= {}

)

[ ] =

Input

def filename_to_dict( ):

Page 414: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

414

symbol_to_name

filename

filename

Make it a function!

symbol

symbol_to_name

data = open(

for line in data: [symbol, name] = line.split()

data

name

.close()

= {}

)

[ ] =

def filename_to_dict( ):

Output

Page 415: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

415

x_to_y

filename

filename

Make it a function!

x

x_to_y

data = open(

for line in data: [x, y] = line.split()

data

y

.close()

= {}

)

[ ] =

def filename_to_dict( ):

Output

Page 416: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

416

x_to_y

x_to_y

filename

filename

Make it a function!

x

x_to_y

data = open(

for line in data: [x, y] = line.split()

data

y

.close()

= {}

)

[ ] =

def filename_to_dict( ):

Output

return( )

Page 417: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

417

Exercise1. Write filename_to_dict()

in your utils module.

2. Write a script that does this:

a. Loads the file elements.txt as a dictionary(This maps 'Li' → 'lithium' for example.)

b. Reads each line of inputs.txt(This is a list of chemical symbols.)

c. For each line, prints out the element name10 minutes

Page 418: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

418

Keys in a dictionary?

“Treat it like a list”

total_weight = 0for symbol in name = symbol_to_name[symbol] print '%s\t%s' % (symbol, name)

symbol_to_name :

Page 419: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

419

“Treat it like a list”

“Treat it like a list and itbehaves like a (useful) list.”

File → List of lines

String → List of letters

Dictionary → List of keys

Page 420: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

420

“Treat it like a list”

for item in list: blah blah …item… blah blah

for key in dictionary: blah blah …dictionary[key]… blah blah

Page 421: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

421

Missing key?

data = {'a':'alpha', 'b':'beta'}>>>

data['g']>>>

Traceback (most recent call last): File "<stdin>", line 1, in <module>KeyError: 'g'

Dictionary equivalent of“index out of range”

Page 422: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

422

“Treat it like a list”

if item in list: blah blah …item… blah blah

if key in dictionary: blah blah …dictionary[key]… blah blah

Page 423: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

423

Convert to a list

keys = list(data)print(keys)

['b', 'a']

Page 424: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

424

Progress

Keys in a dictionary

“Treat it like a list”

list(dictionary) [keys]

for key in dictionary: ...

if key in dictionary: ...

Page 425: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

425

Exercise

Write a function invert()in your utils module.

symbol_to_name 'Li' 'Lithium'

name_to_symbol = invert(symbol_to_name)

name_to_symbol 'Lithium' 'Li'

10 minutes

Page 426: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

426

One last example

Word counting

Given a text, what words appear and how often?

Page 427: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

427

Word counting algorithm

Run through file line-by-line

Clean up word

Run through line word-by-word

Is word in dictionary?

If not: add word as key with value 0

Increment the counter for that word

Output words alphabetically

Page 428: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

428

Word counting in Python: 1

# Set upimport sys

count = {}

data = open(sys.argv[1]) Filename oncommand line

Empty dictionary

Need sys forsys.argv

Page 429: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

429

Word counting in Python: 2

for line in data:

for word in line.split():

Lines

Words

clean_word = (word)cleanup

We needto write thisfunction.

Page 430: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

430

Word counting in Python: 3

def cleanup(word_in): word_out = word_in.lower() return word_out

“Placeholder”function

Insert at start of script

Page 431: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

431

Word counting in Python: 4

Two levelsindented

clean_word = (word)cleanup

if not clean_word in count :

count[clean_word] = 0

count[clean_word] = count[clean_word] + 1

Create newentry indictionary?

Incrementcount for word

Page 432: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

432

Word counting in Python: 5

words = list(count) All the words

count[clean_word] = count[...

words.sort() Alphabeticalorder

data.close() Be tidy!

Page 433: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

433

Word counting in Python: 6

words.sort() Alphabeticalorder

for word in words:

print('%s\t%d' % (word,count[word]))

Page 434: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

434

Run it!

$ python counter.py treasure.txt

What changes would you make to the script?

Page 435: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

435

And we're done!

Python types

Python control structures

Python functions

Python modules

and now you are readyto do things with Python!

Page 436: Python: Introduction for Absolute Beginners · 3 Variables if…then…else… while… loops Comments Lists for… loops Functions Tuples Modules Course outline — 2 Using Python

436

More Python

Python forAbsoluteBeginners

Python:Further topics

Python forProgrammers

Python:Regularexpressions

Python:Object orientedprogramming

Python:Checkpointing

Python:O/S access