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 - - 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
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 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!
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
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?!"
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
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
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
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
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
So…slicing…docs are weird
So…slicing…docs are weird
So…slicing…docs are weird
Quiz 4 comments
High level stats: Mean
84%, Median 88%
Good job! Trending up!! …Is stonks still funny? Was it ever funny?
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
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.
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!
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
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
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: )
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"
ZyBooks 7.8.1, 7.8.2, 7.10.2
ZyBooks questions
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…
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
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