## Gotchas ---- ### Converting to integer * The built-in function `int()` can only handle one value at a time; that is, it cannot handle a list of strings ----- ### Plus operator * Can handle two numbers or two strings; that is, it cannot handle a number and a string, or vice-versa. ----- ### Percent sign operator * `a % b` returns the remainder of `a` divided by `b` ----- ### Strings * They are immutable — they cannot be changed. For example `s.lower()` does not change `s`; it does return a new string whose character sequence are those of `s` with all alphabetic characters in lowercase. * `s.capitalize()` returns a new string whose character sequence are those of `s` with the first character in the new string capitalized and the remaining alphabetic characters in lowercase. ----- ### Pycharm * When typing in a weblink into the console, type a space after the link ----- ### Indexing * Python indexing begins at 0 ----- ### Web reading * Import local library url.py. * Use `url.get_text()` to get the contents of a web page. * Use `url.get_dataset()` to get a dataset from the web. ----- ### Problem solving * Never hardcode values as an answer; you will get the problem wrong when we test your solution with different inputs. ---- # you must get input in the specified order or else the words generated from the seed may be different # '==' tests (boolean) equality whereas '=' is assignment # accumulators must be initialized before loops # [] is subscripting into a sequence # you can modify a list # the len() will return an integer of the number of elements in a sequence # you can't use an operator in a print statement ( you actually can but not the " Prof. Cohoon way" ) # '//' means integer division # 5.0 // 2 = 2.0 ~tricky~ # to decode the content of a weblink use 'UTF-8' # folders should end in a '/' # when you want to specify a range you must use the keyword range # Ex: for i in ( 0, n ): <- bad but legal in Python # first index in a list is 0 # '()' means a "tuple" or ordered pair (sequences); is like a list # end= will allow you to control the ending of the print statement # Escape sequences: '\t' = tab; '\n' = new line # accumulators that sums should start at 0; lists should be the empty brackets [], and strings '' # you can "remove" strings with replace() using the empty string '' as the replacement # ranges are exclusive for the second term; you must add one to get up to that value # string functions must be called on a individual string and not on an entire list -> use a loop!! # functions always have '()' Ex: 'reply.split' does not do what you want # compound statements have colons ':' while loops, for loops, if statements # min() and max() do not have to be used on sorted lists # sorted(): you give the function a list and the function returns a sorted version of the list; does not alter the original list list = [ 3, 1, 4 ] => [ 1, 3, 4 ] # for loops are used when you do not know how many times you will repeat some action; when you known the size you can iterate on the elements themselves # you cannot compare numbers to strings and vice-versa # strings are compared using lexicographical (dictionary) order # s.split() will hand back a list of strings; .split() is not a list function meaning you cannot split a list # range() is a built-in function that hands back a sequence of values that goes from the first element to the (last element - 1) # never write x = x.append( v ); never use assignment with the append() function on a list