CIT 590 Intro to Programming Lecture 5 – completing lists
Textbook sections skipped Writing a function that calls itself (Recursion) – postponed till later Raising exceptions – postponed Please read your textbook and work through the examples by actually typing them up.
Agenda Lists The concept of side effect Be careful with list manipulations Strings Tuples Hoping to complete Chapter 4
lists ls = [1, 4, 5] Very similar to strings in terms of some of the basic operations Slicing is your best friend What is [2, 3, 4] + [7, 8, 9]? ls * 3 will repeat the list 3 times List functions Append – add elements Extend - add this other list at the end Index - find and return index Iterating over the elements of a list for listelement in lst: Python has no issue with mixed types >>> [1, 0.45, True, 'abc', [1,2,5]] [1, 0.45, True, 'abc', [1, 2, 5]]
List functions work by side effect ls.append() automatically changes the value of ls The function append actually returns the value ‘None’, so if you were to do ls = ls.append(5) or ls = ls.extend([5, 6, ‘t’]) Disaster! >>> lst = [45, '6', True] >>> lst.append(lst.append(576)) What will print lst give me now?
Fixed sized lists (arrays?) You can initialize a fixed length list by doing something like [None] * 10 [0] *5 Fixed length lists behave very similar to the concept of arrays found in other programming languages
Range Range basically produces a list range(1,10) 1 through 9 range(1,17,4) – in steps of 4 [1, 5, 9, 13] Xrange on the other hand, is a better thing to use when you are not interested in actually creating the list.
Assignment by reference as opposed to value(copy) A variable is always assigned by reference in Python If you’ve seen pointers in C, this is equivalent to that concept You can make a true copy by doing a complete slice Function arguments are also passed by reference Be very careful when doing this See sneakyAppend for an example
Identity and equality Use the identity testing operator ‘is’ if you want to ensure two variables hold references to the same object Use ‘==‘ operator if you want to ensure that the two variables have the same value, even if they are not the same object a is b implies a == b, but not necessarily the other way around
Tuples Defined using () Immutable - once defined I cannot change individual elements Similar to the list function there is a tuple function which makes a tuple out of a string or for that matter any list Very useful when you want to return multiple things from a function – getMaxAndMin.py Why not return things as a list? The mutability of a list makes it a somewhat dangerous datastructure
Strings are somewhat like lists but … Strings are immutable Lists are mutable If a is a list I can do things like a[0] = ‘hahaha’ When you do b = a for a list you have to careful String to list conversion list(‘computer science rocks!’) Stringifying a list ''.join(lst) Join is a powerful function that basically uses the string as a separator between elements of the list argument '||'.join(['45','56','56'])
Strings and cool string functions Split Join Both split and join will work essentially with any symbol for concatenation or separation Grab sentences using the ‘.’ separator
Sets They do not allow repeated elements set() Since sets are unordered collections, they do not support things like indexing However they do support most standard set operations – union, intersection, difference. Best way to eliminate dupes?
A few examples from the book Encryption (simpleEncryption.py)
Hands on example.. Most common starting letter for first names of students in this class Any guesses?