CIT 590 Intro to Programming Lecture 4 Random utilities to improve efficiency • Search everything! • Beyond Compare • Keyboard shortcuts • Not essentially but oh.

Download Report

Transcript CIT 590 Intro to Programming Lecture 4 Random utilities to improve efficiency • Search everything! • Beyond Compare • Keyboard shortcuts • Not essentially but oh.

CIT 590
Intro to Programming
Lecture 4
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!)
Agenda
• docStrings
• Lists
• Tuples
docStrings
• http://www.python.org/dev/peps/pep-0257/
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
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
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’])
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
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)]
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
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
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
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
Cool string functions
• Split
• Join
• Both split and join will work essentially with any symbol for
concatenation or separation
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
Examples from the book
• Palindrome
• Encryption – simpleEncryption.py
Summary
• Lists
• Strings are lists? Cool!
• Tuples
• Sets