1 of 43

Lists:

index, slice, in, iterate

Jake Shoudy

Sept 19, 2022

CSCI 110 - Lecture 14

2 of 43

Announcements

3 of 43

HW5

Posted last Thursday

For loops!

Due 9/22 (Thursday) because it is posted a day late

4 of 43

Project 1: Part 4

Due Sunday 9/25 11:59PM

No new functionality!

Writing unit tests and integration tests!

5 of 43

STEP Application Party!

  • STEP application opens today (9/19)
  • Darian will be on campus!
  • 9/20
  • Jubilee Hall: Appleton Room
  • 5-7pm
  • RSVP
  • Book OH with Darian
    • Already full :(
    • PLEASE go if you signed up

6 of 43

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%

7 of 43

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!

8 of 43

Recap

9 of 43

for loops

for loop: control structure that runs block of code repeatedly by iterating over elements in sequence

for letter in 'cat':

print(letter)

10 of 43

index = 0

while index < len(word):

print(word[index])

index = index + 1

print('All done!')

for letter in word:

print(letter)

print('All done!')

11 of 43

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

12 of 43

index = 0

while index < 10:

print(index)

index = index + 1

print('All done!')

for i in range(0, 10):

print(i)

print('All done!')

13 of 43

Review: range()

result = ''

for i in range(4):

result = result + str(i) + 'a'

print(result)

0a1a2a3a

14 of 43

Lists

15 of 43

Data Structures

16 of 43

Data Structures

What?

Different ways to organize and store data

Why?

Data can be accessed in the fastest way possible, depending on the data

17 of 43

Why does it matter? A real world example:

Put books in a pile as people return them to the library…

18 of 43

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!

19 of 43

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

20 of 43

Student names (before lists)

21 of 43

This quickly becomes too much work

plus, what happens if a new student “Bob” joins the class?

Rename all our variables?!?

22 of 43

Lists make all this much easier

23 of 43

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

24 of 43

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:

25 of 43

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]

26 of 43

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]

27 of 43

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

28 of 43

Tracing for loops

for num in [1, 20, 3]:

print(num)

print('no more numbers')

num

29 of 43

Tracing for loops

for num in [1, 20, 3]:

print(num)

print('no more numbers')

num

1

30 of 43

Tracing for loops

for num in [1, 20, 3]:

print(num)

print('no more numbers')

num

1

1

31 of 43

Tracing for loops

for num in [1, 20, 3]:

print(num)

print('no more numbers')

num

1

20

32 of 43

Tracing for loops

for num in [1, 20, 3]:

print(num)

print('no more numbers')

num

1

20

20

33 of 43

Tracing for loops

for num in [1, 20, 3]:

print(num)

print('no more numbers')

num

1

20

3

34 of 43

Tracing for loops

for num in [1, 20, 3]:

print(num)

print('no more numbers')

num

1

20

3

3

35 of 43

Tracing for loops

for num in [1, 20, 3]:

print(num)

print('no more numbers')

num

1

20

3

3

No elements left!

36 of 43

Tracing for loops

for num in [1, 20, 3]:

print(num)

print('no more numbers')

num

1

20

3

no more numbers

3

37 of 43

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

38 of 43

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

39 of 43

Lists vs Strings: Operators

  • operator concatenates lists together

* 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]

40 of 43

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

41 of 43

Let’s Code!

42 of 43

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

43 of 43

Questions?