cisc101 reminders notes today
play

CISC101 Reminders & Notes Today Assignment 2 grades are posted - PowerPoint PPT Presentation

CISC101 Reminders & Notes Today Assignment 2 grades are posted in Moodle Cover material on exceptions from last lecture Slides 52-60 Test 2 is marked Continue with exceptions Grades will be posted in Moodle


  1. CISC101 Reminders & Notes Today • Assignment 2 grades are posted in Moodle • Cover material on exceptions from last lecture – Slides 52-60 • Test 2 is marked • Continue with exceptions – Grades will be posted in Moodle – Tests will be handed back in tutorial this week • Strings – What we already know – What we already know • Assignment 3 is now posted – Keywords and BIFs – Due on Sunday, March 20 th – Methods (lots of them!) • May have a guest lecture ... – Demos – Notes will not be posted on the website • Basic file input and output – Related questions will be on the exam Winter 2011 CISC101 - Whittaker 1 Winter 2011 CISC101 - Whittaker 2 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod None – What is it and Why is it Useful? Demo: Robust Input Between Limits • Modify getInt(…) from MoreRobust.py • None is a built-in constant – Indicates the absence of a value ( i.e. , nothing) – Can supply limits for the integer number • None is not zero • What if you don’t want to use one or both limits? – Use default arguments! – Zero is a number, None is not • What would be a good default limit? • What would be a good default limit? • Use it when you need a value but don’t have one • Use it when you need a value but don’t have one – We don’t want to assign an inappropriate limit … – Return it if you can’t return something meaningful – Solution: use None ! – Use it to create a variable for which you have no value – Use it for default arguments for which there are no sensible values to assign • Demo: MoreRobustRange.py • You can test to see if something equals None in a boolean expression Winter 2011 CISC101 - Whittaker 3 Winter 2011 CISC101 - Whittaker 4 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  2. Raising Exceptions Raising Exceptions - Cont. • What do you do when your function cannot do its • You can raise (or “throw”) an exception by using the raise keyword job? raise exception_name • You could return something so the invoking function knows that there is a problem • If you want to supply a “reason” as well, • Or, you could raise an exception raise exception_name ( reason_string ) – This is better in many situations Winter 2011 CISC101 - Whittaker 5 Winter 2011 CISC101 - Whittaker 6 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod Raising Exceptions - Cont. Raising Exceptions - Cont. • This is just like creating an error condition, but in • Whenever you raise an exception, the function an artificial way … that raised the exception is halted – No other code in the function will execute • Demo: RaiseException.py • If the function call was part of an expression then the rest of the expression will not be evaluated the rest of the expression will not be evaluated • Demo: RaiseExceptionWithMessage.py • As you know – if the exception is not caught, you will see the nasty red stuff! Winter 2011 CISC101 - Whittaker 7 Winter 2011 CISC101 - Whittaker 8 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  3. Raising Exceptions - Cont. Strings • String manipulation is a frequent activity in any • It is easiest to just raise one of the existing programming language exceptions – Speech analysis – Text searching and indexing • If it is because of a parameter error then – Spell and grammar checkers ValueError is appropriate – Program (code) interpretation – Program (code) interpretation – Scanning emails for SPAM – … and more • Creating our own exception objects is beyond the scope of this course • A string is a kind of data structure – Like a tuple, but they have lots of methods • Tuples have very few methods Winter 2011 CISC101 - Whittaker 9 Winter 2011 CISC101 - Whittaker 10 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod Strings Thus Far Strings Thus Far – Cont. • String literals • input(…) is a BIF "Hello there! “ – Returns user input from the keyboard as a string ‘CISC 101‘ """Multiline • You can use escape sequences string""" – \n , \” , \’ , \\ , \t – \n \” \’ \\ \t • You can store a string in a variable • You can store a string in a variable – These control how a string is displayed – Just like anything else • They are of type str • The string format() method is useful to format • str(…) is a BIF numeric output for display – Returns a string version of the given argument Winter 2011 CISC101 - Whittaker 11 Winter 2011 CISC101 - Whittaker 12 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  4. Strings Thus Far – Cont. Strings And Collections - Similarities • Many collection “accessories” work with strings • You can concatenate strings using + – The slice operator [ : ] • You can generate repeating strings using * – in and not in • A string must be placed on both sides • You can compare strings – for loops – Use == , > , < , >= , <= and != – len(…) BIF – len(…) – Just like comparing numbers, but you must have a – Just like comparing numbers, but you must have a – list(…) and tuple(…) BIFs string on both sides of the operator • Create a list or a tuple with the individual characters – Strings are compared on the basis of the ASCII code – sorted() BIF values for their individual characters • Returns a sorted list of individual characters – reversed() and enumerate(…) BIFs • They are a type of collection • Demo: StringsAsCollections.py – A collection a characters Winter 2011 CISC101 - Whittaker 13 Winter 2011 CISC101 - Whittaker 14 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod Strings And Collections - Differences Other String BIFs – Unicode and ASCII • Strings are immutable • chr(…) – Cannot put the slice operator on the left side of the – Takes an integer argument (a Unicode value) assignment operator • For the “narrow build” of Python that we use, this number can – del does not work range from 0x0000 to 0xFFFF – 2 bytes, compared to ASCII’s 1 byte – But ASCII values still work! – Returns the corresponding character – Returns the corresponding character • Tuples only have two methods • Tuples only have two methods – count(…) and index(…) • ord(…) does the reverse of chr(…) – Strings have many methods … – Takes a single character as a string for the argument – Returns the character’s code value • Demo: ASCIITable.py Winter 2011 CISC101 - Whittaker 15 Winter 2011 CISC101 - Whittaker 16 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  5. ASCII Characters - Observations Unicode Characters • The empty boxes are non-printing characters • Demo: UnicodeTable.py – They do something like <enter> or <del> or <\n>, etc . – Unicode numeric values are not displayed and only a fraction of the table is printed out • Some characters seem to have a <backspace> – Most empty boxes represent un-assigned Unicode built in … values • ASCII 32 is a space • They really are empty • They really are empty • ASCII 10 must be an <enter> (or “newline”) • Demo: UnicodeBox.py • Keyboard characters stop at ASCII 127 • Characters from ASCII 128 to 254 are called “extended characters” – Not all of them are available in the console window Winter 2011 CISC101 - Whittaker 17 Winter 2011 CISC101 - Whittaker 18 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod Character Codes for Escape Characters String Methods • Demo: EscapeCharacters.py • Just like a list, a string has many methods – Uses the ord(…) BIF – 35 of them (a subset) are listed here • The next 3 slides list the methods in alphabetical • You now know that you could use the chr(…) BIF order to generate these escape sequences … – There is no other description – There is no other description – Note the use of default arguments • Remember that they are invoked as follows: string_variable . method_name () Winter 2011 CISC101 - Whittaker 19 Winter 2011 CISC101 - Whittaker 20 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

Recommend


More recommend