Transcript Lecture 3
CIT 590 Intro to Programming Lecture 3 Few waitlist related logistics • I’d like to resolve the waitlist before assignment 3 is • • • • handed out You can still ‘sit through’ the course but your assignments might not be graded in a timely manner (sometimes not graded at all) We have 6 slots and some 20 people on the list. I might just have to use a lottery unless a few more drop If you are no longer interested in being on the waitlist, please send me an email If you are dropping please send me an email Pair programming • Both people in the pair get the same grade! • Perhaps the silliest reason why students lost marks last time • Someone submitted the wrong version • TRUST YOUR PARTNER • Set a reminder/alert for Thursday night to check which version has been uploaded • We will only grade the most recent version – ifs and buts in very few cases • Tools that might help • Dropbox – does minimalistic versioning • Beyond Compare – comparison tool • Version control tools like Git (we might cover this later in the course) Agenda • Few notes about style • More about strings • slicing • Recursion • Throwing exceptions • Functions returning nothing Function style 1. Give functions reasonable names – sometimes might be hard to come up with an elegant name (see 3 for a possible solution) 2. Define functions in their own separate block – Python is super flexible and allows you to do this anywhere, but that affects readability 3. Put down a docString – a comment that explains what the function does. Triple quoted string to begin the function Strings and string functions • name = ‘superman’ • len(name) for length • name[0] • ‘s’ • Slicing – to get portions of a string (or list) • Positive indices name[1:3] • Includes the first index and excludes the ending index • Negative index means grab characters from the end • name[-1] is the last character String and string functions • More slicing • name[1:] • name[:5] • name[:-5] • Copying a string • name[:] gives you all the character of the string • Looping through characters in a string for ch in string: The ‘+’ operator does concatenation for strings Recursion • function f calls itself with a smaller sized input • function f1 calling function f2 is common enough • You did this in both assignments extensively • Sometimes the solution to a big problem can become easy if you can solve a smaller sized problem • The factorial function for a positive integer • Product of all positive integers upto and including that integer • 5! = 1 * 2 * 3 * 4 * 5 • 10! = 1 * 2 * 3 * 4 * 5 * ..* 10 • Does factorial(n) contain other factorials in it? Recursion examples • numToString example from the book • Factorial • Doing fibonacci with and without recursion Recursion pitfalls • Not having a base case • Errors out very much like an infinite loop • Not thinking about the rate of growth of the solution • Fibonacci can actually be written nicely using loops and some clever swapping of variables • iterativeFibonacci.py Exceptions • Handling unexpected situations • Halt the execution so that the potential error does not propagate and cause an even bigger one • ‘defensive programming’ How to throw an exception in Python • For now we will have only 1 type of exception • raise RuntimeError, ‘negative interest rate’ • An exception will quit the program, so use it only when needed • Negative inputs • You think the user is trying to be destructive • Avoid using it in places where it might be genuine user error. • User picks something out of range – asks for the 8th character in ‘arvind’ Functions that return nothing • The NoneType • used fairly often to initialize the value of a variable when you do not know what it might eventually hold