CS 105 SUMMER WEDNESDAY 5 Hey Max, what about that bee movie - - PowerPoint PPT Presentation

cs 105 summer wednesday 5
SMART_READER_LITE
LIVE PREVIEW

CS 105 SUMMER WEDNESDAY 5 Hey Max, what about that bee movie - - PowerPoint PPT Presentation

CS 105 SUMMER WEDNESDAY 5 Hey Max, what about that bee movie thing? patient! Jokes aside still intend to Probably Week 6, 7, or 8, as we'll have time May have to be email - SMS apis are pricier than they used to be What to


slide-1
SLIDE 1

CS 105 SUMMER – WEDNESDAY 5

slide-2
SLIDE 2

Hey Max, what about that bee movie thing?

patient!

Jokes aside – still intend to Probably Week 6, 7, or 8, as we'll have time May have to be email - SMS apis are pricier

than they used to be

slide-3
SLIDE 3

What to talk about today?

From the muddiest points

While loop vs For loop

Loop nesting – when, why, etc (ZyBooks 7.8.1, 7.8.2,

7.10.2)

Why care about dynamic typing? What's with global vs local? When does it matter?

Your questions – please feel free to post

questions you'd like to see in chat!

slide-4
SLIDE 4

Quick MPs

 In the context of for loops - "Do you have to define i before using it? Also,

does the loop variable have to be i or it could be any variable? " for i in ["Alice", "Bob", "Charlie"]: print(i) for name in ["Alice", "Bob", "Charlie"]: print(name)

  • Name is arbitrary –

call it whatever you want

  • i is convention for

index

slide-5
SLIDE 5

Quick MPs

Some MPs:

"I also struggle on some parts of the homework."

"Can we have quiz review sessions?"

Please post your questions to Piazza. Also use office

hours/tutoring!! (should have better hours…)

Piazza exists for a dialog – it's okay if what you

have is a broken code and a "yo wtf?!"

slide-6
SLIDE 6

Quick MPs

 “confused about why range(0,5) doesn't go until 5 is that just something

that i should know and accept or is there like an actual reason”

 Largely something you should just accept, but there is a good reason  Think looping through list indices (without enumerate)

 range(len(my_list)) –> 0 up to the last index of the list  if my_list is [1,2,3,4], then range(4) gives me (0,1,2,3)

 In brief – exclude the end so you can use the length as the stopping point  Same idea with slicing to the end

slide-7
SLIDE 7

Quick MPs

 "Could we go deeper into polymorphism?"

We could…largely out of class scope for the summer Polymorphism is a key part of object-oriented

languages (like Python, Java)

Plan to talk about in optional video content/maybe

  • ptional parts of class meeting in Week 8
slide-8
SLIDE 8

Quick MPs

 "Why do docstrings matter?"  From Python.org:  A docstring is a string literal that occurs as the first statement in a module,

function, class, or method definition. Such a docstring becomes the __doc__ special attribute of that object.

 Basically, a fancy comment telling you WHAT a function does  Good habit for larger programming projects, but not really a part of this

course

slide-9
SLIDE 9

Quick MPs

 "When I'm taking a quiz or the final and there's a question

that I am unsure of, what are the first things I should think or look for that would help me find the right solution to the problem?"

  • 1. Question prompt
  • 2. Python docs
  • 3. Trying anything in Repl.it
slide-10
SLIDE 10

For example – area code from a phone number

 Identifies a technique

– slicing

 Also gives a worse

cast test case – could try return '555' if all else fails

slide-11
SLIDE 11

So…slicing…docs are weird

slide-12
SLIDE 12

So…slicing…docs are weird

slide-13
SLIDE 13

So…slicing…docs are weird

slide-14
SLIDE 14

Quiz 4 comments

High level stats: Mean

84%, Median 88%

 Good job! Trending up!!  …Is stonks still funny?  Was it ever funny?

slide-15
SLIDE 15

Practice Quiz 5

Slightly different question setup

Still 30, but 5 programming Qs

Excel as much as HW7 – lab solutions

posted to help with this!

Looking at doing a code reading

question on Quiz 6, Quiz 7

slide-16
SLIDE 16

While loop vs For loop

 High level –

while loop is "indefinite" -> runs until a condition ends

for loop can also be considered a "for each loop" ->

"for each item in this thing"

The thing can be: a list (or other collection) a range statement etc.

slide-17
SLIDE 17

Can for loops and while loops do the exact same things?

In a manner of speaking… For example, we could make an infinite for loop Note this loop isn't really "running until X condition",

though for element in my_list:

my_list.append(new_element) #This won't actually #extend the loop!

slide-18
SLIDE 18

Better to think about what each loop is BEST for

 for loops – looping over collections, processing each

element of SOME THING, "x" number of steps

 while loops – Running until a sentinel value, running until

something is true or something else is no longer true

slide-19
SLIDE 19

break and continue – best with while

Let's code this: "Write a program which gets a list of numbers from the

  • user. If the user enters a non-numeric string, skip the
  • string. If the user enters a negative number, end and print

the list"

We will need str.isnumeric() for this

slide-20
SLIDE 20

Did we need break?

No – we used break to break the while True, but could

have also just used a sentinel value

continue here was also not strictly needed, but simplified

it down from an else

You almost never STRICTLY NEED continue or break

UNLESS you have an otherwise infinite loop (while True: )

slide-21
SLIDE 21

Using break with for loops – decent example

Let's write this function… "Write a function that takes a list of numbers as an

  • argument. Sum up all the numbers up to the first

negative number and return that sum"

slide-22
SLIDE 22

ZyBooks 7.8.1, 7.8.2, 7.10.2

ZyBooks questions

slide-23
SLIDE 23

Dynamic typing, global vs local

Largely, these are concerns you'd only deal with in

larger pieces of code, outside of the course scope

Dynamic is still a little bit neat We can use type-checking to take advantage of

dynamic typing…

slide-24
SLIDE 24

An example of dynamic typing being useful

 Let's write a function which can take either a string or a

list.

If it's a list, return the sum of all the numbers in that list If it's a string, split it on commas (,) and return the sum of all

the numbers in that string

Assume we'll either get a list of numbers or a csv string

slide-25
SLIDE 25

Global vs local

Largely, everything you've been writing is local Why is global bad? Changing a variable by accident

(dynamic typing negative!!)

Avoiding globals – principle for larger programs Still sometimes useful, like importing the math module

globally

slide-26
SLIDE 26

Any homework questions you'd like looked at, for example?

Any other questions you may have?