cs 105 summer wednesday 6
play

CS 105 SUMMER WEDNESDAY 6 What to talk about today? The with - PowerPoint PPT Presentation

CS 105 SUMMER WEDNESDAY 6 What to talk about today? The with statement in more detail Splitting and joining strings csv files and the csv module More slicing Nested Loop Example Questions youd want me to touch on


  1. CS 105 SUMMER – WEDNESDAY 6

  2. What to talk about today?  The with statement in more detail  Splitting and joining strings  csv files and the csv module  More slicing  Nested Loop Example  Questions you’d want me to touch on

  3. Quiz 5 comments  High level stats: Mean 79%, Median 83%  Not as lovely  Single biggest issue? Not content – Parson’s  Thus, the Parsons grade adjustment  +6 – the worth of the Parsons problem on the quiz  +4 – Boosts to 2/3 rd a letter grade, allowance for “Parson’s tilt”  Also, going to go without Parson’s on future quizzes (may still appear on homework)

  4. Practice Quiz 6  Has been up since Monday  Another good reflection of the Quiz  More reiteration of loops, conditionals, a bit more file stuff, pattern stuff  Trying to downplay…  Excel  No Parsons!  Now includes  Code-Reading/Explain in Plain English Question (as will Q7, but not Final)

  5. Speaking of the final  The final’s regular time is set! August 7 1:00 – 3:00 PM  You can reserve a “seat” (or conflict times) as early as July 30 th  Not twice the size of a quiz – approx. 1.5 times

  6. One thing that may help – help function  Ever not sure what to do with a python object? Not sure where to look in the docs?  Use Python’s help function! my_list help(my_list)

  7. Quick MPs  A fair amount on more advanced string formatting…  “ I would like to learn more about advanced string formatting and how it is used in real-life situations. Although I assume that it is used to create websites, I would like to learn about other potential uses.”  “ I am still confused on text allignment , as the inputs seem pretty similar but the outputs don’t. ”  I’m slightly disinclined – won’t ask for more advanced formatting on quizzes/homeworks  https://pyformat.info/ is the best formatting resource I know of  Why format? Mostly for nice output in command line interfaces or files

  8. Quick MPs - Sys.argv  I very rarely use sys.argv  Stands for “system argument vector”  Used when running a Python script from the command line and passing it some argument – I can show you quickly

  9. Quick MPs  “Why do people say that Al Gore invented the internet?”  Out of context and inflated quote from an interview with Wolf Blitzer in 1999 –  ““During my service in the United States Congress, I took the initiative in creating the Internet. I took the initiative in moving forward a whole range of initiatives that have proven to be important…”  According to Robert Kahn and Vinton Cerf (TCP/IP creators), Gore was first politician to really support the net in the 80s

  10. Quick MPs  Not the only weird net comment  Ted Stevens on the internet  In a way, trying to describe bandwidth issues…  More likely routing issues (“lost packets”)

  11. Quick MPs – on HTTP/HTML  “I guess my question would be what exactly do we need to be able to recall from memory from this chapter? Should we study specific HTTP response status codes and details like that or do we just need to have a general idea of how web programming in general works?”  “What is the requirement for this week's reading in terms of Python coding? Do we need to remember all specific request methods or HTML tags for coding?”  “I think Max mentioned that he will not put too much emphasis on topic 9 in future assessments;”  “ The readings did not include any code formatting, and the homework isn't showing me how to apply the textbook's lesson. Can we talk more about what the reading discussed? “

  12. Quick MPs - Gist of HTTP/HTML  Given summer course, struggle to ask a lot  Traditionally only ask true/false, multiple choice, some parsons, some programming with Python  This summer – planning only to do Python programming  “Given a list, turn it into an HTML unordered list string”

  13. Quick MP – Write Buffer  “Why doesn't the write() function buffer output to the disk immediately?”  Answer: ‘stream’ setup and physical distance

  14. Python’s with statement

  15. The with statement  Designed to fix a “problem”. Consider this code from effbot.org set things up try: #Kind of like “if something is true” do something finally: #Kind of like “after a loop/after the if” tear things down

  16. A design flaw  Imagine working with a file. It’s annoying to…  (1) Open it  (2) Make sure it is open  (3) Do stuff with it  (4) Make sure you close it so data saves!

  17. with statement simplifies  (1) Open it  (2) Make sure it is open  (3) Do stuff with it  (4) Make sure you close it so data saves!  The with statement handles all this  Leaving you free to write this

  18. Let’s practice with with!  Let’s make a with to…  write to a file, with w  read from that file, with r  append to that file, with a  “What’s the + for?” –  w+ read and write at same time (and create file if it doesn’t exist)  r+ read and write at same time (do NOT create file)  a+ read and write at same time, but start at end of file

  19. String split and join

  20. split and join  split() and join() are different sides of the same coin split join string to list to list string

  21. How do we split?  By default, split splits on all whitespace  “Get all the words from a sentence…”  words = sentence.split()  Can also split on anything we want  split a csv  data = “this,is,a,csv”.split(“,”)

  22. Split works well on strings with the same delimiter  Get the numbers out of a date in the form MM/DD/YYYY  date_string.split (“/”)

  23. How does join work then?  join() is a method of strings  Takes a list as an argument and joins each element with that string as a delimiter!

  24. Most common pattern – building csvs  “Given a list of numbers, return them as a csv string”  Long way… s = “” for num in num_list: s+= str(num) + “,” #Leaves a comma on the end.  Short way – “,”.join( num_list)

  25. Doesn’t have to be a comma s_list = [“one”, “two,” “red”, “blue”] seuss = “ fish “.join( a_list) print(seuss)

  26. Csv files

  27. CSV files  csv files are basically spreadsheets, but as rows of CSVs “column1”, “column2” “puffin”, “5” “dog”, “2”

  28. By example – let’s combine everything  In repl.it, I’m going to write a Python script which  Opens a csv file of numbers, given the file’s name  Updates each number in each row by 2  Saves the file with the new content

  29. But what about the csv module?  csv module isn’t necessary, but it provides a different interface for working with csvs  Compare regular file approach vs csv

  30. Regular vs csv module with open(“f.csv”,”r”) as csvfile: import csv all_lines = csvfile.readlines() with open(“f.csv”,”r”) as csvfile: for row in all_lines[1:] reader = csv.reader(csvfile) #do stuff with row reader.next() for row in reader: #do stuff with row

  31. csv module is nicer when writing a csv  It gives us a writerow function, so we don’t have to worry about correct comma placement and newlines: import csv with open('eggs.csv', 'w') as csvfile: writer = csv.writer(csvfile) writer.writerow ([“Pikachu”, “Shocked”]) writer.writerow ([“ Snivy ”, “Smug”])

  32. List slicing example

  33. Slicing in general  A slice makes a copy between two indices – inclusive on the left side, exclusive on the right  my_list = [“puffin”, “dog”, “red panda”, “manatee”]  What slice is…  new_list = my_list[1:2]

  34. slice is useful with index  “Return a slice of the list between, and including, the first two numbers larger than 10 in a list…”  How can we do this? How can index help?  index can just be…  a_list.index(value_to_find)  a_list.index(value_to_find, starting_location)

  35. Nested Loop Example

  36. Nested for loops  “My muddiest point this week is combining for loop with nesting.”  Assuming multiple nested loops – so for example, a list of list or multiple ranges?  Example – Write a function which…  For every number from 1 to n, print the factorial of the number, one on each line  So for input 3, print:  1!  2!  3!

  37. Any questions you want to see?

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend