0% found this document useful (0 votes)
45 views2 pages

Outcomes Writing Loops Using For: Gibson Lam and David Rossiter

The document discusses using for loops in Python. It covers: 1. Using the range command to generate a sequence of numbers to loop through. Range includes the start but not end number by default. 2. Writing for loops that iterate over each item in the sequence, with the loop body executing once per item. 3. Optional arguments like step value that can be used to skip numbers in the range.

Uploaded by

Martin Yu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views2 pages

Outcomes Writing Loops Using For: Gibson Lam and David Rossiter

The document discusses using for loops in Python. It covers: 1. Using the range command to generate a sequence of numbers to loop through. Range includes the start but not end number by default. 2. Writing for loops that iterate over each item in the sequence, with the loop body executing once per item. 3. Optional arguments like step value that can be used to skip numbers in the range.

Uploaded by

Martin Yu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Outcomes Writing Loops Using For

COMP1021 • After completing this presentation, you are • Previously, we have discussed the use of while
Introduction to Computer Science expected to be able to: loops to do things repeatedly in Python
1. Use the range command to make a range of • In this presentation, we will look at another
numbers way of doing loops, using for loops
Using For Loops 2. Write loops using the for command • Using a for loop:
– you can perform some actions a particular
number of times, or:
Gibson Lam and David Rossiter – you can loop through a set of data, performing
the same actions on every item in the set

COMP1021 Using For Loops Page 2 COMP1021 Using For Loops Page 3

The Range Command Showing a Range The Ending Number


• If you print the result of a range, you will only see
• Before explaining how to use for loops let’s look this, which is not very helpful: • If you look carefully at the line of code shown
at a command called range in the previous slides, i.e.
• The range command creates a range of numbers range(1, 6)
• For example, you can make a range of numbers
you may think that it should return the number
between 1 and 5 using the following code:
• To see the numbers returned by the range command, 6 as well because the range is from 1 to 6
range(1, 6) you will need to use list(), like this:
• However, the range command does not give
• The above line of code returns a range of numbers you the ending number
which includes 1, 2, 3, 4 and 5 • In this example, the number 6 is not included

COMP1021 Using For Loops Page 4 COMP1021 Using For Loops Page 5 COMP1021 Using For Loops Page 6

The Step Value More Examples of the Range Command


The Default Starting Number is 0
• Here are more examples of using the step value:
• If you want, you can provide an optional third
• If you do not provide the starting number, the range(0, 10, 3) returns 0, 3, 6 and 9
input value in the range command
default value used by the range command will range(-1,-10,-2) returns -1, -3, -5, -7 and -9
be 0 (not 1) • This value is called the step value
• Here are some unusual examples of using range():
• For example, you can create a range of numbers • You can use it to skip (jump over) numbers
range(10, 1)
from 0 to 4 by using this line of code: • For example, a step value of 2 will skip every returns nothing because the default step value is 1
range(5) other number within the range, like this:
range(-10, 1,-1)
range(1, 10, 2) returns 1, 3, 5, 7 and 9 returns nothing because the step value is -1
• This code generates 0, 1, 2, 3 and 4 (again, the range(2, 10, 2) returns 2, 4, 6 and 8
ending number 5 is not included by range) range(0, 10, 0)
Note that 10, the ending number, is results in an error because the step value must not be zero
not included in the resulting range
COMP1021 Using For Loops Page 7 COMP1021 Using For Loops Page 9
For Loops The Flow of a For Loop Using Range in a For Loop
Start item = first item in the list • You need to give a list of items to a for loop
for item in a list of items : • This is where the range command is commonly used
. . .statement(s). . . • For example, the following code prints out a list of
no four numbers:
Does the item exist? The range command
for i in range(4) : returns four items:
• The statement(s) are executed for each item in a yes 0, 1, 2 and 3
given list of items print(i, end=" " )
Execute statement(s) The end value means a space is
• For example, if there are 10 items in the given put at the end of the number
list, the statement(s) will be executed 10 times when it is printed, instead of
item = next item in the list End
moving to the next line
COMP1021 Using For Loops Page 10

Controlling a For Loop Printing Things Using ‘end’ Using a ‘Fixed’ List in a For Loop
• As you can see from the previous slide the range • When you print things using the print command it • You can use a ‘fixed’ list instead of a list of
command can be used to control: moves to the next line after it finishes printing numbers given by the range command
– how many times the content of a for loop is • In the previous examples, we use the ‘end’ value to ask • For example, you can use any numbers you like:
repeatedly executed the print command to print a space at the end instead for i in [33, 19, 5, -7]:
– the number each time the loop content is executed • The end value is useful when you have multiple print print(i, end=" ")
commands that print things on the same line, e.g:
• Here are some more examples: You use a pair of brackets, i.e. [],
print("These are ", end="") In this example, to enclose the list of items
for i in range(0, 6): nothing, i.e. "",
print("shown on ", end="")
print(i, end=" ") print("the same line!") is printed at the • Or, you can choose not to use any numbers at all:
end of the text for word in ["How", "are", "you"]:
for i in range(1, 6, 2):
print(i, end=" ") print(word, end=" ")

Using a String in a For Loop


• A string will be changed into a list of letters
automatically when it is used in a for loop
• For example, the string "Wild hair" becomes
["W","i","l","d"," ","h","a","i","r"]
in the code below:
for letter in "Wild hair":
print(letter, end="-")
There is a hyphen
after each letter

COMP1021 Using For Loops Page 16

You might also like