Download presentation
Presentation is loading. Please wait.
Published byKaren Washington Modified over 9 years ago
1
CIT 590 Intro to Programming Lecture 4
2
Random utilities to improve efficiency Search everything! Beyond Compare Keyboard shortcuts Not essentially but oh so useful when you want to be in the zone CTRL + N Configure your IDLE to the up-down for history ALT + G – goto line number (v useful when things go wrong!)
3
Agenda docStrings Lists Tuples
4
docStrings http://www.python.org/dev/peps/pep-0257/
5
lists ls = [1, ‘abc’, 5] Very similar to strings in terms of slicing ls * 3 will repeat the list 3 times ‘+’, in operator work as expected List functions List.append and list.extend List.index del for deleting elements of a list Iterating over the elements of a list
6
Strings … lists You can always convert a string into a list of its characters List(‘abc’) Lists to strings the join operation ‘’.join[‘a’, ‘b’,’c’] Example – caseConversion.py
7
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.extend([5, 6, ‘t’])
8
Fixed sized lists (arrays, anyone?) 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
9
Range, zip Range basically produces a list Zip produces tuples Note that zip does not produce the cartesian product, it produces things in an ordered fashion zip([1,2,3],[5,6,7]) [(1, 5), (2, 6), (3, 7)]
10
Assigment 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 (a[:]) a = [1, 2, 3] b = a b [2] = 7 Function arguments are also passed by reference Be very careful when doing this sneakyAppend.py
11
Identity and equality ‘is’ versus ‘==‘ 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 => a == b, but not necessarily the other way around
12
Sorted lists You can use the in-built ‘sorted’ function sorted([‘a’, ‘b’, ‘A’]) You can use ls.sort You can get fancy and use ls.sort with an argument An argument which is a function – what???? For writing a function that works as an argument to sort def sortingFunc(x, y): #return 0 when x and y are ‘equal’ # return -1 when x is ‘less’ than y # return 1 otherwise Example – fancySort.py
13
Tuples Defined using () So it’s a list right??? No because this is immutable Similar to the list function there is a tuple function which makes a tuple out of a string or for that matter any list Tuple([‘a’, ‘b’, ‘c’]) Tuples are very useful when you want to return more than 1 value from a function – minMax.py
14
Cool string functions Split Join Both split and join will work essentially with any symbol for concatenation or separation
15
Sets They do not allow repeated elements set() Since sets are unordered collections, they do not support things like indexing However they do support things like union and intersection Intersection Union difference
16
Examples from the book Palindrome Encryption – simpleEncryption.py
17
Summary Lists Strings are lists? Cool! Tuples Sets
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.