Slide 1

Slide 1 text

Introduction to Programming with Python Ignas Rudaitis, 2020 Draft version 2

Slide 2

Slide 2 text

In what ways are programs like (or unlike) sequences of actions?

Slide 3

Slide 3 text

A B C D E F G Sequence of actions A B C D E F G A() B() C() D() E() F() G() Code

Slide 4

Slide 4 text

A B C D E F G Sequence of actions A B C D E F G A() B() C() D() E() F() G() Code

Slide 5

Slide 5 text

A B C D E F G Sequence of actions A B C D E F G A() B() C() D() E() F() G() Code

Slide 6

Slide 6 text

A() B() for i in range(5): D() E() F() G() Code A B C D E F G A B C D E Loop (for ... in range) D E D E D E D E F G 5 times

Slide 7

Slide 7 text

A() B() for i in range(5): D() E() F() G() Code A B C D E F G A B C D E Loop (for ... in range) D E D E D E D E F G 5 times

Slide 8

Slide 8 text

A() B() for i in range(5): D() E() F() G() Code A B C D E F G A B C D E Loop (for ... in range) D E D E D E D E F G 5 times

Slide 9

Slide 9 text

A() B() for i in range(5): D() E() F() G() Code A B C D E F G A B C D E Loop (for ... in range) D E D E D E D E F G 5 times

Slide 10

Slide 10 text

A B C D E H I F G + - A B C D E H I A B C F G H I Conditional (if ... else) A() B() if C(): D() E() else: F() G() H() I() Code

Slide 11

Slide 11 text

A B C D E H I F G + - A B C D E H I A B C F G H I Conditional (if ... else) A() B() if C(): D() E() else: F() G() H() I() Code

Slide 12

Slide 12 text

A B C D E H I F G + - A B C D E H I A B C F G H I Conditional (if ... else) A() B() if C(): D() E() else: F() G() H() I() Code

Slide 13

Slide 13 text

This action might result in two distinct outcomes, e.g. the action “check if it’s raining outside”. The remainder of the action sequence then depends on the particular outcome.

Slide 14

Slide 14 text

Conditional within a loop A B C F G D E + - 3 times A B C F C B F C B F G A B D F C B F C B F G E , , A B D F C B F C B F G E , A B D F B F C B F G E D E , ... A() for i in range(3): if B(): C() else: D() E() F() G() Code 4 more

Slide 15

Slide 15 text

Conditional within a loop A B C F G D E + - 3 times A B C F C B F C B F G A B D F C B F C B F G E , , A B D F C B F C B F G E , A B D F B F C B F G E D E , ... A() for i in range(3): if B(): C() else: D() E() F() G() Code 4 more

Slide 16

Slide 16 text

Conditional within a loop A B C F G D E + - 3 times A B C F C B F C B F G A B D F C B F C B F G E , , A B D F C B F C B F G E , A B D F B F C B F G E D E , ... A() for i in range(3): if B(): C() else: D() E() F() G() Code 4 more

Slide 17

Slide 17 text

A G 3 times - do something A() for i in range(3): do_something() G() Code

Slide 18

Slide 18 text

A G 3 times - do something B C F D E - what exactly? A() for i in range(3): do_something() G() Code

Slide 19

Slide 19 text

A B C F C B F C B F G A B D F C B F C B F G E , , A B D F C B F C B F G E , A B D F B F C B F G E D E , ... 4 more In total, 2 × 2 × 2 = 8 distinct sequences of actions are possible here, given that in every one of the three repetitions action B might yield a different outcome.

Slide 20

Slide 20 text

Transporting data with variables

Slide 21

Slide 21 text

Display Ask for text: Enter your name. Insert at the beginning: Hello, Display

Slide 22

Slide 22 text

The result of this action may be any piece of text, which will fill the slot as soon as the action completes. Ask for text:

Slide 23

Slide 23 text

The slots in these actions are associated using colors and arrows. This is to indicate that once a slot’s content changes, an identical change also takes place in all others that are associated.

Slide 24

Slide 24 text

Display Ask for text: Enter your name. Insert at the beginning: Hello, Display

Slide 25

Slide 25 text

Enter your name. Display Ask for text: Enter your name. Insert at the beginning: Hello, Display

Slide 26

Slide 26 text

Display Ask for text: Enter your name. Insert at the beginning: Hello, Display

Slide 27

Slide 27 text

OK Display Ask for text: Enter your name. Insert at the beginning: Hello, Display

Slide 28

Slide 28 text

R OK Display Ask for text: Enter your name. Insert at the beginning: Hello, Display

Slide 29

Slide 29 text

Ro OK Display Ask for text: Enter your name. Insert at the beginning: Hello, Display

Slide 30

Slide 30 text

Rom OK Display Ask for text: Enter your name. Insert at the beginning: Hello, Display

Slide 31

Slide 31 text

Rome OK Display Ask for text: Enter your name. Insert at the beginning: Hello, Display

Slide 32

Slide 32 text

Romeo OK Display Ask for text: Enter your name. Insert at the beginning: Hello, Display

Slide 33

Slide 33 text

Romeo OK Display Ask for text: Enter your name. Insert at the beginning: Hello, Display

Slide 34

Slide 34 text

Display Ask for text: Enter your name. Romeo Insert at the beginning: Hello, Romeo Display Romeo

Slide 35

Slide 35 text

Display Ask for text: Enter your name. Romeo Insert at the beginning: Hello, Romeo Display Romeo

Slide 36

Slide 36 text

Display Ask for text: Enter your name. Hello, Romeo Insert at the beginning: Hello, Hello, Romeo Display Hello, Romeo

Slide 37

Slide 37 text

Display Ask for text: Enter your name. Hello, Romeo Insert at the beginning: Hello, Hello, Romeo Display Hello, Romeo

Slide 38

Slide 38 text

Hello, Romeo Display Ask for text: Enter your name. Hello, Romeo Insert at the beginning: Hello, Hello, Romeo Display Hello, Romeo

Slide 39

Slide 39 text

display('Enter your name.') t = ask_for_text() t = 'Hello, ' + t display(t)

Slide 40

Slide 40 text

If, for instance, we put a u instead of t here, like we just did, it would mean that this action’s slot would receive a “different color” and would therefore be no longer synchronized with the rest of them, unless... display('Enter your name.') t = ask_for_text() u = 'Hello, ' + t display(t)

Slide 41

Slide 41 text

...unless we were to use u elsewhere as well ‒ in which case, another process of synchronization would occur as well, this time among the blue slots. display('Enter your name.') t = ask_for_text() u = 'Hello, ' + t display(t) If, for instance, we put a u instead of t here, like we just did, it would mean that this action’s slot would receive a “different color” and would therefore be no longer synchronized with the rest of them, unless...

Slide 42

Slide 42 text

Example: cleaning up the desktop

Slide 43

Slide 43 text

Go to Create folder Count files, of which there are: Desktop Oldies 0 Repeat times 0 Select file # 1 Is the file older than ? 1 Jan 2015 Cut yes no Go to Oldies Paste Go up a level Increase by one: 1

Slide 44

Slide 44 text

0 0 1 1

Slide 45

Slide 45 text

go_to('Desktop') create_folder('Oldies') n = count_files() k = 1 for i in range(n): select_file(k) if file_older_than('1 Jan 2015'): cut() go_to('Oldies') paste() go_up_a_level() else: k += 1

Slide 46

Slide 46 text

n = count_files() k = 1 for i in range(n): select_file(k) k += 1

Slide 47

Slide 47 text

What kinds of actions will be available in “real-world” programming?

Slide 48

Slide 48 text

# “Listen” for a string of text from the user (up to Return) text = input() # Add “rocks!” to the end of the string, with an extra space # at the beginning and another one at the end text += ' rocks! ' # Triple the string (attach two copies of it at the end) text *= 3 # Show the final string to the user print(text)

Slide 49

Slide 49 text

# “Listen” for a string of text from the user (up to Return) line = input() # Add “rocks!” to the end of the string, with an extra space # at the beginning and another one at the end line += ' rocks! ' # Triple the string (attach two copies of it at the end) line *= 3 # Show the final string to the user print(line)

Slide 50

Slide 50 text

# “Listen” for a string of text from the user (up to Return) cookie = input() # Add “rocks!” to the end of the string, with an extra space # at the beginning and another one at the end cookie += ' rocks! ' # Triple the string (attach two copies of it at the end) cookie *= 3 # Show the final string to the user print(cookie)

Slide 51

Slide 51 text

# “Listen” for a string of text from the user (up to Return) zxcvbnm = input() # Add “rocks!” to the end of the string, with an extra space # at the beginning and another one at the end zxcvbnm += ' rocks! ' # Triple the string (attach two copies of it at the end) zxcvbnm *= 3 # Show the final string to the user print(zxcvbnm)

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

Actions (functions) y = input() Asks for a line of text; saves it to y. print(x) Displays a line with the text in x. y = x.replace(a, b) Takes a piece of text x, replaces all occurrences of a within it with the piece b, and saves the result to y. y = len(x) Measures the length of the text in x; saves it to y. y = x[k] Extracts the (k + 1)-th character from the text in x; saves it to y. y = a + b Attaches the text in b to the end of the text in a, saving the result to y. y = a * k Repeats the piece of text in a (k times), saving the result to y.

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

Sequencing actions through function application

Slide 56

Slide 56 text

Ask for text: In the text string , replace with , giving Display kitten Ask for text:

Slide 57

Slide 57 text

OK Ask for text: In the text string , replace with , giving Display kitten Ask for text:

Slide 58

Slide 58 text

S OK Ask for text: In the text string , replace with , giving Display kitten Ask for text:

Slide 59

Slide 59 text

Se OK Ask for text: In the text string , replace with , giving Display kitten Ask for text:

Slide 60

Slide 60 text

Sea OK Ask for text: In the text string , replace with , giving Display kitten Ask for text:

Slide 61

Slide 61 text

Sea l OK Ask for text: In the text string , replace with , giving Display kitten Ask for text:

Slide 62

Slide 62 text

Sea li OK Ask for text: In the text string , replace with , giving Display kitten Ask for text:

Slide 63

Slide 63 text

Sea lio OK Ask for text: In the text string , replace with , giving Display kitten Ask for text:

Slide 64

Slide 64 text

Sea lion OK Ask for text: In the text string , replace with , giving Display kitten Ask for text:

Slide 65

Slide 65 text

Sea lions OK Ask for text: In the text string , replace with , giving Display kitten Ask for text:

Slide 66

Slide 66 text

Sea lions OK Ask for text: In the text string , replace with , giving Display kitten Ask for text:

Slide 67

Slide 67 text

Ask for text: In the text string , replace with , giving Sea lions Display kitten Ask for text: Sea lions

Slide 68

Slide 68 text

OK Ask for text: In the text string , replace with , giving Sea lions Display kitten Ask for text: Sea lions

Slide 69

Slide 69 text

l OK Ask for text: In the text string , replace with , giving Sea lions Display kitten Ask for text: Sea lions

Slide 70

Slide 70 text

li OK Ask for text: In the text string , replace with , giving Sea lions Display kitten Ask for text: Sea lions

Slide 71

Slide 71 text

lio OK Ask for text: In the text string , replace with , giving Sea lions Display kitten Ask for text: Sea lions

Slide 72

Slide 72 text

lion OK Ask for text: In the text string , replace with , giving Sea lions Display kitten Ask for text: Sea lions

Slide 73

Slide 73 text

lion OK Ask for text: In the text string , replace with , giving Sea lions Display kitten Ask for text: Sea lions

Slide 74

Slide 74 text

Ask for text: lion In the text string , replace with , giving Sea lions Display lion kitten Ask for text: Sea lions

Slide 75

Slide 75 text

Ask for text: lion In the text string , replace with , giving Sea lions Display lion kitten Ask for text: Sea lions

Slide 76

Slide 76 text

Ask for text: lion In the text string , replace with , giving Sea lions Display Sea kittens lion kitten Sea kittens Ask for text: Sea lions

Slide 77

Slide 77 text

Sea kittens Ask for text: lion In the text string , replace with , giving Sea lions Display Sea kittens lion kitten Sea kittens Ask for text: Sea lions

Slide 78

Slide 78 text

Ask for text: lion In the text string , replace with , giving Sea lions Display Sea kittens lion kitten Sea kittens Ask for text: Sea lions

Slide 79

Slide 79 text

= input() Sea lions  Ask for text: lion In the text string , replace with , giving Sea lions Display Sea kittens lion kitten Sea kittens

Slide 80

Slide 80 text

= input() lion = input() Sea lions   In the text string , replace with , giving Sea lions Display Sea kittens lion kitten Sea kittens

Slide 81

Slide 81 text

ž = .replace( , ) Sea lions lion kitten Sea kittens    Display Sea kittens = input() lion = input() Sea lions

Slide 82

Slide 82 text

= input() lion ž = .replace( , ) Sea lions print( ) Sea kittens lion kitten Sea kittens = input() Sea lions    

Slide 83

Slide 83 text

x = input() y = input() z = x.replace(y, 'kitten') print(z) = = input() lion ž = .replace( , ) Sea lions print( ) Sea kittens lion kitten Sea kittens = input() Sea lions

Slide 84

Slide 84 text

ž = .replace( , ) Sea lions print( ) Sea kittens lion kačiuk Sea kittens = input() Sea lions = input() lion x = input() y = input() z = x.replace(y, 'kitten') print(z) =

Slide 85

Slide 85 text

= input() lion ž = .replace( , ) Sea lions print( ) Sea kittens lion kitten Sea kittens = input() Sea lions x = input() y = input() z = x.replace(y, 'kitten') print(z) =

Slide 86

Slide 86 text

= input() lion ž = .replace( , ) Sea lions print( ) Sea kittens lion kitten Sea kittens = input() Sea lions x = input() y = input() z = x.replace(y, 'kitten') print(z) =

Slide 87

Slide 87 text

= input() lion ž = .replace( , ) Sea lions print( ) Sea kittens lion kitten Sea kittens = input() Sea lions x = input() y = input() z = x.replace(y, 'kitten') print(z) =

Slide 88

Slide 88 text

= input() lion ž = .replace( , ) Sea lions print( ) Sea kittens lion kitten Sea kittens = input() Sea lions

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

= input() lion ž = .replace( , ) Sea lions print( ) Sea kittens lion kitten Sea kittens

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

No content

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

ž = input().replace(input(), ) print( ) Sea kittens kitten Sea kittens

Slide 102

Slide 102 text

print( ) Sea kittens ž = input().replace(input(), ) Sea kittens kačiuk

Slide 103

Slide 103 text

No content

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

No content

Slide 107

Slide 107 text

print(input().replace(input(), )) kitten

Slide 108

Slide 108 text

print(input().replace(input(), 'kitten'))

Slide 109

Slide 109 text

print(input().replace(input(), 'kitten')) x = input() y = input() z = x.replace(y, 'kitten') print(z) =

Slide 110

Slide 110 text

Thank you for your attention!