CS 61A Discussion 5 Iterators/Generators and Midterm Review Albert - - PowerPoint PPT Presentation

cs 61a discussion 5
SMART_READER_LITE
LIVE PREVIEW

CS 61A Discussion 5 Iterators/Generators and Midterm Review Albert - - PowerPoint PPT Presentation

CS 61A Discussion 5 Iterators/Generators and Midterm Review Albert Xu Kaavya Shah Slides: albertxu.xyz/teaching/cs61a/ Announcements Iterators, Iterables oh my iterable iterator Iterators, Iterables oh my iterable iterator Iterators,


slide-1
SLIDE 1

CS 61A Discussion 5

Iterators/Generators and Midterm Review

Slides: albertxu.xyz/teaching/cs61a/

Albert Xu Kaavya Shah

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Iterators, Iterables

iterable iterator

  • h my
slide-4
SLIDE 4

Iterators, Iterables

  • h my

iterator iterable

slide-5
SLIDE 5

Iterators, Iterables

  • h my

iterator iterable

iter as in iteration!

slide-6
SLIDE 6

Iterators, Iterables

iterator

  • h my

both iterators and iterables implement the __iter__ method, meaning that given an instance x of an iterator or iterable, calling iter(x) will give you a new iterator over x!

iterable

iter as in iteration!

slide-7
SLIDE 7

Iterators, Iterables

iterator

  • h my

iterable

implying that it is ABLE to be iterated over!

slide-8
SLIDE 8

Iterators, Iterables

iterator

  • h my

an iterable is any object, sequence or not, that can be iterated over! Examples include lists, tuples, sets, strings, and dictionaries!

iterable

implying that it is ABLE to be iterated over!

slide-9
SLIDE 9

Iterators, Iterables

iterable iterator

  • h my

implying that it is ABLE to be iterated over!

an iterable is any object, sequence or not, that can be iterated over! Examples include lists, tuples, sets, strings, and dictionaries!

this is the thing DOING the iteration!

slide-10
SLIDE 10

Iterators, Iterables

iterable iterator

  • h my

implying that it is ABLE to be iterated over!

an iterable is any object, sequence or not, that can be iterated over! Examples include lists, tuples, sets, strings, and dictionaries!

this is the thing DOING the iteration!

an iterator is an object that lets you get the next element of a sequence repeatedly until no more elements exist.

slide-11
SLIDE 11

A Good Comparison

*borrowed from Kevin T. Li

slide-12
SLIDE 12

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a)

slide-13
SLIDE 13

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter)

slide-14
SLIDE 14

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1

slide-15
SLIDE 15

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter)

slide-16
SLIDE 16

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2

slide-17
SLIDE 17

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter)

slide-18
SLIDE 18

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a)

slide-19
SLIDE 19

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter)

slide-20
SLIDE 20

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3

slide-21
SLIDE 21

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter)

slide-22
SLIDE 22

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration

slide-23
SLIDE 23

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration >>> next(unfun_iter)

slide-24
SLIDE 24

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration >>> next(unfun_iter) StopIteration

slide-25
SLIDE 25

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration >>> next(unfun_iter) StopIteration >>> next(funner_iter)

slide-26
SLIDE 26

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration >>> next(unfun_iter) StopIteration >>> next(funner_iter) 1

slide-27
SLIDE 27

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration >>> next(unfun_iter) StopIteration >>> next(funner_iter) 1 >>> a

slide-28
SLIDE 28

WWPD, Iter*

>>> a = [1, 2, 3] >>> fun_iter = iter(a) >>> next(fun_iter) 1 >>> next(fun_iter) 2 >>> unfun_iter = iter(fun_iter) >>> funner_iter = iter(a) >>> next(unfun_iter) 3 >>> next(fun_iter) StopIteration >>> next(unfun_iter) StopIteration >>> next(funner_iter) 1 >>> a [1, 2, 3]

slide-29
SLIDE 29

Generators

how do you write your own iterators?

slide-30
SLIDE 30

Generators

how do you write your own iterators?

Using a list

slide-31
SLIDE 31

Generators

how do you write your own iterators?

Using a list

>>> fibs = [1, 1, 2, 3, 5, 8] >>> fib_iter = iter(fibs)

slide-32
SLIDE 32

Generators

how do you write your own iterators?

Using a list

>>> fibs = [1, 1, 2, 3, 5, 8] >>> fib_iter = iter(fibs)

Two issues: 1) I have to calculate out values of fib myself 2) I can only make a finite iterator

slide-33
SLIDE 33

Generators

how do you write your own iterators?

Using a list

>>> fibs = [1, 1, 2, 3, 5, 8] >>> fib_iter = iter(fibs)

Two issues: 1) I have to calculate out values of fib myself 2) I can only make a finite iterator

Using a generator

slide-34
SLIDE 34

Generators

how do you write your own iterators?

Using a list

>>> fibs = [1, 1, 2, 3, 5, 8] >>> fib_iter = iter(fibs)

Two issues: 1) I have to calculate out values of fib myself 2) I can only make a finite iterator

Using a generator

>>> def fibs(): prev = 0 current = 1 while True: yield current current = current + prev prev = current

slide-35
SLIDE 35

Generators

how do you write your own iterators?

Using a list

>>> fibs = [1, 1, 2, 3, 5, 8] >>> fib_iter = iter(fibs)

Two issues: 1) I have to calculate out values of fib myself 2) I can only make a finite iterator

Using a generator

>>> def fibs(): prev = 0 current = 1 while True: yield current current = current + prev prev = current >>> fib_iter = fibs()

slide-36
SLIDE 36

Generators

how do you write your own iterators?

Using a list

>>> fibs = [1, 1, 2, 3, 5, 8] >>> fib_iter = iter(fibs)

Two issues: 1) I have to calculate out values of fib myself 2) I can only make a finite iterator

Using a generator

>>> def fibs(): prev = 0 current = 1 while True: yield current current = current + prev prev = current >>> fib_iter = fibs()

generator function

slide-37
SLIDE 37

Generators

how do you write your own iterators?

Using a list

>>> fibs = [1, 1, 2, 3, 5, 8] >>> fib_iter = iter(fibs)

Two issues: 1) I have to calculate out values of fib myself 2) I can only make a finite iterator

Using a generator

>>> def fibs(): prev = 0 current = 1 while True: yield current current = current + prev prev = current >>> fib_iter = fibs()

generator function generator object (generator)

slide-38
SLIDE 38

a final note on equality

if the second part doesn’t make sense, it’s okay.

  • a is b
  • Object equality
  • This tells you whether two names are pointing to the

same thing (consult environment diagram)

slide-39
SLIDE 39

a final note on equality

  • a is b
  • Object equality
  • This tells you whether two names are pointing to the

same thing (consult environment diagram)

  • a == b
  • Value equality
  • This tells you whether two numbers or two lists contain

identical values. if the second part doesn’t make sense, it’s okay.

slide-40
SLIDE 40

a final note on equality

  • a is b
  • Object equality
  • This tells you whether two names are pointing to the

same thing (consult environment diagram)

  • a == b
  • Value equality
  • This tells you whether two numbers or two lists contain

identical values. But what happens if you use is for checking number equality? Weird things happen. if the second part doesn’t make sense, it’s okay.

slide-41
SLIDE 41

a final note on equality

  • a is b
  • Object equality
  • This tells you whether two names are pointing to the

same thing (consult environment diagram)

  • a == b
  • Value equality
  • This tells you whether two numbers or two lists contain

identical values. But what happens if you use is for checking number equality? Weird things happen. >>> a = 256 >>> b = 256 >>> a is b True if the second part doesn’t make sense, it’s okay.

slide-42
SLIDE 42

a final note on equality

  • a is b
  • Object equality
  • This tells you whether two names are pointing to the

same thing (consult environment diagram)

  • a == b
  • Value equality
  • This tells you whether two numbers or two lists contain

identical values. But what happens if you use is for checking number equality? Weird things happen. >>> a = 256 >>> b = 256 >>> a is b True >>> a = 257 >>> b = 257 >>> a is b False if the second part doesn’t make sense, it’s okay.

slide-43
SLIDE 43

Thanks for coming.

Have a great rest of your week! :)

Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/