CIT 590 Intro to Programming Lecture 7. Agenda Configuring IDLE (now that your code is getting huge) Exceptions Testing for exceptions – the weird self.assertRaises.

Slides:



Advertisements
Similar presentations
Exception Handling Genome 559. Review - classes 1) Class constructors - class myClass: def __init__(self, arg1, arg2): self.var1 = arg1 self.var2 = arg2.
Advertisements

Container Types in Python
CIT 590 Intro to Programming Classes. Schedule change The upcoming HW (HW6) is your last major Python HW. It will involve object oriented programming.
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 16 Dictionaries 5/10/09 Python Mini-Course: Lesson 16 1.
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Python Dictionary.
Guide To UNIX Using Linux Third Edition
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Lecture 23 – Python dictionaries 1 COMPSCI 101 Principles of Programming.
CS324e - Elements of Graphics and Visualization Java Intro / Review.
Computing Science 1P Large Group Tutorial 17 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CIT 590 Intro to Programming Style Classes. Remember to finish up findAllCISCourses.py.
Bags, foreign keys, selects, joins, and yes, btw, SVN 2/18/2014.
Introduction to Python
CS 177 Week 11 Recitation Slides 1 1 Dictionaries, Tuples.
Chapter Nine: Subprograms Lesson 09. What are they  Modularized code  Might return a value  Functions  Or not  Procedures  Subroutines  In object.
Lists in Python.
November 15, 2005ICP: Chapter 7: Files and Exceptions 1 Introduction to Computer Programming Chapter 7: Files and Exceptions Michael Scherger Department.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
Guide to Programming with Python Chapter Seven (Part 1) Files and Exceptions: The Trivia Challenge Game.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
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!
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Built-in Data Structures in Python An Introduction.
Chapter 8 More On Functions. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. First cut, scope.
CIT 590 Intro to Programming Lecture 5 – completing lists.
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
Python Arrays. An array is a variable that stores a collection of things, like a list. For example a list of peoples names. We can access the different.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 Dictionaries and Sets.
CIT 590 Intro to Programming Lecture 6 (dictionaries)
Chapter 9 Dictionaries and Sets.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
Scoping and Namespaces CSIS 1595: Fundamentals of Programming and Problem Solving 1.
PHP vs. Python. Similarities are interpreted, high level languages with dynamic typing are Open Source are supported by large developer communities are.
Arrays Adapted from materials created by Dr. Donald Bell, Cal Poly 2000 (updated February 2004)
14. DICTIONARIES AND SETS Rocky K. C. Chang 17 November 2014 (Based on from Charles Dierbach, Introduction to Computer Science Using Python and Punch and.
Compsci 101.2, Fall Plan for October 29 l Review dictionaries and their use  Very efficient, easy to use  Efficiency doesn't matter much for.
CSC Introduction to Data Structures Devon M. Simmonds Computer Science Department University of North Carolina Wilmington Wilmington, NC 28403
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’
CSE 130 : Winter 2009 Programming Languages Lecture 11: What’s in a Name ?
7. Lists 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
CIT 590 Intro to Programming Files etc. Agenda Files Try catch except A module to read html off a remote website (only works sometimes)
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
CIT 590 Intro to Programming Lecture 4. Random utilities to improve efficiency Search everything! Beyond Compare Keyboard shortcuts Not essentially but.
Dr. Philip Cannata 1 Python Overview. Dr. Philip Cannata 2 “Bad program” developed during class # The following python statement has python variables.
CIT 590 Intro to Programming Lecture 6. Vote in the doodle poll so we can use some fancy algorithm to pair you up You.
Dictionaries. The compound types you have learned about - - strings, lists, and tuples – use integers as indices. If you try to use any other type as.
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
Today… Files from the Web! Dictionaries. Lists of lists. Winter 2016CISC101 - Prof. McLeod1.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
More Python Data Structures  Classes ◦ Should have learned in Simpson’s OOP ◦ If not, read chapters in Downey’s Think Python: Think like a Computer Scientist.
10 - Python Dictionary John R. Woodward.
Python – May 18 Quiz Relatives of the list: Tuple Dictionary Set
When to use Tuples instead of Lists
Containers and Lists CIS 40 – Introduction to Programming in Python
Department of Computer Science,
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Python Primer 2: Functions and Control Flow
Bryan Burlingame Halloween 2018
Programming Logic and Design Fourth Edition, Comprehensive
CISC101 Reminders Slides have changed from those posted last night…
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Intro to Programming Lecture 5
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
Advanced Python Concepts: Exceptions
Bryan Burlingame Halloween 2018
Presentation transcript:

CIT 590 Intro to Programming Lecture 7

Agenda Configuring IDLE (now that your code is getting huge) Exceptions Testing for exceptions – the weird self.assertRaises Dictionaries

Error handling Raise def defensiveWithdraw (amount, balance): if balance < amount: raise ValueError, "insufficient funds" balance -= amount

Testing our error checking. How? Unit Test!!! self.assertRaises The syntax is a little weird Argument 1 – type of error you are asserting gets raised Argument 2 – the function name (name only!) Argument 3 – the arguments to the function

Dictionary Dictionaries consist of key, value pairs Also know as Hashmaps, associative arrays in other languages Initialized with dictionary = {} And then we can add key, value pairs as follows dictionary[‘name’] = ‘Arvind’ dictionary[‘age’] = 92 Very efficient way of storing sparse data A lot of matrices and vectors that come up in probability are sparse, so you could use an integer key and store values in that manner

Live coding – word count example See wordCounter.py in the repository Modifications will be made in class We will also write unit tests with dictionaries

Dictionary update operation. Copying dictionaries dict_one = {‘abc’: 3, ‘def’:7, ‘xyz’: 9} dict_two = {‘def’: 5, ‘pdq’ : 4} dict_one.update(dict_two) As with lists or for that matter any kind of assignment, you need to be careful when you do assignment, since that is done by reference So dict1 = dict2 will make both dictionaries refer to the same value If you want to make what is generally called a ‘shallow copy’, you need to use the copy method dict 2 = dict1.copy()

Altering dictionaries by passing them into functions Remember that arguments are passed into functions by reference, so you could get some unintended consequences if you are not careful Similar to lists, dictionaries are passed ‘by reference’ to functions

Initializing a dictionary version 2 The dict() function can convert a list of 2 element tuples into dictionaries Very useful when you want to initialize a dictionary by using 2 lists, one of which has keys = [‘Arvind’, ‘Aashish’] values = [33, 28] dict(zip(keys, values))

Looping over a dictionary For e in dict: This will loop over the keys For e in dict.values(): As is somewhat obvious, this will loop over the values Since there is no guarantee about what order the keys are looped over, you might sometimes want to call the sorted function

Persistent variables The shelve module allows data to persist without using explicit file commands import shelve Data = shelve.open(“database”) this will make an implicit file called database And then data from that point can basically be treated just as any other dictionary Remember to close the shelf before quitting by using Data.close()

The internal dictionaries Python maintains its own dictionaries for your variables Locals() gives you the local variables Correspondingly global() gives you the global variables