CS 105: TOPIC 8 – STRINGS AND FILES TOPIC 9…ISN'T IN THIS
Max Fowler (Computer Science) https://pages.github-dev.cs.illinois.edu/cs-105/web/
JULY 5, 2020
TOPIC 9ISN'T IN THIS Max Fowler (Computer Science) - - PowerPoint PPT Presentation
CS 105: TOPIC 8 STRINGS AND FILES TOPIC 9ISN'T IN THIS Max Fowler (Computer Science) https://pages.github-dev.cs.illinois.edu/cs-105/web/ JULY 5, 2020 Week 5 Video Series Topics String operations Slicing Splitting, joining
Max Fowler (Computer Science) https://pages.github-dev.cs.illinois.edu/cs-105/web/
JULY 5, 2020
String operations
Slicing Splitting, joining
Files
Writing, flushing, closing
Comma-separated values (CSV files)
Reading, splitting, filter
Coding patterns – how to recognize and apply common
Slicing is how we "slice" out a part of a collection
Given…
1.
2.
3.
1.
Note the white space!
2.
3.
my_str = "Hello" my_str_2 = my_str[:] Copies the string More important for lists later on…
print("_{}_".format(my_str[-4:-1])) Prints: _ 10_ How? -4 -> 4 spots back from the end of the string -1 -> 1 spot back from the end of the string
Remove parenthesized regions from a string
What is the slice provided when I use the following
split separates a string into a list, based on a separator. "I want all the words in this sentence".split() #The default is whitespace, so we'd get ["I", "want", "all", "the", "words", "in", "this", "sentence"] A very common separator is the comma – comma separated
"1,2,3,4".split(",")
Joining is the opposite of splitting! Fairly common pattern – split, process, rejoin
Given a string of comma separated
Add two to each number and return a comma
How can we do this?
A split, a loop, and a join!
What list do I get when I use the following
"This is a sample!".split("s")
Files are how data is stored
Operating system manages
Disk is slow open moves files to memory in
file_object = open('filename', 'w') file_object.write('thing to write') file_object.close() automatic at program end file_object.flush()
Will automatically close the file at the end!
Write a shopping list to a file…
Opening a file moves the file temporarily
Very common data format Can be generated from spreadsheet programs – or
To process…
Each "row" is a line in the file split(',') can separate columns Indexing reads columns of interest
I have a CSV of names and car makes/brands I want to count how many times each brand shows up!
Which of these will skip the first line in a file after reading the lines in?
for line in lines[1:]: for line in lines[:1]: for line in lines[1]: for line in lines[2:]:
Coding is a lot like building a birdhouse Recognize task -> use a pattern from your toolkit
Use a hammer and nails to fix the roof to a
Use a for loop to process a list…
Check if even/odd Visit everything in a collection Sum Counter Finding "best" in collection Filtering a collection
total = 0 # initialize variable to zero … # usually in a loop total += value # accumulate … total … # do something with the total Also Average = sum / count
counter = 0 # initialize variable to zero … # usually in a loop and/or conditional counter += 1 # increment … counter … # do something with the counter
current_best = a value you know is worse than best for thing in collection: if thing is better than current_best: current_best = thing return / do something with current_best
Write a function that counts the number of even
What patterns are needed here?
Even/odd check pattern Filtering pattern
You are asked to count how many strings in a list have
["zebra", "bob", "cobra", "fish", "a"] would have a
What two patterns could you combine for this task?