ITSE 1359 | PyThon Scripting

ACC

Sequence-Selection-Repetition

Foreword

All programming languages have these three building blocks in common: sequence, selection, and repetition. These three control structures were first formally proposed in the Structured Program Theorem. Just as the three simple component groups of DNA (phosphate, sugar, nitrogen) can be used to describe all life, all program solutions are built using these simple constructs. In this chapter, we take a brief look at each of these. Selection and repetition will be addressed more thoroughly in subsequent chapters.

Sequence

The sequence construct is the simplest and is so obvious it almost 'goes without saying'. It consists of performing one action, then another, then another, and so on. It is performing one step after another in a sequence. That's it. All of the program examples we have reviewed thus far have consisted primarily of the sequence construct. Let's look at another example. Create a new project in PyCharm called project-three-constructs. Create a new file, name it sequence.py, and code it as shown. Each of the lines in this example represent a single step. Line 1 is a comment and therefore not processed by the interpreter. Line 3 is step one and line 4 is step two. Lines 6 and 7 are steps three and four. The remaining steps are shown as the print statements.

Here's the output.

The term 'sequence' in this chapter is used in the generic sense, as it applies to any programming language. It is not to be confused with the 'sequence' types in Python. Sequence types include strings, lists, etc. Sequence types will be covered thoroughly in subsequent chapters. See this link for more about sequence types.

Selection

Selection is the second of the three constructs and is not much more complex than sequence, at least in its simplest form. Selection consists of making a decision. iPhone or Galaxy, left or right, Aspen or Vail? Create a new file in the project-three-constructs project and name the file selection.py. Code it as shown.

On line 3 the user is asked to enter a favorite color. Lines 6-14 contain an 'if-elif-else statement' which is one of the selection constructs in the program. In that statement, the question is posed, 'if fav_color is equal to Yellow and fav_number is equal to 77 proceed to line 7'. If fav_color is not equal to Yellow and/or fav_number is not equal to 77, then lines 7-8 will be skipped and the program flow will proceed to line 9 and so on. By the way, note that parentheses are not required around the condition on lines 6, 9, or other conditions. Many languages require conditions to be enclosed in parentheses but Python does not.

See the two output examples below. In the first, the user entered Yellow and 77 lines 7-8 were processed. Confirm that both lines were printed in the output and by running the code yourself. In the second output, the user enter Blue and 9 and lines 7-8 were skipped but line 14was processed.

Note that the indentation of lines makes those lines part of the block. For instance, lines 7-8 and 10-12 are indented the same amount and are therefore part of the 'if statement'. Most other languages use braces {} to define program blocks (separate sections of code). Python uses indentation to define program blocks. We will see more examples of this in subsequent chapters.

Line 19 contains an if statement which states 'If first_number is less than second_number then do the statement on line 20'. If first_number is not less than second_number then line 21 is processed and that condition is evaluated. On line 21, if first_number is also not greater than second_number then lines 23 and 24 are processed. Line 23 is called a trailing else. The statement(s) in the trailing else block will be executed if none of the statements above are true. So, in this case, if neither the < or > condition is true, then the == condition must be true.

Here's the output when Yellow is input as the favorite color.

Here's the output when Blue is input as the favorite color and 9 is entered as the favorite number. Notice that the body of the 'if' statement on lines 5 and 6 is skipped.

Repetition

As the name suggests, repetition entails performing steps repeatedly until a condition is met. Repetition is also known as iteration. When performing repetition, a program may reserve seats until all have been selected, pump fuel until the full level has been reached, run a compressor until a temperature has been achieved, etc. Like the other two constructs, in its simplest form, repetition should be fairly easy to understand. Create a new file and name the file repetition.py. Code it as shown.

On line 6, the user is asked if there are strawberry sales to enter. This type of entry before a loop that uses the information (more_sales_data) in the condition (more_sales_data == "y") is known as a 'priming read'. The value of more_sales_data must be given an initial value before encountering the beginning of the loop on line 14. A list data type is created on line 12 to store data on line 18. The contents of the list are displayed on lines 32-48.

What would happen if line 6 were commented and therefore not run? Try it. You will see those two words, Try it, many times throughout the course. To learn to code, it is essential to have the four C's of the modern software developer - curiosity, capability, confidence, and courage to try new approaches and ideas on a regular basis.

The while statement on line 14 represents a loop which consists of lines 14-28. The statement is read as 'while more_sales_data is equal to y, perform the steps in the loop.' Notice that if the user enters 'y' when line 6 is executed, s/he intends to execute the loop. Therefore, more_sales_data will equal 'y' the first time line 14 is encountered and the loop will be processed. Line 15 asks the user to enter a number for sales in pints. The int() built-in function on line 15 converts the keyboard entry into an integer so it can be used in the calculation on line 21. What would happen if the built-in function int() were removed from line 15? Try it.

Line 21 uses a compound addition operator (+=) which is shorthand for writing:

total_sales_data = total_sales_data + sales

The variable total_sales_data is known as an accumulator (or running total) since it accumulates the new_sales_data values each time the loop is processed. It is initialized to 0 on line 8.

Recall that line 6 was known as a priming read. The value for more_sales_data is obtained from the user. When we see a priming read outside of the loop, then, in almost all cases a very similar statement will also exist near the bottom of the loop to ask the user if s/he wishes to continue.

Notice that line 27 is almost identical to line 6 (only one more word). As long as the user enters 'y' on line 27, the condition on line 14 will be true, and the loop will execute again. When the user enters a value other than 'y' in response to the question on line 27, the condition on line 14 will be false and the loop will end. The accumulator is output on line 30 to display the total sales in pints that were entered. A for loop is used on lines 33-34 to output the contents of the list that were appended each time through the while loop on line 18.

A while loop is included on lines 38-40 to demonstrate the use of an index 'i' to access the contents of a sequence. There are two important differences between the for and while loops as shown. When we use the for loop like on line 33, we don't need to know the number of values in the list. The for loop does the counting for us and iterates until the end. With the while loop, we need to know the size of the list and we have that in the variable sales_number.The second difference between these two loop types is that with the while loop, the index value is necessary and tracked and is therefore available within the loop which is useful in many situations.

Two more variations of the for loop are shown on lines 43 and 47. On line 43, the built-in function range() is used to iterate through the values in the loop up to the length of the list_sales.

On line 47, the built-in function enumerate() is used to return both the iterator i and the values in list_sales.

Here's the output.