<jupyter_start><jupyter_text>Algorithm Building BlocksAll programs are made up of three fundamental patterns of control or flow:1. Sequence - one instruction after another2. Selection (Conditional) - A group of instructions may or may not be executed, depending on some condition3. Repetition (Loop) – A group of instructions repeated multiple times---  Sequential StructureAll programs are made up of combinations of these patterns (plus one more we’ll discuss later).  The simplest pattern, sequence, means that one computer instruction, or “line of code” is executed after another, in sequence.  It is the default pattern.<jupyter_code>%reset -f
x = 5
y = 10
x = x + y
print(x)<jupyter_output>15<jupyter_text>In a Jupyter notebook, the instructions in a cell are executed, following the pattern(s) in the code in that cell.  The code in cells form a continuous program, but the order depends on the order of the cells the user chooses to execute.<jupyter_code>x = 5
y = 10
x = x + y
x = x*2
x + 4<jupyter_output><empty_output><jupyter_text>Selection StructuresSelection is a pattern where the flow of execution branches one way or another, depending on a condition.  The Python statements related to selection are `if` statements.**`if` statement**In the simple if statement, the body of the if statement (the indented instructions following the colon at the end of the if) is only executed if the condition evaluates to True.  The statements following the if statement body that are indented at the same level as the if are always executed regardless of the value of the condition.<jupyter_code>y = 2
x = int(input('Enter value for x (numeric)'))
if x < 10:
    y = 20
y = y * 2
print(y)<jupyter_output>Enter value for x (numeric) 9<jupyter_text>**`if-else` statement**When there are 2 possibilities, an if-else is used.  The else clause (the statements indented after the else statement) are executed if the condition specified with the if is False.  The else is never followed by a condition, just a colon.  In the example above, the body of the if (y = 20) is executed if x = 10).<jupyter_code>x = int(input('Enter value for x (numeric)'))
if x < 10:
    y = 20
else:
    y = 2
y = y * 2
print(y)<jupyter_output>Enter value for x (numeric) 9<jupyter_text>**`if-elif-else` statement**When there are multiple possibilities, the the `if-elif-else` statement is used.  An `if` statement may have just one `if` and one `else` clause, but it may have multiple `elif` (else if) clauses.  In the example, the `else` is executed only if x is greater than 10, because the cases of x less than 10 and x equal to 10 are already covered.  Notice that the `==` operator is used to test for equality, because `=` is used for assignment.  Each `elif` clause must have its own condition, followed by a colon.<jupyter_code>x = int(input('Enter value for x (numeric)'))
if x < 10:
    y = 20
elif x == 10:
    y = 4
else:
    y = 2
y = y * 2
print(y)<jupyter_output>Enter value for x (numeric) 9<jupyter_text>--- Repetition (Loop) StructuresObviously, a powerful part of programming is that you can write a single piece of code, and execute it as many times as needed, from once to millions of times.Python has two types of loop structures, the `for` loop and the `while` loop. **`for` loop** The `for` loop iterates (loops) over a set of values, and it is used along with the `in` keyword.The simplest version iterates over a set of integers created using the `range()` function.<jupyter_code>for index in range(3):
        print( index )<jupyter_output>0
1
2<jupyter_text>In this case, `range()` produces the set of integers from 0 to 2, inclusive. Notice how the last value 3 is not returned by range().  This behavior is python specific, and range() returns values up to, but not including the stopping value - another way to interpret is start at 0 and produce 3 values, including the zero - the syntax below is more reflective of such thinking:<jupyter_code>for index in range(0,3):
        print( index )<jupyter_output>0
1
2<jupyter_text>**`while` loop** The while loop iterates (loops) while some condition remains True.The condition is specified by a Boolean expression (True/False)<jupyter_code>x = 0
y = 10
print(x,y)
while x < y:
    x += 2
    y -= 2
print(x,y)<jupyter_output>0 10
6 4<jupyter_text>The statements in the loop body are executed while the condition x < y remains True. As with the for loop, the while loop statement ends in a colon, and the loop body is indented (and ends when the indentation level returns to the same level as the while.A Boolean expression is any expression that evaluates to True or False.  Expression that include the relational operators or the logical operators are Boolean expressions.  Also, the integer value 0 is False and a non-zero value is True, and there are other special cases, for example, the empty string “” is False.With while loops, the usefulness of the := (assignment that also evaluates) becomes clear.<jupyter_code>while (str := input("Please enter a string or 'quit' to stop: ")) != 'quit':
    print( "Your string's length is " )
    print( len(str) )<jupyter_output>Please enter a string or 'quit' to stop:  what is my name<jupyter_text>versus<jupyter_code>str = input("Please enter a string or 'quit' to stop: ")
while str != 'quit':
    print( "Your string's length is " )
    print( len(str) )
    str = input("Please enter a value or 'quit' to stop: ")<jupyter_output>Please enter a string or 'quit' to stop:  what is my name
{'fraction_non_alphanumeric': 0.07829570284049527, 'fraction_numerical': 0.013292061179898033, 'mean_word_length': 4.390578999018646, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '418596', 'license': 'CC0-1.0', 'n_tokens_mistral': 1638, 'n_tokens_neox': 1544, 'n_words': 821, 'path': 'site/1-programming/8-algorithmbuildingblocks/algorithmbuildingblocks.ipynb', 'repo_name': 'dustykat/p4e', 'size': '5492'}