Course Material
|
TEST 1 MATERIAL
Iteration on sequences
sequence types: range, string, list, tuple
returning a sequence, assigning a sequence
in operator
for-in loop, loop variable
printing using f-strings
input files
open, read, readlines, close
Sequences and strings
common operations/functions:
+, *, len
indexing via [i]
slicing via [s:e] and [s:e:n]
traversal
can traverse using 'for X in SEQ: ... X ...'
can traverse using 'for i in range(len(SEQ)): ... SEQ[i] ...'
can test membership using 'X in SEQ' and 'X not in SEQ'
each type of sequence has its own type-specific methods
unlike functions, called as SEQ.METHOD(INPUTS)
strings
sequence of characters, immutable
methods
type related: isalpha, isupper, islower, isspace
case related: capitalize, upper, lower
format related: center, rjust, strip, rstrip
search related: count, find, replace
traversing a string
by character vs. by index
building a copy of a string (accumulator pattern)
examples: palindrome, rotation, cipher
Lists
sequence of arbitrary items, mutable
methods
search related: count, index
order related: sorted, reversed, sort, reverse
updates: append, extend, insert, remove
strings to lists
string split method, splitting user input into words/numbers
building lists
building a list (accumulator pattern), lists of counters
examples: dice stats, averaging, acronym
Lists and files
reading in and storing file contents
strip to remove line breaks
large file of words
examples: isWord, wordStats, anagrams
try-except to catch and handle errors
efficiency: sequential vs. binary search, multiple passes
Nested lists and dictionaries
simple vs. complex data, records as lists, lists of lists
reading from a csv file, building a list of lists
searching based on different fields, within fields
data files & tasks
state capitals: getCapital, getGPS, quiz
HDI stats: getHDI, getRank, displayAllAbove
tweets: inTweets, inMentions, mostLiked, onDay, allDays
dictionaries
dictionaries store key:value pairs
create using {}, access using []
any type of value can be the index (e.g., string)
examples
manyDecisions, allDays, wordCounts, anagrams
|