Transcript Week 5 PPT

Last Week
Looping through strings
Lists
List methods
Nested Lists
This Week
• For loops and range for lists
• Strings versus Lists
• Aliasing
• While loops
Visiting the Items in a List L
Visiting the items in a the list
English:
for each item in L
statements using item
Python:
sum = 0
for item in L:
sum = sum + item
For Loops -- Revisited
L1 = [1, 2, 3, 4]
L2 = [‘A’, ‘B’, ‘C’, ‘D’]
Want to print each element from L1 followed by the
corresponding element from L2.
for num in L1:
print(num, ??)
# How do we print the item from
L2?
Loop over indices:
for index in [0, 1, 2, 3]:
print(L1[index], L2[index])
For Loops -- Revisited
L1 = [1, 2, 3, 4, …, 100]
L2 = [‘A’, ‘B’, ‘C’, ‘D’, …, ‘Z’, ‘a’, …, ‘z’, …]
Want to print each element from L1 followed by the
corresponding element from L2.
for num in L1:
print(num, ??)
# How do we print the item from
L2?
Loop over indices:
Don’t want to write all the
numbers from 0 – 100!!
for index in ??:
print(L1[index], L2[index])
For Loops -- Revisited
Want to create a list of numbers from 0-100.
>>> L = [0, 1, 2, 3, 4, …, 99, 100]
Is there an easier way to do this?
>>>
>>>
>>>
>>>
>>>
L = range(0, 101)
L
range(0, 101)
list(L)
[0, 1, 2, 3, 4, …, 99, 100]
Range
>>>
>>>
>>>
>>>
>>>
L = range(0, 101)
L
range(0, 101)
list(range(0, 1010))
[0, 1, 2, 3, 4, …, 99, 100]
range(start, stop[,step])
•
•
•
•
returns a list of integers
beginning with start to the last integer before stop.
start can be omitted and defaults to 0
step can be omitted and defaults to 1
For Loops -- Revisited
L1 = [1, 2, 3, 4, …, 100]
L2 = [‘A’, ‘B’, ‘C’, ‘D’, …, ‘Z’, ‘a’, …, ‘z’, …]
Want to print each element from L1 followed by the
corresponding element from L2.
for index in range(0, len(L1)):
print(L1[index], L2[index])
Lists vs. Strings
• Lists and strings both have functions and methods.
• Lists can be changed, strings cannot be changed.
• new_string = old_string
– creates a new copy of old_string
– we now have two different strings with the same content.
– we cannot change the strings (we can change the content of
the variables)
• new_list = old_list
– does not create a new copy
– new_list and old_list store the same list
– changing one changes the other
Aliasing
Alias: an assumed or additional name.
squares = [0, 1, 4, 9, 16, 25, 36]
squares_copy = squares
squares.append(49)
Q. What is squares now? squares_copy?
squares == [0, 1, 4, 9, 16, 25, 36, 49]
squares_copy == [0, 1, 4, 9, 16, 25, 36, 49]
Aliasing Pictorally
square
square_copy
[0, 1, 4, 9, 16, 25, 36]
square_slice = square[4:7]
[16, 25, 36]
Slicing creates a new object.
While Loops
Sometimes we need to loop until a condition is
met.
For example:
– Ask user for a password twice
– If the passwords don’t match, ask again until they
match or the user quits
While Loop Example
English example:
Ask for password.
Ask for password again.
While the passwords don’t match:
Ask again.
While Loop Example
Python example:
password1 = input(‘Enter password: ’)
Ask for password again.
While the passwords don’t match:
Ask again.
While Loop Example
Python example:
password1 = input(‘Enter password: ’)
password2 = input(‘Re-enter password: ’)
While the passwords don’t match:
Ask again.
While Loop Example
Python example:
password1 = input(‘Enter password: ’)
password2 = input(‘Re-enter password: ’)
while password1 != password2:
Ask again.
While Loop Example
Python example:
password1 = input(‘Enter password: ’)
password2 = input(‘Re-enter password: ’)
while password1 != password2:
password2 = input(‘Re-enter
password:’)