Course Material
|
TEST 1 MATERIAL
Sequences
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'
each type of sequence has its own type-specific methods
unlike functions, called as str.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, char-by-char
examples: palindrome, Caesar cipher, Pig Latin
lists
sequence of arbitrary items, mutable
methods
search related: count, index
order related: sort, reverse
modifiers: append, extend, insert, remove
strings to lists
string split method
splitting user input into words/numbers
building lists
building a list, item by item with append/extend/+
list comprehensions, conditional comprehensions
examples: dice stats, averaging, acronym, Pig Latin phrases
big data
e.g., dictionary --> anagrams, basketball stats --> top players
User Interaction
input/output
input: numbers must be converted (int, float)
print: automatically separates values with spaces, adds newline
can avoid spacing using concatenation, e.g., str(percent)+"%"
stdout.write: sys module, writes without spaces & newline
files
open a file using open function
open(filename, "r") & open(filename, "w") return file objects
file input methods:
read() vs. read(N) vs. readline()
close a file using close method
file dialog windows (tkinter module)
filedialog.askopenfilename & filedialog.asksaveasfilename functions
|