Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIT 590 Intro to Programming Lecture 4. How are assignments evaluated Pay attention to what the HW says it is trying to teach you about ‘modular programming’

Similar presentations


Presentation on theme: "CIT 590 Intro to Programming Lecture 4. How are assignments evaluated Pay attention to what the HW says it is trying to teach you about ‘modular programming’"— Presentation transcript:

1 CIT 590 Intro to Programming Lecture 4

2 How are assignments evaluated Pay attention to what the HW says it is trying to teach you about ‘modular programming’ ‘lists’ If it is a mathematical/scientific program we will care more about correctness. A math error in HW1 is more pardonable than one in HW2 If it is a game, the fun element is important. If it is a user interface (we’ll have one later) – make it look nice! Pay a ton of attention to function names and specs Style is hard to define but you know bad style when you see it Plagiarism is often detectable and will be dealt with harshly Do not post your question on stack overflow!

3 Agenda Lists The concept of side effect Be careful with list manipulations Tuples

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 Count – count the number of occurrences of an element

5 lists 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]]

6 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?

7 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

8 Range, zip Range basically produces a list range(1,10) 1 through 9 range(1,17,4) – in steps of 4 [1, 5, 9, 13] Just like range produces lists you can use the function zip for tuples >>> zip([1,2],[1,2]) [(1, 1), (2, 2)]

9 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

10 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

11 Sorted lists You can use the in-built ‘sorted’ function You can use ls.sort You can get fancy and use ls.sort with an argument First time we are passing a function as an arguments 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 fancySort

12 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

13 Strings are somewhat like lists but … Strings are immutable and 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. Fine to do it for strings For lists you often actually want b = a[:] 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'])

14 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

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

16 A few examples from the book Palindrome (palindrome.py) Encryption (simpleEncryption.py)

17 Hands on example.. Most common starting letter for first names of students in this class Any guesses?


Download ppt "CIT 590 Intro to Programming Lecture 4. How are assignments evaluated Pay attention to what the HW says it is trying to teach you about ‘modular programming’"

Similar presentations


Ads by Google