Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIT 590 Intro to Programming Lecture 4. Agenda Doubts from HW1 and HW2 Main function Break, quit, exit Function argument names, scope What is modularity!

Similar presentations


Presentation on theme: "CIT 590 Intro to Programming Lecture 4. Agenda Doubts from HW1 and HW2 Main function Break, quit, exit Function argument names, scope What is modularity!"— Presentation transcript:

1 CIT 590 Intro to Programming Lecture 4

2 Agenda Doubts from HW1 and HW2 Main function Break, quit, exit Function argument names, scope What is modularity! Snowman example Lists Please note that we are not covering recursion (yet!)

3 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!

4 Proper way to end your python file if __name__ == “__main__”: starting_function() When the program runs, it will begin at starting_function() Advantages of doing this Code will run automatically when you hit Run Your unit tests (coming up shortly) will not execute the whole program.

5 Break, quit, exit How to stop a loop? YOU WILL NOT LOSE POINTS IF THIS IS A MISTAKE YOU MADE Quit() is a bad bad idea. Quit exits the python process. You only wanted to end your program. You did not want to end Python. Sys.exit() also a bad idea That quits the execution of the program and that means that subsequent commands are not executed break is good but can make code hard to read when code is long The ideal way is to set a boolean

6 functions and argument passing Options for question 1 are A) 4 B) 8 C) -8 D) -4 E) Error of some sort

7 Scope Options for question 2 are A) 4 B) 8 C) -8 D) -4 E) Error of some sort

8 Functions, slicing, scope Options for question 3 are A) 92 B) 90 C) 3 D) 1 E) 5

9 Modularity Snowman example HW2 focus is on modularity Not a hard HW but you should be writing more functions than the ones prescribed in the assignment Do not worry about the m

10 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

11 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]]

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

13 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

14 Range generates a list Range basically produces a list range(1,10) 1 through 9 range(1,17,4) – in steps of 4 [1, 5, 9, 13] xrange versus range xrange is more memory efficient but does not give you an actual list

15 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

16 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


Download ppt "CIT 590 Intro to Programming Lecture 4. Agenda Doubts from HW1 and HW2 Main function Break, quit, exit Function argument names, scope What is modularity!"

Similar presentations


Ads by Google