For Loops - Printing a loop variable - 6a
Document Details
Uploaded by MiraculousCyan
Tags
Summary
This document provides an introduction to the concept of for loops in Python, demonstrating the printing of loop variables through programming examples. It covers the basic syntax and concepts of programming constructs including sequence, selection, and iteration in programming.
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.