Lecture 3 - Overview. Object-oriented programming and classes Operators Very briefly about naming conventions Graphical user interfaces (GUIs)

Size: px
Start display at page:

Download "Lecture 3 - Overview. Object-oriented programming and classes Operators Very briefly about naming conventions Graphical user interfaces (GUIs)"

Transcription

1 Lecture 3 - Overview Object-oriented programming and classes Operators Very briefly about naming conventions Graphical user interfaces (GUIs)

2 Object-oriented philosophy Object = Data + Algorithm An object is a self-contained unit The object has a state Hiding internal structure Origin: The software is a model

3 Terminology Attribute: An objects public interface. In Python this can be both public instance variables and methods. Argument: Is here used for the variables that are sent to a method or function. Parameter: Equivalent to argument (in parameter) and return value (out parameter) Instance variables: Variables that are connected to a certain object. In Python these are (almost) always public, which makes them to attributes automatically. Class variable: Variables which are connected to a certain class, not an instance of a class. Can exists without any instances of the class.

4 A class with instance methods Methods are defined the same way as functions but inside a class definition. The first argument to a method is always a reference to the object that has called the method. The (strong) convention is to call this argument self. Instance variables are created with: self.variablename. # classes_02.py class SimpleClass: def assign_name(self, name): self.name = name def print_name(self): print "I'm %s." % self.name girl = SimpleClass() girl.assign_name("abby") girl.print_name() boy = SimpleClass() boy.assign_name("daniel") boy.print_name() $> python classes_02.py I'm Abby. I'm Daniel.

5 Constructors Constructors have the reserved name init (double underscores in the beginning and the end). The constructor can take arguments just as an ordinary method. # classes_03.py class SimpleClass: def init (self, name="unknown"): self.name = name $> python classes_03.py I'm Abby. I'm Unknown. def print_name(self): print "I'm %s." % self.name girl = SimpleClass("Abby") girl.print_name() unknown = SimpleClass() unknown.print_name()

6 Destructors Destructors have the reserved name del and are called when an object is deleted. # classes_04.py class SimpleClass: def del (self): print "Object 0x%08X is closing down." \ % id(self) print "Creating an object." obj_1 = SimpleClass() obj_1 = 1 # Implicit deletion print "Creating another one." obj_2 = SimpleClass() del(obj_2) # Explicit deletion $> python classes_04.py Creating an object. Object 0x00AD3C88 is closing down. Creating another one. Object 0x00AD3C88 is closing down.

7 Class variables Class variables are used to handle information that belongs to the class rather than specific instances of the class. The classic example is a variable that keeps track of how many instances of a class that exists at a given time point.

8 Class variables, example # classes_05.py class SimpleClass: # Class variable instances = 0 def init (self): self. class.instances += 1 print "Creating object %08X." \ " instances = %i" \ % (id(self), self. class.instances) def del (self): self. class.instances -= 1 print "Object 0x%08X is closing down." \ " instances = %i" \ % (id(self), self. class.instances) Creating object 023D3490. instances = 1 Creating object 023D34B8. instances = 2 Creating object 023D34E0. instances = 3 Creating object 023D3508. instances = 4 Creating object 023D3530. instances = 5 Now there are 5 instances. Object 0x023D3530 is closing down. instances = 4 Object 0x023D3508 is closing down. instances = 3 Object 0x023D34E0 is closing down. instances = 2 Object 0x023D34B8 is closing down. instances = 1 Object 0x023D3490 is closing down. instances = 0 Now there are 0 instances. objects = [] for i in range(5): objects.append(simpleclass()) print "Now there are %i instances." % objects[0].instances del objects print "Now there are %i instances." % SimpleClass.instances

9 Redefining classes # classes_06.py class SimpleClass: def init (self): print "Creating an instance of the first version." def print_info(self): print "I'm an instance of the first version." obj_1 = SimpleClass() obj_1.print_info() class SimpleClass: def init (self): print "Creating an instance of the second version." def print_info(self): print "I'm an instance of the second version." obj_2 = SimpleClass() obj_2.print_info() obj_1.print_info() We can redefine a class at run-time Objects of the old class keeps its methods. $> python classes_06.py Creating an instance of the first version. I'm an instance of the first version. Creating an instance of the second version. I'm an instance of the second version. I'm an instance of the first version.

10 Private instance variables Private instance variables and methods are created with two underscores in the beginning of the name It's still possible to access the function # private_01.py class SimpleClass: def init (self): self. secret_data = 19 def secret(self): print "Secret data: %i" \ % self. secret_data def public_method(self): self. secret() obj = SimpleClass() obj.public_method() print obj. secret_data obj. secret() $> python private_01.py Secret data: 19 Traceback (most recent call last): File "private_01.py", line 17, in <module> print obj. secret_data AttributeError: SimpleClass instance has no attribute ' secret_data'

11 Regarding get and set methods In Java get and set methods are often used to handle private instance variables. This is seen as needless in Python, even though it's possible. The philosophy is that it's up to the programmer to take responsibility over the code and what it's doing. If you want to access some inner part of an object shouldn't the language stop that. Documentation becomes more important when objects aren't as protected.

12 Inheritance A hierarchical class structure can reduce the code base and makes the relationship between different classes clearer. Through inheritance can a new class reuse existing methods and instance variables from an existing class. Constructors in base classes aren't called automatically.

13 Example of inheritance # -*- coding: utf-8 -*- # car_01.py class Car: def init (self, distance=0): self.distance = distance def repr (self): return "Travelled %i miles." \ % self.distance Car: Travelled 10 miles. Taxi: Travelled 13 miles. The trip costs 455 kr. class Taxi(Car): def init (self, distance=0, fare=10): Car. init (self, distance) self.fare = fare def total_fare(self): return self.distance * self.fare def repr (self): return Car. repr (self) + \ "\nthe trip costs %i kr." \ % self.total_fare() car = Car(10) taxi = Taxi(13, 35) print "Car:\n", car print "Taxi:\n", taxi

14 Operators Operators are characters with special meaning in the language. These include the standard mathematical operators as +, -, * and /, but also more programming oriented operators as [...] (for both getting and setting a value). Overloaded operators make it possible to use normal mathematical syntax for the builtin and own classes.

15 Operatorer are syntactical sugar Operators work in Python as special methods >>> a = "Hello " >>> b = "world!" >>> a + b 'Hello world!' >>> a. add (b) # Equivalent 'Hello world!' Note that a and b have different parts in the addition. a becomes the called object and b the argument. In other words: It's the add -method of a that will be called. A consequence of this is that addition not necessarily is a symmetric operation in Python. a + b can't be assumed to be the same as b + a.

16 Example 1: Addition with a class An overloaded operator can carry out anything. There is no requirement that operators shall do something that resembles the mathematical equivalent, even if it's strongly recommended. # -*- coding: utf-8 -*- # operators_01.py class ClassA: def init (self, text = "Default string"): self.text = text $> python operators_01.py Default stringdefault string def add (self, other): return ClassA(self.text + other.text) obj_1 = ClassA() obj_2 = ClassA() obj_3 = obj_1 + obj_2 print obj_3.text

17 Example 2: Different classes # -*- coding: utf-8 -*- # operators_02.py class ClassA: def init (self, text="instance of ClassA"): self.text = text def str (self): return self.text def add (self, other): return ClassA(str(self) + " + " + str(other)) class ClassB: def init (self, text="instance of ClassB"): self.text = text def str (self): return self.text def add (self, other): return "Addition not allowed with ClassB first." obj_a = ClassA() obj_b = ClassB() print obj_a + obj_a # => Instance of ClassA + Instance of ClassA print obj_a + obj_b # => Instance of ClassA + Instance of ClassB print obj_b + obj_a # => Addition not allowed with ClassB first.

18 Example 3: Indexing To read values getitem is used. To set values setitem is used. # -*- coding: utf-8 -*- # operators_03.py import random class ColorRepo: colors = ["Yellow", "White", "Black", "Pink", "Brown"] def getitem (self, index): return self. class.colors[index % len(self. class.colors)] cr = ColorRepo() for i in range(5): print cr[random.randint(0, 30000)]

19 Mathematical operators add (self,other) Addition, + sub (self,other) Subtraction, - mul (self,other) Multiplication, * div (self,other) Division, / floordiv (self,other) Integer division, // mod (self, other) Rest, % pow (self, other) Power of, ** or pow and (self, other) Logical and, and or (self, other) Logical or, or xor (self, other) Logical exclusive or, xor

20 Formatting and naming Variable names can't begin with a number. Conventions (PEP-8): 4 spaces for indentation most common Never mix spaces and tabs Limit all lines to a maximum of 79 characters. Classes begin with a capital letter Functions with lower case Multiple words in function names should preferably be separated with _: function_name

21 GUI: Graphical User Interface

22 Overview What is a GUI? Why do we need it? How to code in Python (one way) Difference in programming style: Event-driven prog. vs linear prog.

23 GUI examples The graphical frontend visible to the user

24 Why have a GUI? Increase the usability Easier to learn Not always the best solution, it's easier to make scripts for command line programs for example. Is perceived as more user friendly

25 GUI-library TkInter De facto standard, comes with almost all standard installations. Stands for Tk interface and can be seen as a connection library between Tk GUI toolkit and Python (i.e. an adapter or wrapper) One of several ways of creating graphics in Python (other GUI library: wxpython, 3D graphics: VPython)

26 What objects does a GUI contain? Window Color area Frame Label Button Button

27 A first code example # first_example.py from Tkinter import * root = Tk() my_widget = Label(root, text="hello world") my_widget.pack() root.mainloop()

28 Breakdown of the program # first_example.py from Tkinter import * root = Tk() my_widget = Label(root, text="hello world") my_widget.pack() root.mainloop() The program follows the standard way in Tk to create widgets: 1. Create a main container, in this case a Tk-object. 2. Create a widget of the type you want, here a Label-object. 3. Define the characteristics of the widget, for example color and form. 4. Call the pack-method to add the widget in the interface. 5. Repeat step 2-4 for each widget. 6. Call the mainloop-method for the main container to show it and all the underlying widgets on the screen.

29 Packing widgets The packer is called for each widget with a request of how the widget should look and act. The requests are partly regarding placement and extension in a container, and partly about how the widget should change when the container changes size. The packer tries to meet all requests. If two requests are in conflict, the packer will take regard to the packing order, that is in which order pack has been called.

30 Placement A widget's placement is governed by the argument to the pack-method. Sides in the container are denoted with side and placement along the sides with anchor. How the widget should adapt to changes in the main container is set with expand and fill.

31 Example on placements # graphics02.py from Tkinter import * Button(text="A button").pack() mainloop() # graphics03.py from Tkinter import * Button(text="A button").pack(anchor=ne) mainloop() # graphics04.py from Tkinter import * Button(text="A button").pack(expand=yes, fill=both) mainloop()

32 Several widgets # graphics07.py from Tkinter import * Button(text="Button 1").pack(side=TOP, fill=x) Button(text="Button 2").pack(side=RIGHT, fill=y) Button(text="Button 3").pack(expand=YES, fill=both) mainloop()

33 Common widgets Label. Text label. Button. Clickable button. Radiobutton. Choose between of many. Checkbutton. Choose any number of alternatives. Entry. Read a shorter string. Frame. Arrange different groups of widget.

34 Multiple frames from Tkinter import * from tkmessagebox import * class TestGUI: def init (self, parent): # Create frames for the GUI frametop = Frame(parent) frametop.pack() framebottom=frame(parent, bg="blue") framebottom.pack(expand=yes, fill=both) frametopleft=frame(frametop) frametopleft.pack(side=left) frametopright=frame(frametop) frametopright.pack(side=right) # Add buttons Button(frameTopLeft, text="launch window", \ command=self.show_message).pack() Entry(frameTopRight).pack() Button(frameBottom, text="exit", \ command=parent.destroy).pack() def show_message(self): showinfo("message title", "Message text") if name == ' main ': root = Tk() root.title("test window") app = TestGUI(root) root.mainloop()

35 More advanced widgets Menu. Ordinary menus. Menubutton. Buttons that open a menu. Canvas. Painting area for composed graphics. Text. Text box for longer texts. Dialog boxes (not really widgets).

36 Classes and inheritance in TkInter Each type of widget in TkInter is a class. Each widget in a GUI is an object. The main program can be created as subclasses to TkInter-classes, for example Tk or Toplevel

37 Event handling The purpose of the GUI is to communication efficiently with the user, so it must be able to take input from the user and act according to this. Input can either be through the mouse, keyboard or other similar equipment. The focus on events change the program structure and the design. Suitable for modularisation and object oriented programming.

38 Linear or event driven program In a linear program: Things are carried out in a predetermined order. All input is done at predetermined occasions. In an event driven program: There is often a central waiting loop (main loop) Program pieces called event manager are executed depending on which event that occur. Note that event driven programs doesn't necessarily have to be dependent on a GUI.

39 Control variables Used to smoothly handle data from widgets. Can connect several widgets which in that way are kept synchronized. The content can either be changed by the user or by the program through the control variable.

40 Example with control variables # events07.py from Tkinter import * root = Tk() t = StringVar() # Create control variable t.set('this value is given to both objects.') # Set start value # The button's and the text box text is connected to the same variable. Button(root,textvariable=t).pack(fill=X) Entry(root, textvariable=t).pack(fill=x) root.mainloop()

41 Check boxes and control variables # checkbutton01.py from Tkinter import * def printform(): if haslicence.get(): print 'The person has a driver's license.' if hasowncar.get(): print 'The person has a car.' import sys sys.exit() root = Tk() haslicence = IntVar() Checkbutton(root,text = 'Driver's license', variable =haslicence).pack(anchor=w) hasowncar = IntVar() Checkbutton(root,text = 'Car',variable = hasowncar).pack(anchor=w) Button(root,text = 'Save',command = printform).pack(anchor=s) root.mainloop()

42 Group radio buttons # radiobutton01.py from Tkinter import * def onmealclick(): print mealvar.get() def ondrinkclick(): print drinkvar.get() root = Tk() mealvar = StringVar() meals = ['Breakfast','Lunch','Dinner'] for meal in meals: Radiobutton(root,command = onmealclick, text = meal, value = meal, variable = mealvar).pack(anchor=w) drinkvar = StringVar() drinks = ['Milk','Water','Soft drink'] for drink in drinks: Radiobutton(root,command = ondrinkclick, text = drink, value = drink, variable = drinkvar).pack(anchor=w) root.mainloop()

Lecture 3 - Overview. More about functions Operators Very briefly about naming conventions Graphical user interfaces (GUIs)

Lecture 3 - Overview. More about functions Operators Very briefly about naming conventions Graphical user interfaces (GUIs) Lecture 3 - Overview More about functions Operators Very briefly about naming conventions Graphical user interfaces (GUIs) Function parameters Passed by reference, but the standard implication that the

More information

Programming Training. This Week: Tkinter for GUI Interfaces. Some examples

Programming Training. This Week: Tkinter for GUI Interfaces. Some examples Programming Training This Week: Tkinter for GUI Interfaces Some examples Tkinter Overview Set of widgets designed by John K. Ousterhout, 1987 Tkinter == Tool Kit Interface Mean to be driven by Tcl (Toolkit

More information

Application Note: Creating a Python Graphical User Interface. Matthew Roach March 31 st, 2014

Application Note: Creating a Python Graphical User Interface. Matthew Roach March 31 st, 2014 Application Note: Creating a Python Graphical User Interface Matthew Roach March 31 st, 2014 Abstract: This document contains 2 portions. First, it provides an introduction into phase and the use of phase

More information

CSE : Python Programming

CSE : Python Programming CSE 399-004: Python Programming Lecture 2: Data, Classes, and Modules January 22, 2007 http://www.seas.upenn.edu/~cse39904/ Administrative things Teaching assistant Brian Summa (bsumma @ seas.upenn.edu)

More information

CSE : Python Programming

CSE : Python Programming CSE 399-004: Python Programming Lecture 08: Graphical User Interfaces with wxpython March 12, 2005 http://www.seas.upenn.edu/~cse39904/ Plan for today and next time Today: wxpython (part 1) Aside: Arguments

More information

A Crash Course in Python Part II. Presented by Cuauhtémoc Carbajal ITESM CEM

A Crash Course in Python Part II. Presented by Cuauhtémoc Carbajal ITESM CEM A Crash Course in Python Part II Presented by Cuauhtémoc Carbajal ITESM CEM 1 Importing and Modules 2 Importing and Modules Use classes & functions defined in another file A Python module is a file with

More information

Tkinter: Input and Output Bindings. Marquette University

Tkinter: Input and Output Bindings. Marquette University Tkinter: Input and Output Bindings Marquette University Tkinter Variables Tkinter contains a useful mechanism to connect widgets to variables This allows us to have variables change when widgets do and

More information

Object-Oriented Python

Object-Oriented Python Object-Oriented Python Everything is an object. Every object has a value. a type. an identity. a namespace. CS105 Python def initstudent(student, eid): global A class is like a namespace. CS105 Python

More information

# arrange Label in parent widget

# arrange Label in parent widget Much of today s software uses a point-and-click graphical user interface (GUI). The standard library modules Tkinter and Tix allow for portable, event-driven, GUI development in Python. A Python/Tkinter

More information

Level 3 Computing Year 2 Lecturer: Phil Smith

Level 3 Computing Year 2 Lecturer: Phil Smith Level 3 Computing Year 2 Lecturer: Phil Smith We looked at: Debugging Previously BTEC Level 3 Year 2 Unit 16 Procedural programming Now Now we will look at: GUI applications. BTEC Level 3 Year 2 Unit 16

More information

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017 Overview of OOP Dr. Zhang COSC 1436 Summer, 2017 7/18/2017 Review Data Structures (list, dictionary, tuples, sets, strings) Lists are enclosed in square brackets: l = [1, 2, "a"] (access by index, is mutable

More information

Tkinter Part II: Buttons, Lambda & Dynamic Content

Tkinter Part II: Buttons, Lambda & Dynamic Content Tkinter Part II: Buttons, Lambda & Dynamic Content July 8, 2015 Brian A. Malloy Slide 1 of 11 1. We further investigate Labels and Buttons and hook Python actions to these widgets. We present lambda functions,

More information

Object Oriented Programming in Python. Richard P. Muller Materials and Process Simulations Center California Institute of Technology June 1, 2000

Object Oriented Programming in Python. Richard P. Muller Materials and Process Simulations Center California Institute of Technology June 1, 2000 Object Oriented Programming in Python Richard P. Muller Materials and Process Simulations Center California Institute of Technology June 1, 2000 Introduction We've seen Python useful for Simple Scripts

More information

Programming I. Course 9 Introduction to programming

Programming I. Course 9 Introduction to programming Programming I Course 9 Introduction to programming What we talked about? Modules List Comprehension Generators Recursive Functions Files What we talk today? Object Oriented Programming Classes Objects

More information

CS2021 Week #6. Tkinter structure. GUI Programming using Tkinter

CS2021 Week #6. Tkinter structure. GUI Programming using Tkinter CS2021 Week #6 GUI Programming using Tkinter Tkinter structure Requires integra>on with Tk a GUI library Python program makes widgets and registers func>ons to handle widget events Program consist of theses

More information

Thinking in Tkinter. Thinking in Tkinter. by Stephen Ferg ferg.org) revised:

Thinking in Tkinter. Thinking in Tkinter. by Stephen Ferg ferg.org) revised: 1 of 53 Thinking in Tkinter by Stephen Ferg (steve@ ferg.org) revised: 2005-07-17 This file contains the source code for all of the files in the Thinking in Tkinter series. If you print this file with

More information

CS 112: Intro to Comp Prog

CS 112: Intro to Comp Prog CS 112: Intro to Comp Prog Tkinter Layout Managers: place, pack, grid Custom Frames Widgets In-depth StringVar tkfont Upcoming Tk To use Tkinter Widgets (components: buttons, labels, etc). You must import

More information

Mid Unit Review. Of the four learning outcomes for this unit, we have covered the first two. 1.1 LO1 2.1 LO2 LO2

Mid Unit Review. Of the four learning outcomes for this unit, we have covered the first two. 1.1 LO1 2.1 LO2 LO2 Lecture 8 Mid Unit Review Of the four learning outcomes for this unit, we have covered the first two. LO Learning outcome (LO) AC Assessment criteria for pass The learner can: LO1 Understand the principles

More information

ECE 364 Software Engineering Tools Laboratory. Lecture 7 Python: Object Oriented Programming

ECE 364 Software Engineering Tools Laboratory. Lecture 7 Python: Object Oriented Programming ECE 364 Software Engineering Tools Laboratory Lecture 7 Python: Object Oriented Programming 1 Lecture Summary Object Oriented Programming Concepts Object Oriented Programming in Python 2 Object Oriented

More information

OBJECT ORIENTED PROGRAMMING

OBJECT ORIENTED PROGRAMMING OBJECT ORIENTED PROGRAMMING (download slides and.py files follow along!) 6.0001 LECTURE 8 6.0001 LECTURE 8 1 OBJECTS Python supports many different kinds of data 1234 3.14159 "Hello" [1, 5, 7, 11, 13]

More information

Data Structures (list, dictionary, tuples, sets, strings)

Data Structures (list, dictionary, tuples, sets, strings) Data Structures (list, dictionary, tuples, sets, strings) Lists are enclosed in brackets: l = [1, 2, "a"] (access by index, is mutable sequence) Tuples are enclosed in parentheses: t = (1, 2, "a") (access

More information

Introduction. We've seen Python useful for. This lecture discusses Object Oriented Programming. Simple scripts Module design

Introduction. We've seen Python useful for. This lecture discusses Object Oriented Programming. Simple scripts Module design Introduction We've seen Python useful for Simple scripts Module design This lecture discusses Object Oriented Programming Better program design Better modularization What is an object? An object is an

More information

CSE 341, Autumn 2015, Ruby Introduction Summary

CSE 341, Autumn 2015, Ruby Introduction Summary CSE 341, Autumn 2015, Ruby Introduction Summary Disclaimer: This lecture summary is not necessarily a complete substitute for atting class, reading the associated code, etc. It is designed to be a useful

More information

Graphical User Interfaces

Graphical User Interfaces to visualize Graphical User Interfaces 1 2 to visualize MCS 507 Lecture 12 Mathematical, Statistical and Scientific Software Jan Verschelde, 19 September 2011 Graphical User Interfaces to visualize 1 2

More information

Lecture 18. Classes and Types

Lecture 18. Classes and Types Lecture 18 Classes and Types Announcements for Today Reading Today: See reading online Tuesday: See reading online Prelim, Nov 6 th 7:30-9:30 Material up to next class Review posted next week Recursion

More information

Selected GUI elements:

Selected GUI elements: Selected GUI elements: Element tkinter Class Description Frame Frame Holds other GUI elements Label Label Displays uneditable text or icons Button Button Performs an action when the user activates it Text

More information

PTN-202: Advanced Python Programming Course Description. Course Outline

PTN-202: Advanced Python Programming Course Description. Course Outline PTN-202: Advanced Python Programming Course Description This 4-day course picks up where Python I leaves off, covering some topics in more detail, and adding many new ones, with a focus on enterprise development.

More information

Lecture 21. Programming with Subclasses

Lecture 21. Programming with Subclasses Lecture 21 Programming with Subclasses Announcements for Today Reading Today: See reading online Tuesday: Chapter 7 Prelim, Nov 10 th 7:30-9:00 Material up to Today Review has been posted Recursion + Loops

More information

Introduction to Programming Using Python Lecture 6. Dr. Zhang COSC 1437 Spring, 2018 March 01, 2018

Introduction to Programming Using Python Lecture 6. Dr. Zhang COSC 1437 Spring, 2018 March 01, 2018 Introduction to Programming Using Python Lecture 6 Dr. Zhang COSC 1437 Spring, 2018 March 01, 2018 Chapter 9 GUI Programming Using Tkinter Getting started with Tkinter with a simple example. Code example:

More information

Glossary. For Introduction to Programming Using Python By Y. Daniel Liang

Glossary. For Introduction to Programming Using Python By Y. Daniel Liang Chapter 1 Glossary For Introduction to Programming Using Python By Y. Daniel Liang.py Python script file extension name. assembler A software used to translate assemblylanguage programs into machine code.

More information

CIS192 Python Programming

CIS192 Python Programming CIS192 Python Programming Introduction Robert Rand University of Pennsylvania September 16, 2015 Robert Rand (University of Pennsylvania) CIS 192 September 16, 2015 1 / 21 Outline 1 Object Orientation

More information

Lecture 21. Programming with Subclasses

Lecture 21. Programming with Subclasses Lecture 21 Programming with Subclasses Announcements for This Lecture Assignments Prelim 2 A4 is now graded Mean: 90.4 Median: 93 Std Dev: 10.6 Mean: 9 hrs Median: 8 hrs Std Dev: 4.1 hrs A5 is also graded

More information

ENGR/CS 101 CS Session Lecture 15

ENGR/CS 101 CS Session Lecture 15 ENGR/CS 101 CS Session Lecture 15 Log into Windows/ACENET (reboot if in Linux) Use web browser to go to session webpage http://csserver.evansville.edu/~hwang/f14-courses/cs101.html Right-click on lecture15.py

More information

Babu Madhav Institute of Information Technology, UTU 2015

Babu Madhav Institute of Information Technology, UTU 2015 Five years Integrated M.Sc.(IT)(Semester 5) Question Bank 060010502:Programming in Python Unit-1:Introduction To Python Q-1 Answer the following Questions in short. 1. Which operator is used for slicing?

More information

CIS192 Python Programming

CIS192 Python Programming CIS192 Python Programming Object-Oriented Programming Robert Rand University of Pennsylvania February 10, 2016 Robert Rand (University of Pennsylvania) CIS 192 February 10, 2016 1 / 25 Outline 1 Object

More information

Java Programming Lecture 6

Java Programming Lecture 6 Java Programming Lecture 6 Alice E. Fischer Feb 15, 2013 Java Programming - L6... 1/32 Dialog Boxes Class Derivation The First Swing Programs: Snow and Moving The Second Swing Program: Smile Swing Components

More information

Object Model Comparisons

Object Model Comparisons Object Model Comparisons 1 Languages are designed, just like programs Someone decides what the language is for Someone decides what features it's going to have Can't really understand a language until

More information

Question Possible Points Earned Points Graded By GUI 22 SQL 24 XML 20 Multiple Choice 14 Total Points 80

Question Possible Points Earned Points Graded By GUI 22 SQL 24 XML 20 Multiple Choice 14 Total Points 80 CS 1803 Spring 2011 Exam 3 KEY Name: Section: Grading TA: Integrity: By taking this exam, you pledge that this is your work and you have neither given nor received inappropriate help during the taking

More information

CSE : Python Programming. Decorators. Announcements. The decorator pattern. The decorator pattern. The decorator pattern

CSE : Python Programming. Decorators. Announcements. The decorator pattern. The decorator pattern. The decorator pattern CSE 399-004: Python Programming Lecture 12: Decorators April 9, 200 http://www.seas.upenn.edu/~cse39904/ Announcements Projects (code and documentation) are due: April 20, 200 at pm There will be informal

More information

Lecturer: William W.Y. Hsu. Programming Languages

Lecturer: William W.Y. Hsu. Programming Languages Lecturer: William W.Y. Hsu Programming Languages Chapter 9 Data Abstraction and Object Orientation 3 Object-Oriented Programming Control or PROCESS abstraction is a very old idea (subroutines!), though

More information

Forth Meets Smalltalk. A Presentation to SVFIG October 23, 2010 by Douglas B. Hoffman

Forth Meets Smalltalk. A Presentation to SVFIG October 23, 2010 by Douglas B. Hoffman Forth Meets Smalltalk A Presentation to SVFIG October 23, 2010 by Douglas B. Hoffman 1 CONTENTS WHY FMS? NEON HERITAGE SMALLTALK HERITAGE TERMINOLOGY EXAMPLE FMS SYNTAX ACCESSING OVERRIDDEN METHODS THE

More information

Pace University. Fundamental Concepts of CS121 1

Pace University. Fundamental Concepts of CS121 1 Pace University Fundamental Concepts of CS121 1 Dr. Lixin Tao http://csis.pace.edu/~lixin Computer Science Department Pace University October 12, 2005 This document complements my tutorial Introduction

More information

COMP519 Web Programming Lecture 21: Python (Part 5) Handouts

COMP519 Web Programming Lecture 21: Python (Part 5) Handouts COMP519 Web Programming Lecture 21: Python (Part 5) Handouts Ullrich Hustadt Department of Computer Science School of Electrical Engineering, Electronics, and Computer Science University of Liverpool Functions

More information

Chapter 9 GUI Programming Using Tkinter. Copyright 2012 by Pearson Education, Inc. All Rights Reserved.

Chapter 9 GUI Programming Using Tkinter. Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 9 GUI Programming Using Tkinter 1 Motivations Tkinter is not only a useful tool for developing GUI projects, but also a valuable pedagogical tool for learning object-oriented programming. 2 Objectives

More information

Lecture 21. Programming with Subclasses

Lecture 21. Programming with Subclasses Lecture 21 Programming with Subclasses Announcements for Today Reading Today: See reading online Tuesday: Chapter 7 Prelim, Nov 9 th 7:30-9:00 Material up to Today Review has been posted Recursion + Loops

More information

CIS192 Python Programming

CIS192 Python Programming CIS192 Python Programming Graphical User Interfaces Robert Rand University of Pennsylvania November 19, 2015 Robert Rand (University of Pennsylvania) CIS 192 November 19, 2015 1 / 20 Outline 1 Graphical

More information

A GUI for DFT and Orthogonal DWT in Tkinter

A GUI for DFT and Orthogonal DWT in Tkinter A GUI for DFT and Orthogonal DWT in Tkinter Tariq Javid Ali, Pervez Akhtar, Muhammad Faris Hamdard Institute of Engineering & Technology Hamdard University Karachi-74600, Pakistan Email: {tariq.javid pervez.akhtar

More information

This text is used together with Mark Pilgrims book Dive Into Python 3 for the Arthead course Python Fundamentals.

This text is used together with Mark Pilgrims book Dive Into Python 3 for the Arthead course Python Fundamentals. 2 About this text This text is used together with Mark Pilgrims book Dive Into Python 3 for the Arthead course Python Fundamentals. The first part is written for Python 2 and at the end there is a section

More information

Teaching London Computing

Teaching London Computing Teaching London Computing A Level Computer Science Programming GUI in Python William Marsh School of Electronic Engineering and Computer Science Queen Mary University of London How the Session Works Outline

More information

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE PART A UNIT I 1. Differentiate object oriented programming from procedure oriented programming. 2. Define abstraction and encapsulation. 3. Differentiate

More information

User Interfaces. getting arguments of the command line a command line interface to store points fitting points with polyfit of numpy

User Interfaces. getting arguments of the command line a command line interface to store points fitting points with polyfit of numpy User Interfaces 1 Command Line Interfaces getting arguments of the command line a command line interface to store points fitting points with polyfit of numpy 2 Encapsulation by Object Oriented Programming

More information

Object Oriented Programming #10

Object Oriented Programming #10 Object Oriented Programming #10 Serdar ARITAN Biomechanics Research Group, Faculty of Sports Sciences, and Department of Computer Graphics Hacettepe University, Ankara, Turkey 1 Simple programming tasks

More information

PREPARING FOR PRELIM 2

PREPARING FOR PRELIM 2 PREPARING FOR PRELIM 2 CS 1110: FALL 2012 This handout explains what you have to know for the second prelim. There will be a review session with detailed examples to help you study. To prepare for the

More information

Al al-bayt University Prince Hussein Bin Abdullah College for Information Technology Computer Science Department

Al al-bayt University Prince Hussein Bin Abdullah College for Information Technology Computer Science Department Al al-bayt University Prince Hussein Bin Abdullah College for Information Technology Computer Science Department 0901212 Python Programming 1 st Semester 2014/2015 Course Catalog This course introduces

More information

CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Dan Grossman Winter 2013

CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Dan Grossman Winter 2013 CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP Dan Grossman Winter 2013 Ruby logistics Next two sections use the Ruby language http://www.ruby-lang.org/ Installation / basic usage

More information

5. In JAVA, is exception handling implicit or explicit or both. Explain with the help of example java programs. [16]

5. In JAVA, is exception handling implicit or explicit or both. Explain with the help of example java programs. [16] Code No: R05220402 Set No. 1 1. (a) java is freeform language. Comment (b) Describe in detail the steps involved in implementing a stand-alone program. (c) What are command line arguments? How are they

More information

1 Classes. 2 Exceptions. 3 Using Other Code. 4 Problems. Sandeep Sadanandan (TU, Munich) Python For Fine Programmers May 16, / 19

1 Classes. 2 Exceptions. 3 Using Other Code. 4 Problems. Sandeep Sadanandan (TU, Munich) Python For Fine Programmers May 16, / 19 1 Classes 2 Exceptions 3 Using Other Code 4 Problems Sandeep Sadanandan (TU, Munich) Python For Fine Programmers May 16, 2009 1 / 19 Start with an Example Python is object oriented Everything is an object

More information

Python Scripting for Computational Science

Python Scripting for Computational Science Hans Petter Langtangen Python Scripting for Computational Science Third Edition With 62 Figures Sprin ger Table of Contents 1 Introduction 1 1.1 Scripting versus Traditional Programming 1 1.1.1 Why Scripting

More information

CMSC201 Computer Science I for Majors

CMSC201 Computer Science I for Majors CMSC201 Computer Science I for Majors Lecture 16 Classes Prof. Jeremy Dixon Based on slides from http://www.csee.umbc.edu/courses/691p/notes/python/python3.ppt Last Class We Covered Review of Functions

More information

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers Lecture 27 Lecture 27: Regular Expressions and Python Identifiers Python Syntax Python syntax makes very few restrictions on the ways that we can name our variables, functions, and classes. Variables names

More information

Chapter 10 Introduction to Classes

Chapter 10 Introduction to Classes C++ for Engineers and Scientists Third Edition Chapter 10 Introduction to Classes CSc 10200! Introduction to Computing Lecture 20-21 Edgardo Molina Fall 2013 City College of New York 2 Objectives In this

More information

CMSC201 Computer Science I for Majors

CMSC201 Computer Science I for Majors CMSC201 Computer Science I for Majors Lecture 25 Classes All materials copyright UMBC and Dr. Katherine Gibson unless otherwise noted Run time Last Class We Covered Run time of different algorithms Selection,

More information

Class Design (Section 9 of 17)

Class Design (Section 9 of 17) the architects themselves came in to explain the advantages of both designs Class Design (Section 9 of 17) In the previous sections we have used existing classes that enable us to be able to write functions

More information

Object Oriented Programming

Object Oriented Programming Object Oriented Programming Haleh Ashki 2015, updated Peter Beerli 2017 Traditionally, a program has been seen as a recipe a set of instructions that you follow from start to finish in order to complete

More information

Python Scripting for Computational Science

Python Scripting for Computational Science Hans Petter Langtangen Python Scripting for Computational Science Third Edition With 62 Figures 43 Springer Table of Contents 1 Introduction... 1 1.1 Scripting versus Traditional Programming... 1 1.1.1

More information

Lecture 9: GUI and debugging

Lecture 9: GUI and debugging Lecture 9: GUI and debugging Introduction for Linguists (LT2102) Markus Forsberg Språkbanken University of Gothenburg October 12, 2010 Assignment 2 I have now corrected all assignments. If you have not

More information

What is a class? Responding to messages. Short answer 7/19/2017. Code Listing 11.1 First Class. chapter 11. Introduction to Classes

What is a class? Responding to messages. Short answer 7/19/2017. Code Listing 11.1 First Class. chapter 11. Introduction to Classes chapter 11 Code Listing 11.1 First Class Introduction to Classes What is a class? If you have done anything in computer science before, you likely will have heard the term object oriented programming (OOP)

More information

Lecture #15: Generic Functions and Expressivity. Last modified: Wed Mar 1 15:51: CS61A: Lecture #16 1

Lecture #15: Generic Functions and Expressivity. Last modified: Wed Mar 1 15:51: CS61A: Lecture #16 1 Lecture #15: Generic Functions and Expressivity Last modified: Wed Mar 1 15:51:48 2017 CS61A: Lecture #16 1 Consider the function find: Generic Programming def find(l, x, k): """Return the index in L of

More information

Java: introduction to object-oriented features

Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer

More information

Lesson 1 Python: Interactive Fiction

Lesson 1 Python: Interactive Fiction Lesson 1 Python: Interactive Fiction Introduction Interactive fiction is a story told with software. We will be using the programming language Python. The program will simulate a place and some actions,

More information

Table of Contents. Preface... xxi

Table of Contents. Preface... xxi Table of Contents Preface... xxi Chapter 1: Introduction to Python... 1 Python... 2 Features of Python... 3 Execution of a Python Program... 7 Viewing the Byte Code... 9 Flavors of Python... 10 Python

More information

Unit E Step-by-Step: Programming with Python

Unit E Step-by-Step: Programming with Python Unit E Step-by-Step: Programming with Python Computer Concepts 2016 ENHANCED EDITION 1 Unit Contents Section A: Hello World! Python Style Section B: The Wacky Word Game Section C: Build Your Own Calculator

More information

Lecture 02, Fall 2018 Friday September 7

Lecture 02, Fall 2018 Friday September 7 Anatomy of a class Oliver W. Layton CS231: Data Structures and Algorithms Lecture 02, Fall 2018 Friday September 7 Follow-up Python is also cross-platform. What s the advantage of Java? It s true: Python

More information

CIS192 Python Programming

CIS192 Python Programming CIS192 Python Programming User Interfaces (Graphical and Text) Eric Kutschera University of Pennsylvania March 20, 2015 Eric Kutschera (University of Pennsylvania) CIS 192 March 20, 2015 1 / 23 Final Project

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Get Unique study materials from

Get Unique study materials from Downloaded from www.rejinpaul.com VALLIAMMAI ENGNIEERING COLLEGE SRM Nagar, Kattankulathur 603203. DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING Year & Semester : IV Section : EEE - 1 & 2 Subject Code

More information

Review 2. Classes and Subclasses

Review 2. Classes and Subclasses Review 2 Classes and Subclasses Class Definition class (): """Class specification""" class variables (format: Class.variable) initializer ( init ) special method definitions

More information

CS 11 python track: lecture 4

CS 11 python track: lecture 4 CS 11 python track: lecture 4 Today: More odds and ends assertions "print >>" syntax more on argument lists functional programming tools list comprehensions More on exception handling More on object-oriented

More information

CME 193: Introduction to Scientific Python Lecture 6: Classes and iterators

CME 193: Introduction to Scientific Python Lecture 6: Classes and iterators CME 193: Introduction to Scientific Python Lecture 6: Classes and iterators Sven Schmit stanford.edu/~schmit/cme193 6: Classes and iterators 6-1 Contents Classes Generators and Iterators Exercises 6: Classes

More information

Getting Started p. 1 Obtaining Tcl/Tk p. 1 Interactive Execution p. 1 Direct Execution p. 4 Reading this Book p. 6 Requirements for Networking

Getting Started p. 1 Obtaining Tcl/Tk p. 1 Interactive Execution p. 1 Direct Execution p. 4 Reading this Book p. 6 Requirements for Networking Foreword p. xi Acknowledgments p. xiii Getting Started p. 1 Obtaining Tcl/Tk p. 1 Interactive Execution p. 1 Direct Execution p. 4 Reading this Book p. 6 Requirements for Networking Examples p. 7 Requirements

More information

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Python I. Some material adapted from Upenn cmpe391 slides and other sources Python I Some material adapted from Upenn cmpe391 slides and other sources Overview Names & Assignment Data types Sequences types: Lists, Tuples, and Strings Mutability Understanding Reference Semantics

More information

Tcl/Tk lecture. What is the Wish Interpreter? CIS 410/510 User Interface Programming

Tcl/Tk lecture. What is the Wish Interpreter? CIS 410/510 User Interface Programming Tcl/Tk lecture CIS 410/510 User Interface Programming Tool Command Language TCL Scripting language for developing & using GUIs Allows generic programming variables, loops, procedures Embeddable into an

More information

CS 2316 Exam 2 Summer 2011

CS 2316 Exam 2 Summer 2011 CS 2316 Exam 2 Summer 2011 Name : Grading TA: Integrity: By taking this exam, you pledge that this is your work and you have neither given nor received inappropriate help during the taking of this exam

More information

What are the characteristics of Object Oriented programming language?

What are the characteristics of Object Oriented programming language? What are the various elements of OOP? Following are the various elements of OOP:- Class:- A class is a collection of data and the various operations that can be performed on that data. Object- This is

More information

Project 1: Scheme Pretty-Printer

Project 1: Scheme Pretty-Printer Project 1: Scheme Pretty-Printer CSC 4101, Fall 2017 Due: 7 October 2017 For this programming assignment, you will implement a pretty-printer for a subset of Scheme in either C++ or Java. The code should

More information

Ruby logistics. CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Ruby: Not our focus. Ruby: Our focus. A note on the homework

Ruby logistics. CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Ruby: Not our focus. Ruby: Our focus. A note on the homework Ruby logistics CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP Dan Grossman Autumn 2018 Next two sections use the Ruby language http://www.ruby-lang.org/ Installation / basic usage

More information

This course is designed for anyone who needs to learn how to write programs in Python.

This course is designed for anyone who needs to learn how to write programs in Python. Python Programming COURSE OVERVIEW: This course introduces the student to the Python language. Upon completion of the course, the student will be able to write non-trivial Python programs dealing with

More information

Classes, objects, methods and properties

Classes, objects, methods and properties Learning Object 1. African Virtual University Template for extracting a learning object Main Learning Objective Nature of learning object Key concept (s) Source Module information Access source module

More information

Objects. say something to express one's disapproval of or disagreement with something.

Objects. say something to express one's disapproval of or disagreement with something. Objects say something to express one's disapproval of or disagreement with something. class Person: def init (self, name, age): self.name = name self.age = age p1 = Person("John", 36) class Person: def

More information

Table of Contents EVALUATION COPY

Table of Contents EVALUATION COPY Table of Contents Introduction... 1-2 A Brief History of Python... 1-3 Python Versions... 1-4 Installing Python... 1-5 Environment Variables... 1-6 Executing Python from the Command Line... 1-7 IDLE...

More information

Chapter 3 Classes. Activity The class as a file drawer of methods. Activity Referencing static methods

Chapter 3 Classes. Activity The class as a file drawer of methods. Activity Referencing static methods Chapter 3 Classes Lesson page 3-1. Classes Activity 3-1-1 The class as a file drawer of methods Question 1. The form of a class definition is: public class {

More information

Lecture 18 Tao Wang 1

Lecture 18 Tao Wang 1 Lecture 18 Tao Wang 1 Abstract Data Types in C++ (Classes) A procedural program consists of one or more algorithms that have been written in computerreadable language Input and display of program output

More information

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction Lecture 13: Object orientation Object oriented programming Introduction, types of OO languages Key concepts: Encapsulation, Inheritance, Dynamic binding & polymorphism Other design issues Smalltalk OO

More information

CS 11 python track: lecture 2

CS 11 python track: lecture 2 CS 11 python track: lecture 2 Today: Odds and ends Introduction to object-oriented programming Exception handling Odds and ends List slice notation Multiline strings Docstrings List slices (1) a = [1,

More information

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1 A First Look at ML Chapter Five Modern Programming Languages, 2nd ed. 1 ML Meta Language One of the more popular functional languages (which, admittedly, isn t saying much) Edinburgh, 1974, Robin Milner

More information

Computer Components. Software{ User Programs. Operating System. Hardware

Computer Components. Software{ User Programs. Operating System. Hardware Computer Components Software{ User Programs Operating System Hardware What are Programs? Programs provide instructions for computers Similar to giving directions to a person who is trying to get from point

More information

CS112 Lecture: Working with Numbers

CS112 Lecture: Working with Numbers CS112 Lecture: Working with Numbers Last revised January 30, 2008 Objectives: 1. To introduce arithmetic operators and expressions 2. To expand on accessor methods 3. To expand on variables, declarations

More information

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators 1 Professor: Sana Odeh odeh@courant.nyu.edu Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators Review What s wrong with this line of code? print( He said Hello ) What s wrong with

More information

CS-202 Introduction to Object Oriented Programming

CS-202 Introduction to Object Oriented Programming CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction

More information

COP 3330 Final Exam Review

COP 3330 Final Exam Review COP 3330 Final Exam Review I. The Basics (Chapters 2, 5, 6) a. comments b. identifiers, reserved words c. white space d. compilers vs. interpreters e. syntax, semantics f. errors i. syntax ii. run-time

More information