CSCI/CMPE 4341 Topic: Programming in Python Review: Exam II Xiang Lian The University of Texas – Pan American Edinburg, TX 78539

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Object-Oriented Programming
The Web Warrior Guide to Web Design Technologies
Lecture 2 Introduction to C Programming
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Advanced Object-Oriented Programming Features
C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
ASP.NET Programming with C# and SQL Server First Edition
CSCI/CMPE 4341 Topic: Programming in Python Chapter 3: Control Structures (Part 1) – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
CSCI/CMPE 4341 Topic: Programming in Python Chapter 6: Lists, Tuples, and Dictionaries – Exercises Xiang Lian The University of Texas – Pan American Edinburg,
Chapter 13: Object-Oriented Programming
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
The University of Texas – Pan American
 2004 Prentice Hall, Inc. All rights reserved. Chapter 35 – Python Outline 35.1 Introduction First Python Program Python Keywords 35.2 Basic.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Microsoft Visual Basic 2005: Reloaded Second Edition
An Object-Oriented Approach to Programming Logic and Design
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
Chapter 8: Writing Graphical User Interfaces
1 Python CIS*2450 Advanced Programming Concepts Material for this lecture was developed by Dr. D. Calvert.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 9: Python XML Processing Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
The University of Texas – Pan American
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CSCI/CMPE 4341 Topic: Programming in Python Review: Final Exam Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Introduction to XML This presentation covers introductory features of XML. What XML is and what it is not? What does it do? Put different related technologies.
Built-in Data Structures in Python An Introduction.
The eXtensible Markup Language (XML). Presentation Outline Part 1: The basics of creating an XML document Part 2: Developing constraints for a well formed.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
How the Session Works Outline Practical on arrival Talk 1 Reflect on practical Clarify concepts Practical exercises at your own pace Talk 2: Further concepts.
Unit 10 Schema Data Processing. Key Concepts XML fundamentals XML document format Document declaration XML elements and attributes Parsing Reserved characters.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 13 GUI Programming.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Object-Oriented Application Development Using VB.NET 1 Chapter 10 VB.NET GUI Components Overview.
Creating visual interfaces in python
CSCI/CMPE 4341 Topic: Programming in Python Chapter 6: Lists, Tuples, and Dictionaries Xiang Lian The University of Texas – Pan American Edinburg, TX
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 7 Representing Web Data:
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 7: Introduction to Object- Oriented Programming in Python – Exercises Xiang Lian The University of.
Unit 4 Representing Web Data: XML
Topics Graphical User Interfaces Using the tkinter Module
This Week: Tkinter for GUI Interfaces Some examples
Tkinter GUIs Computer Science and Software Engineering
Chapter 7 Representing Web Data: XML
Chapter 3 Introduction to Classes, Objects Methods and Strings
Introduction to Python
The University of Texas – Pan American
CIS16 Application Development Programming with Visual Basic
CSCI 3328 Object Oriented Programming in C# Review: Exam I
The University of Texas – Pan American
I210 review.
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
Topics Graphical User Interfaces Using the tkinter Module
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
Object Oriented Programming in java
The University of Texas – Pan American
Presentation transcript:

CSCI/CMPE 4341 Topic: Programming in Python Review: Exam II Xiang Lian The University of Texas – Pan American Edinburg, TX

Review Textbook –Self-Review Exercises –Quick Quiz –Exercises Lecture slides (Chapters 6-9) –Lists, Tuples, and Dictionaries –Object-Oriented Programming –Graphical User Interface Components –Python XML Processing –Exercises 2

Review Multiple Choice True/False Statements Programming –Find bugs –Write the code Bonus Question (20 extra points) 3

Chapter 6: Lists, Tuples, and Dictionaries Creation and manipulation of sequences –String –List –Tuple Creation and manipulation of Dictionary Methods of sequences and Dictionary 4

Introduction Data structures –Structures that hold and organize information Sequences –Also known as arrays in other languages (e.g., C++) –Store related data types –3 types Strings Lists Tuples Mappings –In most languages called hashes or associative arrays –Dictionaries are the only python mapping container Key-value pairs 5

Example of Sequences C[0]-45C[-12] C[1]6C[-11] C[2]0C[-10] C[3]72C[-9] C[4]34C[-8] C[5]39C[-7] C[6]98C[-6] C[7]-1345C[-5] C[8]939C[-4] C[9]10C[-3] C[10]40C[-2] C[11]33C[-1] Name sequence (C) Position number of the element within sequence C 6

Creating Sequences – String Strings –Use quotes string1 = "hello" –Empty string string2 = "" Error! –string1[0]="A" 7

Creating Sequences – List Lists –Use brackets –Separate multiple items with a comma list1 = [1, 2, 3, 4] –Empty list list2 = [] Length of the list –len (list1) list1[0]=0 # OK! 8

Creating Sequences – Tuple Tuples –Use parenthesis –Separate multiple items with a comma tuple1 = (1, 2, 3, 4) tuple1 = 1, 2, 3, 4 –Empty tuple tuple2 = () –Singleton (or one-element tuple) singleton3 = 1, Comma (,) after 1 identify the variable signleton3 as a tuple Error! –tuple1[0]=0 9

Differences Between Lists and Tuples Differences –Tuples and lists can both contain the same data aList = [ 1, 2, 3, 4 ] aTuple = ( 1, 2, 3, 4 ) –For practical purposes though each is used to hold different types of items 10

Dictionaries Key-value pairs – Unordered collection of references – Each value is referenced through key in the pair – Curley braces ({}) are used to create a dictionary – When entering values Use { key1:value1, … } Keys must be immutable values – strings, numbers and tuples Values can be of any Python data type 11

Operations in Dictionaries Access dictionary element –dictionaryName [key] –dictionaryName [key] = value # update the value of an existing key Add key-value pair –dictionaryName [newKey] = newValue Delete key-value pair –del dictionaryName [key] 12

Dictionary Methods 13

Dictionary Methods (cont'd) 14

Sorting and Searching Lists Sorting a list –Use the sort method Searching a list –Use the index method 15

Chapter 7: Introduction to Object- Oriented Programming in Python Classes –Attributes Private attributes Class attributes –Methods Constructor: __init__ Get and Set methods Destructor: __del__ Objects –Creation of objects Inheritance –syntax 16

Object Orientation Classes –Encapsulate data Attributes –Encapsulate functions Behaviors –Act as blueprints Objects are instantiated from classes –Implement information hiding 17

Class Declaration Defining a Class –Class header Keyword class begins definition –Followed by name of class and colon (:) –Body of class Indented block of code –Documentation string Describes the class Optional Appears immediately after class header 18

Class Declaration (cont'd) Defining a Class –Constructor method __init__ Executes each time an object is created Initialize attributes of class Returns None –Object reference All methods must at least specify this one parameter Represents object of class from which a method is called Called self by convention 19

Get and Set Methods Access methods –Allow data of class to be read and written in controlled manner –Get and Set methods Allow clients to read and write the values of attributes respectively 20

Private Attributes Data and attributes not to be accessed by clients of a class Private attributes preceded with double underscore (__) –Causes Python to perform name mangling Changes name of attribute to include information about the class For example, attribute __hour in class Time becomes _Time__hour 21

Destructors –Method is named __del__ –Executed when object is destroyed No more references to object exist –Performs termination housekeeping before Python reclaims object memory Typically used to close network or database connections 22

Class Attributes One copy of attribute shared by all objects of a class Represents “class-wide” information –Property of the class, not an object of the class Initialized once in a class definition –Definition of class attribute appears in body of class definition, not a method definition Accessed through the class or any object of the class –May exist even if no objects of class exist 23

Inheritance: Base Classes and Derived Classes Base class –Called superclass in other programming languages –Other classes inherit its methods and attributes Derived class –Called subclass in other programming languages –Inherits from a base class –Generally overrides some base class methods and adds features 24

Chapter 8: Graphical User Interface Components GUI packages –tkinter Each GUI component class inherits from Widget Components –Label –Entry –Button –Checkbutton & Radiobutton Events –Mouse events –Keyboard events 25

tkinter Module Python’s standard GUI package – tkinter module tkinter library provides object-oriented interface to Tk GUI toolkit Each GUI component class inherits from class Widget GUI consists of top-level (or parent) component that can contain children components Class Frame serves as a top-level component 26 try: from Tkinter import * # for Python2 except ImportError: from tkinter import * # for Python3

27 tkinter Overview Frame Label Entry Text Button Checkbutton Radiobutton Menu Canvas Scale Listbox Scrollbar Menubutton Widget Key Subclass name Class name Widget subclasses:

Label Component – Label Labels –Display text or images –Provide instructions and other information –Created by tkinter class Label 28

Entry Component – TextBox Textbox –Areas in which users can enter text or programmers can display a line of text –Created by Entry class event occurs when user presses Enter key inside Entry component 29

Button Component – Button Buttons –Generates events when selected –Facilitate selection of actions –Created with class Button –Displays text or image called button label –Each should have unique label 30

Checkbutton and Radiobutton Components Checkbox –Small white square –Either blank or contains a checkmark –Descriptive text referred to as checkbox label –Any number of boxes selected at a time –Created by class Checkbutton Radio button –Mutually excusive options – only one radio button selected at a time –Created by class Radiobutton Both have on/off or True/False values and two states – selected and not selected (deselected) 31

Mouse Event Handling (cont'd) 32

Keyboard Event Handling Keyboard events generated when users press and release keys 33

Chapter 9: Python XML Processing XML documents –Tree structure –Tags –Elements –Data –Namespaces How to use Python to output XML documents? How to parse XML documents using Python packages? 34

XML Documents XML documents end with.xml extension XML marks up data using tags, which are names enclosed in angle brackets – elements –Elements: individual units of markup (i.e., everything included between a start tag and its corresponding end tag) –Nested elements form hierarchies –Root element contains all other document elements 35

36 XML Namespaces Provided for unique identification of XML elements Namespace prefixes identify namespace to which an element belongs Topic: Programming in Python

 2002 Prentice Hall. All rights reserved. Outline 37 fig16_02.py #!c:\Python\python.exe # Fig. 16.2: fig16_02.py # Marking up a text file's data as XML. import sys # write XML declaration and processing instruction print (""" """) # open data file try: file = open( "names.txt", "r" ) except IOError: sys.exit( "Error opening file" ) print (" ") # write root element # list of tuples: ( special character, entity reference ) replaceList = [ ( "&", "&" ), ( "<", "<" ), ( ">", ">" ), ( '"', """ ), ( "'", "&apos;" ) ] # replace special characters with entity references for currentLine in file.readlines(): for oldValue, newValue in replaceList: currentLine = currentLine.replace( oldValue, newValue ) Print XML declarationOpen text file if it existsPrint root elementList of special characters and their entity referencesReplace special characters with entity references

 2002 Prentice Hall. All rights reserved. Outline 38 fig16_02.py # extract lastname and firstname last, first = currentLine.split( ", " ) first = first.strip() # remove carriage return # write contact element print (""" %s """ % ( last, first )) file.close() print (" ") Extract first and last nameRemove carriage returnPrint contact elementPrint root’s closing tag

 2002 Prentice Hall. All rights reserved. Outline 39 fig16_04.py # Fig. 16.4: fig16_04.py # Using 4DOM to traverse an XML Document. import sys import xml.etree.ElementTree as etree # open XML file try: tree = etree.parse("article2.xml") except IOError: sys.exit( "Error opening file" ) # get root element rootElement = tree.getroot() print ("Here is the root element of the document: %s" % \ rootElement.tag) # traverse all child nodes of root element print ("The following are its child elements:" ) for node in rootElement: print (node)

Good Luck! Q/A