Lists:
index, slice, in, iterate
Jake Shoudy
Sept 19, 2022
CSCI 110 - Lecture 14
Announcements
HW5
Posted last Thursday
For loops!
Due 9/22 (Thursday) because it is posted a day late
Project 1: Part 4
Due Sunday 9/25 11:59PM
No new functionality!
Writing unit tests and integration tests!
STEP Application Party!
Exam 1 Scores
| Before Partial | Final Grade |
Mean | 70.1% | 76.1% |
Min | 10% | 10% |
Max | 100% | 100% |
Median | 74% | 84% |
Percent of class above 70% | 55% | 68% |
Percent of class above 90% | 30% | 31% |
Google 20% TAs
Didn’t like your exam 1 score? Just plain lost?
Some folks from Google are volunteering their time to help you with your computer science scoolwork!
If you feel that you are falling behind and want more 1:1 attention let me know and I will try to pair you with someone who can help!
Recap
for loops
for loop: control structure that runs block of code repeatedly by iterating over elements in sequence
for letter in 'cat':
print(letter)
index = 0
while index < len(word):
print(word[index])
index = index + 1
print('All done!')
for letter in word:
print(letter)
print('All done!')
range()
range(): returns sequence from start to end, incrementing by step (3rd parameter)
Use range() with for loops to repeat a loop when need numbers
range(5, 8)
range(0, 10, 2)
range(5, 0, -1)
range(5)
5 6 7
0 2 4 6 8
5 4 3 2 1
0 1 2 3 4
index = 0
while index < 10:
print(index)
index = index + 1
print('All done!')
for i in range(0, 10):
print(i)
print('All done!')
Review: range()
result = ''
for i in range(4):
result = result + str(i) + 'a'
print(result)
0a1a2a3a
Lists
Data Structures
Data Structures
What?
Different ways to organize and store data
Why?
Data can be accessed in the fastest way possible, depending on the data
Why does it matter? A real world example:
Put books in a pile as people return them to the library…
Eventually, finding a book in a huge pile becomes too much work
The book you want is somewhere in that pile… good luck!
The book you want is somewhere in that pile… good luck!
So real libraries use numbered shelves to organize stored books
Good strategy: storing books in shelves
The book you want is on that shelf
at position [6]
0 1 2 3 4 5 6 7 8
Student names (before lists)
This quickly becomes too much work
plus, what happens if a new student “Bob” joins the class?
Rename all our variables?!?
Lists make all this much easier
Lists
What?
Data structure containing ordered list of elements, which can be strings, numbers, other data types, and other data structures including lists
Is a data type, just like str, int, bool, float
Can index, slice, in, iterate
Why?
Hold data that needs to stay in a certain order
Syntax
ages = [18, 26, 19, 21, 23, 34]
states = ["AL", "MS", "SC", "CA"]
answers = [True, True, False, True]
weird = [True, 1, 1.0, '1']
Create list: putting comma-separated values inside of square brackets []
Can contain zero or more values of any type, usually same type in one list
Can contain multiple types, but rarely useful
Examples:
Practice: Indexing
my_list[0]
len(my_list)
my_list[4]
my_list[5]
my_list[len(my_list)]
18
6
23
34
IndexError
my_list = [18, 26, 19, 21, 23, 34]
Practice: Slicing and in
[26, 19, 21]
True
[18, 26, 19]
False
[23, 34]
[21, 23]
my_list[1:4]
21 in my_list
my_list[:3]
100 in my_list
my_list[4:]
my_list[3:len(my_list)-1]
my_list = [18, 26, 19, 21, 23, 34]
Iteration
for item in my_list:
print(item)
my_list = ['abc', 'xyz', 'hello', 'goodbye']
i = 0
while i < len(my_list):
print(my_list[i])
i = i + 1
for i in range(len(my_list)):
print(my_list[i])
same
Tracing for loops
for num in [1, 20, 3]:
print(num)
print('no more numbers')
num
Tracing for loops
for num in [1, 20, 3]:
print(num)
print('no more numbers')
num
1
Tracing for loops
for num in [1, 20, 3]:
print(num)
print('no more numbers')
num
1
1
Tracing for loops
for num in [1, 20, 3]:
print(num)
print('no more numbers')
num
1
20
Tracing for loops
for num in [1, 20, 3]:
print(num)
print('no more numbers')
num
1
20
20
Tracing for loops
for num in [1, 20, 3]:
print(num)
print('no more numbers')
num
1
20
3
Tracing for loops
for num in [1, 20, 3]:
print(num)
print('no more numbers')
num
1
20
3
3
Tracing for loops
for num in [1, 20, 3]:
print(num)
print('no more numbers')
num
1
20
3
3
No elements left!
Tracing for loops
for num in [1, 20, 3]:
print(num)
print('no more numbers')
num
1
20
3
no more numbers
3
Lists vs Strings
Similarities:
Both are sequences
Can index into, slice, and iterate over them
in operator
Differences:
Strings can only hold characters
Lists can hold any data type, including characters, strings, and other lists
Lists vs Strings: Iteration
x = 'qwerty'
for char in x:
print(char)
x = ['qwerty', 'asdf', 'zxcv']
for word in x:
print(word)
q
w
e
r
t
y
qwerty
asdf
zxcv
Lists vs Strings: Operators
* operator repeats a list
a = [1, 2, 3]
b = [4, 5, 6]
print(a + b)
print(a)
[1, 2, 3, 4, 5, 6]�[1, 2, 3]
a = [1, 2, 3]
print(a * 2)
[1, 2, 3, 1, 2, 3]
Lists vs Strings: Truthiness
Both can be empty
empty string: ''
empty list: []
empty string and list is False-y
bool([]) == bool('') == False
all non-empty strings and lists are Truthy
bool(['hello']) == bool('hello') == True
Let’s Code!
https://replit.com/team/csci110-01
Lists: Intro
Review: Lists
What?
Data structure containing ordered list of elements, which can be strings, numbers, other data types, and other data structures including lists
Is a data type, just like str, int, bool, float
Can index, slice, in, iterate
Why?
Hold data that needs to stay in a certain order
Questions?