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

cs 105 summer wednesday 6
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CS 105 SUMMER – WEDNESDAY 6

slide-2
SLIDE 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

slide-3
SLIDE 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/3rd a letter grade, allowance for

“Parson’s tilt”

 Also, going to go without Parson’s on future quizzes (may

still appear on homework)

slide-4
SLIDE 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)

slide-5
SLIDE 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 30th

Not twice the size of a quiz –approx. 1.5

times

slide-6
SLIDE 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)

slide-7
SLIDE 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

  • n quizzes/homeworks

 https://pyformat.info/ is the best formatting resource I know of  Why format? Mostly for nice output in command line interfaces

  • r files
slide-8
SLIDE 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

slide-9
SLIDE 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

slide-10
SLIDE 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”)

slide-11
SLIDE 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? “

slide-12
SLIDE 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”

slide-13
SLIDE 13

Quick MP – Write Buffer

 “Why doesn't the write() function buffer output to the disk immediately?”  Answer: ‘stream’ setup and physical distance

slide-14
SLIDE 14

Python’s with statement

slide-15
SLIDE 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

slide-16
SLIDE 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!

slide-17
SLIDE 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

slide-18
SLIDE 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

slide-19
SLIDE 19

String split and join

slide-20
SLIDE 20

split and join

split() and join() are different sides of the same

coin

split string to list join list to string

slide-21
SLIDE 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(“,”)

slide-22
SLIDE 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(“/”)

slide-23
SLIDE 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!

slide-24
SLIDE 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)

slide-25
SLIDE 25

Doesn’t have to be a comma

s_list = [“one”, “two,” “red”, “blue”] seuss = “ fish “.join(a_list) print(seuss)

slide-26
SLIDE 26

Csv files

slide-27
SLIDE 27

CSV files

csv files are basically spreadsheets, but as rows of

CSVs “column1”, “column2” “puffin”, “5” “dog”, “2”

slide-28
SLIDE 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

slide-29
SLIDE 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

slide-30
SLIDE 30

Regular vs csv module

with open(“f.csv”,”r”) as csvfile:

all_lines = csvfile.readlines() for row in all_lines[1:] #do stuff with row

import csv with open(“f.csv”,”r”) as csvfile:

reader = csv.reader(csvfile) reader.next() for row in reader: #do stuff with row

slide-31
SLIDE 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”])

slide-32
SLIDE 32

List slicing example

slide-33
SLIDE 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]

slide-34
SLIDE 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)

slide-35
SLIDE 35

Nested Loop Example

slide-36
SLIDE 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!

slide-37
SLIDE 37

Any questions you want to see?