For Loops - Part 1 - Programming Techniques PDF
Document Details
Uploaded by MiraculousCyan
Tags
Summary
This document provides an introduction to programming for loops, focusing on part 1. It covers fundamental concepts of for loops and basic programming techniques.
Full Transcript
2.2 Programming Techniques For loops - Part 1 Printing a loop variable Start print ("What is your SELECT * FROM name") Objectives You will apply: The u...
2.2 Programming Techniques For loops - Part 1 Printing a loop variable Start print ("What is your SELECT * FROM name") Objectives You will apply: The use of the three basic programming constructs used to control the flow of a program: Sequence Selection Iteration (count- and condition-controlled loops) Contents 1.The three programming constructs 2.Introduction to For loops 3.Printing a loop variable - Program A 4.Printing a loop variable - Program B 5.For loop syntax 6.The step parameter 7.Printing a loop variable - Program C 8.Count-controlled loops The three programming constructs Information Three programming constructs Programs are designed using three programming constructs: 1. Sequence ? 2. Selection 3. Iteration Information Iteration Iteration is when code is executed repeatedly. It is also called loops or repetition. There are different type, which includes: While Do until (Not available in python) For Introduction to For loops Discussion How many times will this code repeat? password = input ("What is the password?") while password != "1234": password = input("Incorrect, try again: ") print("Access granted") It is not known the number of times the loop will repeat as it is unknown how many attempts it will take the user to enter their password correctly, e.g. it could be the first attempt, second attempt, fifth attempt. Information For loops For loops repeat a block of code for a set number of times. They are used when it is known in advance (i.e. before entering the loop) the exact number of times the block of code needs to be repeated. For loops have many uses, including: This presentation will focus on this 1.Printing the loop variable. 2.Repeating an action. 3.Working with arrays and strings. 4.Combining for loops with selection. Printing a loop variable - Program A Discussion What is the output for program A? # Program A for counter in range(4): print (counter) 0 1 2 3 Example How program A works for counter in range(4): print (counter) counter 0 Output Example How program A works for counter in range(4): print (counter) counter 0 Output 0 Example How program A works for counter in range(4): print (counter) counter 1 Output 0 Example How program A works for counter in range(4): print (counter) counter 1 Output 0 1 Example How program A works for counter in range(4): print (counter) counter 2 Output 0 1 Example How program A works for counter in range(4): print (counter) counter 2 Output 0 1 2 Example How program A works for counter in range(4): print (counter) counter 3 Output 0 1 2 Example How program A works for counter in range(4): print (counter) counter 3 Output 0 1 2 3 Example Prints the numbers 0 to 3: for counter in range(4): print (counter) Modify the for loop so that it prints all the numbers from 0 to 10. for counter in range(11): print(counter) Modify the for loop so that it prints all the numbers from 0 to 25. for counter in range(26): print(counter) Modify the for loop so that it prints all the numbers from 0 to 100. for counter in range(101): print(counter) Printing a loop variable - Program B Discussion What is the output for program B? # Program B for counter in range(1,4): print (counter) 1 2 3 Example How program B works for counter in range(1,4): print (counter) counter 1 Output Example How program B works for counter in range(1,4): print (counter) counter 0 Output 1 Example How program B works for counter in range(1,4): print (counter) counter 2 Output 1 Example How program B works for counter in range(1,4): print (counter) counter 2 Output 1 2 Example How program B works for counter in range(1,4): print (counter) counter 3 Output 1 2 Example How program B works for counter in range(1,4): print (counter) counter 3 Output 1 2 3 Example Prints the numbers 1 to 3: for counter in range(1,4): print (counter) Modify the for loop so that it prints all the numbers from 5 to 10. for counter in range(5,11): print(counter) Modify the for loop so that it prints all the numbers from 10 to 25. for counter in range(10,26): print(counter) Modify the for loop so that it prints all the numbers from 100 to 105. for counter in range(100,106): print(counter) For loop syntax Information For loop syntax For loops consist of two main parts: A header specifying the iteration which consists of the: For and in keywords Loop variable (named counter) Sequence for counter in sequence: block of code A body specifying the block of code that will be executed repeatedly, once per iteration. Information Indentation The code that is part of the body of a for loop must be indented. Python automatically indents code where needed; however, a line can be manually indented by pressing the tab key once. for counter in range (0, 5): print("Hello") for counter in range (0, 5): print("Hello") print("Welcome") Information Loop variable The loop variable is a part of For loops (named "counter" in this example). Loop variable for counter in sequence It can be given any name by the user. Common names include: counter, count i, j, k, x, y It can be modified using the range() function. The loop variable keeps track of and controls how many times the loop has repeated. Information Range() function The range() function generates a list of numbers. It can be used to modify the loop variable. It has 3 parameters: Start Stop Step (Looked at later). It specifies at which number the loop variable will start. The default is 0. It is optional. for counter in range ( start , stop ): print(counter) It specifies at which number the loop variable will stop. It is required. Comparison Including the start parameter for Program A Method 1 - without start Method 2 - with start parameter parameter Sto Star Sto p t p for counter in for counter in range( 0 , range( 4 ): 4 ): print (counter) print (counter) The stop parameter has been included in both methods. However, the start parameter has not been included in method 1 as it is optional. If the start parameter is 0, there is no need to include it (as with method 1) because the default is 0. Method 1 is thus slightly more efficient than method 2. The step parameter Information The step parameter Step is the third parameter in the range() function and it specifies how much the loop variable is incremented with each iteration. It specifies the increment value for the loop variable. The default is 1. It is optional. for counter in range ( start , stop , step ): print(counter) Printing a loop variable - Program C Discussion What is the output for program C? # Program C for counter in range(1, 10, 2): print (counter) 1 3 5 7 9 Example How program C works for counter in range(1, 10, 2): print (counter) counter 1 Output Example How program C works for counter in range(1, 10, 2): print (counter) counter 1 Output 1 Example How program C works for counter in range(1, 10, 2): print (counter) counter 3 Output 1 Example How program C works for counter in range(1, 10, 2): print (counter) counter 3 Output 1 3 Example How program C works for counter in range(1, 10, 2): print (counter) counter 5 Output 1 3 Example How program C works for counter in range(1, 10, 2): print (counter) counter 5 Output 1 3 5 Example How program C works for counter in range(1, 10, 2): print (counter) counter 7 Output 1 3 5 Example How program C works for counter in range(1, 10, 2): print (counter) counter 7 Output 1 3 5 7 Example How program C works for counter in range(1, 10, 2): print (counter) counter 9 Output 1 3 5 7 Example How program C works for counter in range(1, 10, 2): print (counter) counter 9 Output 1 3 5 7 9 Comparison Including the step parameter for Program B Method 1 - without step Method 2 - with step parameter parameter Star Sto Star Sto t p t p for counter in range( 1 , 4 ): for counter in range ( 1 , print (counter) 4 , 1 ): print(counter) Ste p The start and stop parameters have been included in both methods. However, the step parameter hasn’t been included in method 1 as it is optional. If the step parameter is 1, there is no need to include it (as with method 1) because the default is 1. Method 1 is thus slightly more efficient than method 2. Example What is the output? for counter in range(start, stop, step): print (counter) 1. Print numbers from 0 to 6. for counter in range(7): print (counter) 2. Print the numbers 1 and 2. for counter in range(1,3): print (counter) 3. Print the numbers 2,3 and 4. for counter in range(2,5): print (counter) Example for counter in range(start, stop, step): print (counter) 4. Print the numbers 1, 3 and 5. counter 0 1 2 3 4 5 6 START STEP ST OP for counter in range(1,6,2): print 5. Print the (counter) numbers 2, 4, 6, 8 and 10. counter 0 1 2 3 4 5 6 7 8 9 10 11 START STEP STEP STEP ST OP for counter in range(2,11,2): print (counter) Example for counter in range(start, stop, step): print (counter) 6. Print the numbers 0, 1, 2, 3 and 4 in reverse order. counter -1 0 1 2 3 4 5 6 ST START OP REVERSE - 1 for counter in range(4,- 1,-1): 7. Printprint (counter)0, 3, 6 and 9 in reverse order. the numbers counter -1 0 1 2 3 4 5 6 7 8 9 10 11 ST STEP STEP START OP REVERSE -3 for counter in range(9,- 1,-3): Information Summary of the range() function It specifies at which number the loop variable will start. The default is 0. It is optional. for counter in range ( start , stop , step ): print(counter) It specifies the increment value of the loop variable. The default is 1. It is optional. It specifies at which number the loop variable will stop. It is required. Information Count-controlled loop For loops are count-controlled loops as they iterate for a specific number of times. They use a loop variable to control how many times the loop repeats. They are used when it is known in advance (i.e. before entering the loop) the exact number of times the block of code needs to be repeated. 2.2 Programming Techniques For loops - Part 2 Repeating an action Start print ("What is your SELECT * FROM name") Objectives You will apply: The use of the three basic programming constructs used to control the flow of a program: Sequence Selection Iteration (count- and condition-controlled loops) Contents 1.Introduction to For loops 2.Repeating an action using for loops Program A Program B Program C Program D Program E 3.Running totals Introduction to For loops Information For loops For loops repeat a block of code for a set number of times. They are used when it is known in advance (i.e. before entering the loop) the exact number of times the block of code needs to be repeated. For loops have many uses, including: 1.Printing the loop variable. This presentation will focus on this 2.Repeating an action. 3.Working with arrays and strings. 4.Combining for loops with selection. Repeating an action using for loops - Program A Discussion What is the output for program A? # Program A for counter in range(5): print("Hello") Hello Hello Hello Hello Hello Example How program A works for counter in range(5): print("Hello") counter 0 Output Example How program A works for counter in range(5): print("Hello") counter 0 Output Hello Example How program A works for counter in range(5): print("Hello") counter 1 Output Hello Example How program A works for counter in range(5): print("Hello") counter 1 Output Hello Hello Example How program A works for counter in range(5): print("Hello") counter 2 Output Hello Hello Example How program A works for counter in range(5): print("Hello") counter 2 Output Hello Hello Hello Example How program A works for counter in range(5): print("Hello") counter 3 Output Hello Hello Hello Example How program A works for counter in range(5): print("Hello") counter 3 Output Hello Hello Hello Hello Example How program A works for counter in range(5): print("Hello") counter 4 Output Hello Hello Hello Hello Example How program A works for counter in range(5): print("Hello") counter 5 Output Hello Hello Hello Hello Hello Comparison Comparing program A with and without using loops Method 1 - without a for loop Method 2 - with a for loop print("Hello") for counter in range(0,5): print("Hello") print("Hello") print("Hello") print("Hello") print("Hello") Method 1 and 2 produce the same output, however, method 1 is inefficient as it has repeated lines of code. Method 2 is more efficient as the duplicated lines of code have been removed by using a for loop. Information Repeating an action using for loops For loops can be used to repeat an action (e.g. printing a string, inputting a number, etc.) for a set number of times. We have looked at how for loops are used to: 1. Print a string a set number of times - Program A. Repeating an action using for loops - Program B Discussion What is the output for program B? # Program B for counter in range(1,6): print(count*2) 2 4 6 8 10 Example How program B works for counter in range(1,6): print(count*2) counter 1 Calculatio ns Output Example How program B works for counter in range(1,6): print(count*2) counter 1 = count * 2 = 1 *2 Calculatio = 2 ns Output 2 Example How program B works for counter in range(1,6): print(count*2) counter 2 Calculatio ns Output 2 Example How program B works for counter in range(1,6): print(count*2) counter 2 = count * 2 = 2 *2 Calculatio = 4 ns Output 2 4 Example How program B works for counter in range(1,6): print(count*2) counter 3 Calculatio ns Output 2 4 Example How program B works for counter in range(1,6): print(count*2) counter 3 = count * 2 = 3 *2 Calculatio = 6 ns Output 2 4 6 Example How program B works for counter in range(1,6): print(count*2) counter 4 Calculatio ns Output 2 4 6 Example How program B works for counter in range(1,6): print(count*2) counter 5 = count * 2 = 4 *2 Calculatio = 8 ns Output 2 4 6 8 Example How program B works for counter in range(1,6): print(count*2) counter 5 Calculatio ns Output 2 4 6 8 Example How program B works for counter in range(1,6): print(count*2) counter 5 = count * 2 = 5 *2 Calculatio = 10 ns Output 2 4 6 8 10 Information Repeating an action using for loops For loops can be used to repeat an action (e.g. printing a string, inputting a number, etc.) for a set number of times. We have looked at how for loops are used to: 1. Print a string a set number of times - Program A. 2. Print the 2 times table - Program B Repeating an action using for loops - Program C Example How program C works for counter in range(3): print("Enter number") counter 0 number = int(input()) number Input - Output Example How program C works for counter in range(3): print("Enter number") counter 0 number = int(input()) number Input - Output Enter number Example How program C works for counter in range(3): print("Enter number") counter 0 number = int(input()) number 4 Input - Output Enter number 4 Example How program C works for counter in range(3): print("Enter number") counter 1 number = int(input()) number 4 Input - Output Enter number 4 Example How program C works for counter in range(3): print("Enter number") counter 1 number = int(input()) number 4 Input - Output Enter number 4 Enter number Example How program C works for counter in range(3): print("Enter number") counter 1 number = int(input()) number 2 Input - Output What happened to the number 4 Enter number 4 stored in the variable number? Enter number 2 It was overwritten by the number 2. Example How program C works for counter in range(3): print("Enter number") counter 2 number = int(input()) number 2 Input - Output Enter number 4 Enter number 2 Example How program C works for counter in range(3): print("Enter number") counter 2 number = int(input()) number 2 Input - Output Enter number 4 Enter number 2 Enter number Example How program C works for counter in range(3): print("Enter number") counter 2 number = int(input()) number 5 Input - Output What happened to the number 2 Enter number 4 stored in the variable number? Enter number 2 It was overwritten by the Enter number 5 number 5. We will now look at a program which adds the inputted numbers. Information Repeating an action using for loops For loops can be used to repeat an action (e.g. printing a string, inputting a number, etc.) for a set number of times. We have looked at how for loops are used to: 1. Print a string a set number of times - Program A. 2. Print the 2 times table - Program B. 3. Input 3 numbers - Program C. Repeating an action using for loops - Program D Example What does program D do? #Program D total = 0 for counter in range (3): print ("Enter number") number = int ( input()) total = total + number print ("The total is",total) It allows the user to input three numbers which are added and then the total sum is outputted. Example How program D works total = 0 for counter in range (3): counter print ("Enter number") total 0 number = int ( input()) total = total + number number print ("The total is",total) Calculatio Input - Output ns Example How program D works total = 0 for counter in range (3): counter 0 print ("Enter number") total 0 number = int ( input()) total = total + number number print ("The total is",total) Calculatio Input - Output ns Example How program D works total = 0 for counter in range (3): counter 0 print ("Enter number") total 0 number = int ( input()) total = total + number number print ("The total is",total) Calculatio Input - Output ns Enter number Example How program D works total = 0 for counter in range (3): counter 0 print ("Enter number") total 0 number = int ( input()) total = total + number number 2 print ("The total is",total) Calculatio Input - Output ns Enter number 2 Example How program D works total = 0 for counter in range (3): counter 0 print ("Enter number") total 2 number = int ( input()) total = total + number number 2 print ("The total is",total) total = total + Calculatio number Input - Output ns 2 = 0 + 2 Enter number 2 What is the previous value of 0 total? What is the new value of total?2 Example How program D works total = 0 for counter in range (3): counter 1 print ("Enter number") total 2 number = int ( input()) total = total + number number 2 print ("The total is",total) Calculatio Input - Output ns Enter number 2 Example How program D works total = 0 for counter in range (3): counter 1 print ("Enter number") total 2 number = int ( input()) total = total + number number 2 print ("The total is",total) Calculatio Input - Output ns Enter number 2 Enter number Example How program D works total = 0 for counter in range (3): counter 1 print ("Enter number") total 2 number = int ( input()) total = total + number number 3 print ("The total is",total) Calculatio Input - Output ns Enter number 2 Enter number 3 Example How program D works total = 0 for counter in range (3): counter 1 print ("Enter number") total 5 number = int ( input()) total = total + number number 3 print ("The total is",total) total = total + Calculatio number Input - Output ns 5 = 2 + 3 Enter number 2 Enter number 3 What is the previous value of 2 total? What is the new value of total?5 Example How program D works total = 0 for counter in range (3): counter 2 print ("Enter number") total 5 number = int ( input()) total = total + number number 3 print ("The total is",total) Calculatio Input - Output ns Enter number 2 Enter number 3 Example How program D works total = 0 for counter in range (3): counter 2 print ("Enter number") total 5 number = int ( input()) total = total + number number 3 print ("The total is",total) Calculatio Input - Output ns Enter number 2 Enter number 3 Enter number Example How program D works total = 0 for counter in range (3): counter 2 print ("Enter number") total 5 number = int ( input()) total = total + number number 10 print ("The total is",total) Calculatio Input - Output ns Enter number 2 Enter number 3 Enter number 10 Example How program D works total = 0 for counter in range (3): counter 2 print ("Enter number") total 15 number = int ( input()) total = total + number number 10 print ("The total is",total) total = total + Calculatio number Input - Output ns 15 = 5 + 10 Enter number 2 Enter number 3 Enter number 10 What is the previous value of 5 total? What is the new value of total?15 Example How program D works total = 0 for counter in range (3): counter 2 print ("Enter number") total 15 number = int ( input()) total = total + number number 10 print ("The total is",total) Calculatio Input - Output ns Enter number 2 Enter number 3 Enter number 10 The total is 15 Information Repeating an action using for loops For loops can be used to repeat an action (e.g. printing a string, inputting a number, etc.) for a set number of times. We have looked at how for loops are used to: 1. Print a string a set number of times - Program A. 2. Print the 2 times table - Program B 3. Input 3 numbers - Program C. 4. Calculate the sum of 3 inputted numbers - Program D. Repeating an action using for loops - Program E Example What does program E do? #Program E total = 0 for counter in range (1,4): print (counter) total = total + counter print (total) It adds all the values of the loop variable to a total and then outputs the sum. Example How program E works total = 0 for counter in range (1,4): counter print (counter) total = total + counter total 0 print (total) Calculatio Output ns Example How program E works total = 0 for counter in range (1,4): counter 1 print (counter) total = total + counter total 0 print (total) Calculatio Output ns Example How program E works total = 0 for counter in range (1,4): counter 1 print (counter) total = total + counter total 0 print (total) Calculatio Output ns 1 Example How program E works total = 0 for counter in range (1,4): counter 1 print (counter) total = total + counter total 1 print (total) total = total + counter Calculatio Output 1 = 0 + ns 1 1 Example How program E works total = 0 for counter in range (1,4): counter 2 print (counter) total = total + counter total 1 print (total) Calculatio Output ns 1 Example How program E works total = 0 for counter in range (1,4): counter 2 print (counter) total = total + counter total 1 print (total) Calculatio Output ns 1 2 Example How program E works total = 0 for counter in range (1,4): counter 2 print (counter) total = total + counter total 3 print (total) total = total + counter Calculatio Output 3 = 1 + ns 2 1 2 Example How program E works total = 0 for counter in range (1,4): counter 3 print (counter) total = total + counter total 3 print (total) Calculatio Output ns 1 2 Example How program E works total = 0 for counter in range (1,4): counter 3 print (counter) total = total + counter total 3 print (total) Calculatio Output ns 1 2 3 Example How program E works total = 0 for counter in range (1,4): counter 3 print (counter) total = total + counter total 6 print (total) total = total + counter Calculatio Output 6 = 3 + ns 3 1 2 3 Example How program E works total = 0 for counter in range (1,4): counter 3 print (counter) total = total + counter total 6 print (total) Calculatio Output ns 1 2 3 6 Information Repeating an action using for loops For loops can be used to repeat an action (e.g. printing a string, inputting a number, etc.) for a set number of times. We have looked at how for loops are used to: 1. Print a string a set number of times - Program A. 2. Print the 2 times table - Program B 3. Input 3 numbers - Program C. 4. Calculate the sum of 3 inputted numbers - Program D. 5. Add and print a set of numbers - Program E. Running totals Information Running totals When adding numbers in your head, a "running total" of the sum so far is kept. This is similar to how python programs must be designed. Example: Thus, as well as needing a total Total = Running + New variable to store the total, an total number additional variable is needed to 28 5 +23 store the running total = 35 2 +7 What is the = 43 8 35 +8 Add Add 5 23 7 8 answer? = 43 Information Calculating a running totals in loops Running loops are commonly used in loops. They are used to: Add a list of numbers or join characters (concatenation). Counting the number of occurrences of an event. It is a variable which is updated with a new value with each iteration of a loop. There are usually three steps to them: 1 Create a running total e.g. total = 0 Outside of the loop 2 Add a number to the e.g. total = Inside of the running total total + 1 loop 3 Print the running total e.g. print (total) Outside of the loop Information Examples of running totals total = total + 1 Adds 1 to the total variable and then stores this result in the same total variable. Updating a variable by adding one is called incrementing. total = total – 1 Subtracts 1 from the total variable and then stores this result in the same total variable. Updating a variable by subtracting one is called decrementing. total = total + new Adds the total and new variables and then stores this result in the total variable. total = total + wages [counter] Information More assignment operators Like the equal symbol (=), += and -= are also assignment operators. They are commonly used in running totals to add or subtract a value from a variable and then store this result in the same variable. It allows code to be more efficient and shorter. They are often referred to as the addition assignment and subtraction assignment operators. Using = assignment Using += or -= operator assignment operators total = total + 1 total += 1 total -= 1 total = total - 1 total +=new total = total + new 2.2 Programming Techniques For loops - Part 3 Working with arrays and strings Start print ("What is your SELECT * FROM name") Objectives You will apply: The use of the three basic programming constructs used to control the flow of a program: Sequence Selection Iteration (count- and condition-controlled loops) The use of arrays (or equivalent) when solving problems, including both one-dimensional (1D) and two-dimensional arrays (2D) Contents 1.Introduction to for loops 2.Printing all of the elements/characters 3.Printing specific elements/characters 4.Adding the elements of an array 5.Running totals revisited 6.Printing and adding elements of an array 7.Count-controlled loops vs. Condition-controlled loops Introduction to for loops Information For loops For loops repeat a block of code for a set number of times. They are used when it is known in advance (i.e. before entering the loop) the exact number of times the block of code needs to be repeated. For loops have many uses, including: 1.Printing the loop variable. 2.Repeating an action. This presentation will focus on this 3.Working with arrays and strings. 4.Combining for loops with selection. Information Working with arrays and strings For loops can be used with arrays and strings to repeat actions (such as adding or printing each element/character) on each element of an array or each character in a string. We will look at how for loops can be used with arrays and strings to: A. Print all of the elements/characters. B. Print some of the elements/characters. C. Add the elements. Printing all of the elements/characters A for loop can be used to print all of the: Elements of an array. Characters of a string. Information Printing all of the elements/characters A for loop can be used to print all of the: Elements of an array. Characters of a string. The stop parameter in the range() function represents the number of elements of the array/characters of a string. array_name = [element1, element2, element3] Specifies the number of elements of the array / number of characters of a string. for loop_variable in range(stop): print(array_name[loop_variable]) The loop variable is used as the index. Information Printing all of the elements of an array #Example 1 – an array of integers highscores = [125 , 63 , 35 , 12 ] for counter in range(4): There are 4 elements in the print(highscores array. [counter]) 12 5 63 35 12 #Example 2 – an array of strings letters = ["C" , "S" , "A"] for counter in range(3): There are 3 elements in the array. print( letters [counter]) C S A Information Printing all of the elements of an array #Example 3 – using the len() function heights = [174.4 , 150.5 , 168.14 ] for counter in range(len(heights)): The stop parameter in the range() print(heights [counter]) function can be replaced by the len() function which counts the number of 174.4 elements. 150.5 168.1 4 #Example 4 – a string subject = "ICT" for counter in range(3): There are 3 characters in the string. print(subject [counter]) I C T Example Printing all of the elements of an array - Program A #Program A letters = ["C","S","A"] for counter in range(3): print(letters[counter]) Example How program A works letters = ["C","S","A"] for counter in range(3): 0 1 2 letters C S A print(letters[counter]) counte r Output Example How program A works letters = ["C","S","A"] for counter in range(3): 0 1 2 letters C S A print(letters[counter]) counte 0 r Output Example How program A works letters = ["C","S","A"] for counter in range(3): 0 1 2 print(letters) letters C S A counte 0 r Output Example How program A works letters = ["C","S","A"] for counter in range(3): 0 1 2 print(C) letters C S A counte 0 r Output C Example How program A works letters = ["C","S","A"] for counter in range(3): 0 1 2 letters C S A print(letters[counter]) counte 1 r Output C Example How program A works letters = ["C","S","A"] for counter in range(3): 0 1 2 print(letters) letters C S A counte 1 r Output C Example How program A works letters = ["C","S","A"] for counter in range(3): 0 1 2 print(S) letters C S A counte 1 r Output C S Example How program A works letters = ["C","S","A"] for counter in range(3): 0 1 2 letters C S A print(letters[counter]) counte 2 r What is the loop variable being used as? of the The index array. Output C S Example How program A works letters = ["C","S","A"] for counter in range(3): 0 1 2 print(letters) letters C S A counte 2 r Output C S Example How program A works letters = ["C","S","A"] for counter in range(3): 0 1 2 print(A) letters C S A counte 2 r Output C S A Comparison Comparing printing all of the elements with and without using loops Method 1 - without a for loop Method 2 - with a for loop wages = wages = [1,4,5,10,20,4,20,45,23,3,13,100,4,3] [1,4,5,10,20,4,20,45,23,3,13,100,4,3] print (wages) for counter in range(14): print (wages) print(wages[counter]) print (wages) print (wages) print (wages) print (wages) Printing all of the elements of an array is print (wages) print (wages) more efficient using a for loop. print (wages) print (wages) print (wages) print (wages) print (wages) print (wages) Printing specific elements/characters A for loop can be used to print specific: Elements of an array. Characters of a string. Information Printing specific elements/characters A for loop can be used to print specific: Elements of an array. Characters of a string. The start, stop and step parameters in the range() function represent: array_name = [element1, element2, element3] It is optional and specifies the element/character to start printing. It is optional and specifies the for loop_variable in range(start , stop , step): increment value. It is required and specifies the element/character to stop printing. print(array_name[loop_variable]) Example letters = ["A","B","C","D","E","F","G"] for x in range(start, stop, step): print (letters[x]) 1. Print all of the letters. for x in range(7): print (letters[x]) 2. Print the letters A, B and C. for x in 0 1 2 3 4 5 6 7 range(0,3): A B C D E F G print (letters[x]) Start Stop 3. Print the letters B and C. 0 1 2 3 4 5 6 7 for x in range(1,3): A B C D E F G print (letters[x]) StartStop Example letters = ["A","B","C","D","E","F","G"] for x in range(start, stop, step): print (letters[x]) 4. Print the letters C, D and E. 0 1 2 3 4 5 6 7 for x in range(2,5): A B C D E F G print (letters[x]) Start Stop 5. Print the letters B, D and F. 0 1 2 3 4 5 6 7 for x in A B C D E F G range(1,6,2): print (letters[x]) Start Step Stop 6. Print the letters A, D and G. for x in 0 1 2 3 4 5 6 7 range(0,7,3): A B C D E F G print (letters[x]) Step Start Stop Example letters = ["A","B","C","D","E","F","G"] for x in range(start, stop, step): print (letters[x]) 7. Print all of the letters in reverse order. -1 0 1 2 3 4 5 6 for x in range(6,- A B C D E F G 1,-1): print Stop Start (letters[x]) Reverse (-1) 8. Print the letters F, D and B in reverse order. -1 0 1 2 3 4 5 6 for x in range(5,0,- 2): A B C D E F G print (letters[x]) Stop Step Start Reverse (-2) Example Printing specific elements of an array - Program B #Program B letters = ["A","B","C","D","E"] for counter in range(2,4): print(letters[counter]) Example How program B works letters = ["A","B","C","D","E"] 0 1 2 3 4 for counter in range(2,4): letters A B C D E print(letters[counter]) counte r Output Example How program B works letters = ["A","B","C","D","E"] 0 1 2 3 4 for counter in range(2,4): letters A B C D E print(letters[counter]) counte 2 r Output Example How program B works letters = ["A","B","C","D","E"] 0 1 2 3 4 for counter in range(2,4): letters A B C D E print(letters[counter]) counte 2 r Output Example How program B works letters = ["A","B","C","D","E"] 0 1 2 3 4 for counter in range(2,4): letters A B C D E print(letters) counte 2 r Output Example How program B works letters = ["A","B","C","D","E"] 0 1 2 3 4 for counter in range(2,4): letters A B C D E print("C") counte 2 r Output C Example How program B works letters = ["A","B","C","D","E"] 0 1 2 3 4 for counter in range(2,4): letters A B C D E print(letters[counter]) counte 3 r Output C Example How program B works letters = ["A","B","C","D","E"] 0 1 2 3 4 for counter in range(2,4): letters A B C D E print(letters[counter]) counte 3 r Output C Example How program B works letters = ["A","B","C","D","E"] 0 1 2 3 4 for counter in range(2,4): letters A B C D E print(letters) counte 3 r Output C Example How program B works letters = ["A","B","C","D","E"] 0 1 2 3 4 for counter in range(2,4): letters A B C D E print("D") counte 3 r Output C D Adding elements of an array Example Adding elements of an array - Program C #Program C wages = [1,4,5] total = 0 for y in range(3): total = total + wages[y] print(total) Example How program C works wages = [1,4,5] total = 0 0 1 2 for y in range(3): wages 1 4 5 total = total + wages[y] y print(total) total Calculatio Output ns Example How program C works wages = [1,4,5] total = 0 0 1 2 for y in range(3): wages 1 4 5 total = total + wages[y] y print(total) total 0 Calculatio Output ns Example How program C works wages = [1,4,5] total = 0 0 1 2 for y in range(3): wages 1 4 5 total = total + wages[y] y 0 print(total) total 0 Calculatio Output ns Example How program C works wages = [1,4,5] total = 0 0 1 2 for y in range(3): wages 1 4 5 total = total + wages[y] y 0 print(total) total 1 total = total + wages[y] Calculatio = 0 + Output ns wages 1 = 0 + 1 Example How program C works wages = [1,4,5] total = 0 0 1 2 for y in range(3): wages 1 4 5 total = total + wages[y] y 1 print(total) total 1 Calculatio Output ns Example How program C works wages = [1,4,5] total = 0 0 1 2 for y in range(3): wages 1 4 5 total = total + wages[y] y 1 print(total) total 5 total = total + wages[y] Calculatio = 1 + Output ns wages 5 = 1 + 4 Example How program C works wages = [1,4,5] total = 0 0 1 2 for y in range(3): wages 1 4 5 total = total + wages[y] y 2 print(total) total 5 Calculatio Output ns Example How program C works wages = [1,4,5] total = 0 0 1 2 for y in range(3): wages 1 4 5 total = total + wages[y] y 2 print(total) total 10 total = total + wages[y] Calculatio = 5 + Output ns wages 10 = 5 + 5 Example How program C works wages = [1,4,5] total = 0 0 1 2 for y in range(3): wages 1 4 5 total = total + wages[y] y 2 print(total) total 10 Calculatio Output ns 10 Comparison Comparing two methods for adding elements Method 1 - without a loop Method 2 - with a loop wages = wages = [1,4,5,20,4,23,50,60,3,15,4,3] [1,4,5,20,4,23,50,60,3,15,4,3] total = total = 0 wages+wages+wages for y in range(12): + total = total + wages[y] wages+wages+wages + print(total) wages+wages+wages + wages+wages+wages[ 11] Adding all of the elements of an array is more efficient using a for loop. print(total) Running totals revisited Information Calculating a running totals in loops Running loops are commonly used in loops. They are used to: Add a sequence or list of numbers, or join characters (concatenation). Counting the number of occurrences of an event. It is a variable which is updated with a new value with each iteration of a loop. There are ausually 1 Create runningthree total steps to them: e.g. total = 0 Outside of the loop 2 Add a number to the e.g. total = Inside of the running total total + 1 loop 3 Print the running total e.g. print (total) Outside of the loop Information Examples of running totals total = total + 1 Adds 1 to the total variable and then stores this result in the same total variable. Updating a variable by adding one is called incrementing. total = total – 1 Subtracts 1 from the total variable and then stores this result in the same total variable. Updating a variable by subtracting one is called decrementing. total = total + new Adds the total and new variables and then stores this result in the total variable. total = total + wages [counter] Adds an element of the wages array to the variable total with each iteration of a loop. Important Adding the elements of a loop using a running total wages = [1,4,5,20,4,23,50,60,3,15,4,3] total = 0 1. A running total is created. for y in range(12): total = total + wages[y] 2. Add elements from the array to the print(total) running total. 3. Print the running total. Printing and adding elements of an array Example Program D The program: Creates a prices array. Prints each element in the prices array. Adds the elements in the prices array to a total variable. Prints out the total cost of the elements (once the loop stops). pries=[4,40,30,120] total = 0 for counter in range (4): print(prices[counter]) Prints each element (each total = total + prices[counter] price) print ("The total is", total) Adds the elements of the array (the prices) Prints the sum of all prices. Count-controlled loops vs. Condition- controlled loops Information Count-controlled loops What are they? The loop is executed a specific number of times. They use a loop variable to keep track of how many times the loop has repeated. When are they used? They are used when it is known in advance (i.e. before entering the loop) the exact number of times the block of code needs to be repeated. What type of loops do this? For loops (more efficient) While loops for counter in range(3): Examples: counter=0 print(counter)while counter