Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Programming with Python

Introduction to Programming with Python

Covers several elementary topics in imperative programming. Largely language-agnostic, but uses Python 3 for all examples. A toy library is assumed in the beginning.

Ignas Rudaitis

January 30, 2020
Tweet

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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.
  12. 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
  13. 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
  14. 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
  15. A G 3 times - do something A() for i

    in range(3): do_something() G() Code
  16. A G 3 times - do something B C F

    D E - what exactly? A() for i in range(3): do_something() G() Code
  17. 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.
  18. 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:
  19. 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.
  20. Enter your name. Display Ask for text: Enter your name.

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

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

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

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

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

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

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

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

    Romeo Insert at the beginning: Hello, Hello, Romeo Display Hello, Romeo
  29. 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)
  30. ...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...
  31. 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
  32. 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
  33. # “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)
  34. # “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)
  35. # “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)
  36. # “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)
  37. 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.
  38. Ask for text: In the text string , replace with

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    with , giving Sea lions Display lion kitten Ask for text: Sea lions
  58. 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
  59. 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
  60. 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
  61. = input() Sea lions  Ask for text: lion In

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

    the text string , replace with , giving Sea lions Display Sea kittens lion kitten Sea kittens
  63. ž = .replace( , ) Sea lions lion kitten Sea

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

    print( ) Sea kittens lion kitten Sea kittens = input() Sea lions    
  65. 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
  66. ž = .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) =
  67. = 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) =
  68. = 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) =
  69. = 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) =
  70. = input() lion ž = .replace( , ) Sea lions

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

    print( ) Sea kittens lion kitten Sea kittens