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.
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
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
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
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.
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)
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...
(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)
(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)
(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)
(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)
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.