S206E Lecture 21, 5/26/2016, Python classes

Size: px
Start display at page:

Download "S206E Lecture 21, 5/26/2016, Python classes"

Transcription

1 S206E057 Spring 2016 Copyright 2016, Chiu-Shui Chan. All Rights Reserved. Python has the notions of classes and objects from using dictionaries and modules to facilitate repetitious tasks. Information about the definition of modules, classes, and objects could be found online and manuals. Materials provided in this lecture are compiled partially from the following page: Major applications of modules and classes are to set up some definitions of variables for a design routine, then save them and used by or in other design projects. Here are brief explanations of these concepts. Modules are like dictionaries. A module is a Python file that (generally) has only definitions of variables, functions, and classes. Notions of constructing modules to make dictionaries have the following sequences. 1. You write a Python file with some functions or variables in it. 2. You import that file to a file in use. 3. And you can access the functions or variables in that module with the. (dot) operator. Exercise one: coding of a module example Following these notions, here is an example of module in Python. I have a module named mystuff.py, which will make a print (of A), define a function of apple, a variable of tangerine, print the tangerine variable (of B) and call the apple function to print (C). The image on the right shows the code in it and the results of execution line after line. Once these codes are written and saved as mystuff.py module, we can use this mystuff module with the built-in function of import from another file of callmystuff.py to access the apple function and the variable of tangerine in mystuff.py file. The codes of the callmystuff.py and its run results are shown on the lower right image. import mystuff mystuff.apple() In this call file example, when the module of mystuff.py is loaded to callmystuff.py, it will execute the entire mystuff.py file line by line as it runs itself until it is completed. Then call the apple function and print C, call and print the value of tangerine as B again. For correctly running modules, save the files first and reopen them for test. You can think about a module as a specialized dictionary or container that can store Python code and be accessed with the. (dot) operator. The following one is another shorter example that runs the similar program, but, coding parts are in short cuts by expert programmers. The file name of these two lines of coding is mystuff1.py. mystuff = {'apple' : 'I am apples.'} print mystuff ['apple'] S206E Lecture 21, 5/26/2016, Python classes Page 1 (5/8/2016)

2 Arch534 Spring 2015 In this file, mystuff is a variable, it has a key apple with contents of I am apples declared. Then I saved this file as mystuff1.py. Then I could print, from other Python scripts (i.e., callmystuff1.py), the item of apple inside the variable of mystuff. import mystuff1 print(mystuff1.mystuff['apple']) #Note: the pair of parenthesis after the print function has not been used before Python Python also has another construct that serves a similar purpose called a class. A class is a way to take a grouping of functions and data and place them inside a container so we could access them with the. (dot) operator. Classes are like modules: Python is called an object-oriented programming language. It is because that each program could be seen as an object used by others, which is the notion of object-oriented programming style. In Python, there is a class that lets you structure your program in a particular way. Using classes, we can add consistency to our programs so that they can be used systematically. Thus, classes are a good way of encapsulating functionality that can be kept together and passed around as a complete module for use in other projects. A class could be seen as a blueprint. It isn t something in itself, it just describes how to make something. We can create lots of objects from that blueprint known technically as instances. From the data base point of view, a class has the ability to create a data structure for containing various contents. Therefore, a class is something that just has structure, which defines how something should be laid out or structured, but doesn t have any object (or instance) yet. Then instances should be developed next. An instance is a specific copy of the class that contains all the information. A class is simply a description of what things should look like, what variables will be grouped together, and what functions can be called to manipulate those variables. Using this description, Python can create many instances (or objects) of the class, which can then be manipulated independently. Think of a class as a cookie cutter it is not a cookie itself, but it s a description of what a cookie looks like, and can be used to create many individual cookies. Similarly, it could be thought as a tax form. A particular tax form is a template that requires people to fill out. So, everybody has to fill out the same type of form, but the content that people put in to the form differs from person to person. A class is like the tax form or cookie cutter that specifies what content should be like. Your copy of the form with your specific information is like an instance of the class, which specifies what your content actually is. Thus, we can create objects, rather than simply procedures or variables. Classes are defined by the keyword class followed by a name, a pair of parenthesis, a colon, and definitions. A class template looks like this: # Defining a class class class_name: statement 1 statement n Exercise two: coding of a class example class Foo: def init (self, val): self.val = val def printval (self): print (self.val) Definition Parameter self keyword class name colon Class member variable Instance variable The class Foo creates a class and its functions are defined inside the class (shown by indentation). Following it, the init (self) creates an empty object with a specific initial state. This method is called class instantiation operation. The Foo class could generate many instances, and when it is called, it needs to know which instance it is working with. Therefore, Python Page 2 (5/8/2016)

3 S206E057 Spring 2016 will pass the value via the self parameter. Self is a variable for the instance (or called object) being accessed. In this example, when a value of val is provided by users, it will go through the definition window of init, to create an instance, and put the value to val that belongs to the instance. class Foo: def init (self, val): self.val = val def printval (self): print (self.val) obj1 = Foo("great") obj2 = Foo(200)edie obj1.printval() obj2.printval() In this example, after the class is defined, we create two instances of the class (or two independent objects), obj1 and obj2. When the obj1 is called, Python automatically calls the init function for us and passes the value of the string great through the variable declaration of val to its own instance member variable of val. The same thing happens for obj2. If we want to execute a class function for an instance, the statement must have the instance declared first and followed by the function name. Thus, if we want to apply the function of printval to print, the statement must have the format of obj1.printval(). Of course, the ClassFoo could also be called by other file of callclassfoo.py to implement the functions existing in ClassFoo. Yet, the execution sequences are to implement the class file first and run the call class file to two more prints at the end. See the image on the right. If objects are created in the class file, then they could be called and used in the call class file. If objects are created outside the class file, then use the from filename import* (import all items) method to call the class. This function will only run the needed items and functions, and it is not necessary to include the filename of the class when call the functions or variables. #import ClassFoo from ClassFoo import* obj1 = Foo("great") obj2 = Foo(200) obj1.printval() obj2.printval() Exercise 3: Example of a class on applying geometry 1. Defining a class named Shape first. #An example of a class class Shape: def init (self, x, y): self.x = x self.y = y self.description = "This shape has not been described yet" self.author = "Nobody has claimed to make this shape yet" def area(self): return self.x * self.y Page 3 (5/8/2016)

4 Arch534 Spring 2015 def perimeter(self): return 2 * self.x + 2 * self.y def describe(self, text): self.description = text return self.description def authorname(self, text): self.author = text def scalesize(self, scale): self.x = self.x * scale self.y = self.y * scale The function of init creates an instance of Shape when this function is called - that is, when we create an actual shape, as opposed to the 'blueprint' we have here, init is run. You will understand how this works later. The key word of self is how we refer to things in the class from within itself. Here, self is the first parameter in any function defined inside a class. Any function or variable created on the first level of indentation (that is, lines of code that start one TAB to the right of where we put class), Shape is automatically put into self. To access these functions and variables elsewhere inside the class, their name must be proceeded with self. In other words, function has its own variable scope, class has its scope as well. Variables could be defined inside the function without having self in front. But, after they are completed (function scope), they disappear. But, variables defined with self in front are active inside the class (class scope) and accessible across functions. Those variables defined in the init function part with self in front will be accessible across functions inside the same class, which are the conventional ways of Python coding. 2. Making a class from the class definition How do we use a class? Here is an example, of what we call creating an instance of a class. Assume that the code example on section 3-1 has already been run: rectangle = Shape(100, 45) What has been done? The init function really comes into play at this time. We create an instance of a class by first giving its name (in this case, Shape) and then, in brackets, the values to pass to the init function. The init function runs (using the parameters you gave it in brackets) and then spits out an instance of that class, which in this case is assigned to the name rectangle. Think of our class instance, rectangle, as a self-contained collection of variables and functions. In the same way that we used self to access functions and variables of the class instance from within itself, we use the name that we assigned to it now (rectangle) to access functions and variables of the class instance from outside of itself. Following on from the code we ran above, we would do this: 3. Accessing attributes from outside an instance print rectangle.area() #finding the area of your rectangle: print rectangle.perimeter() #finding the perimeter of your rectangle: #describing the rectangle rectangle.describe("a wide rectangle, more than twice\ as wide as it is tall") rectangle.scalesize(0.5) #making the rectangle 50% smaller print rectangle.area() #re-printing the new area of the rectangle As you see, where self would be used from within the class instance, its assigned name is used when outside the class. We do this to view and change the variables inside the class, and to access the functions that are there. We aren't limited to a single instance of a class - we could have as many instances as we like. We could do the following coding: Page 4 (5/8/2016)

5 S206E057 Spring More than one instance long_rectangle = Shape(120,10) fat_rectangle = Shape(130,120) Both long_rectangle and fat_rectangle have their own functions and variables contained inside them - they are totally independent of each other. There is no limit to the number of instances we could create. 5. Execution A: Completed execution codes are the following. This is the entire file of execution A. We could also separately save the class function to a file named: ShapeClass.py and put the remaining object instantiation plus the four print function-calls to another file named: CallShape.py. class Shape: def init (self, x, y): self.x = x self.y = y self.description = "This shape has not been described yet" self.author = "Nobody has claimed to make this shape yet" def area(self): return self.x * self.y def perimeter(self): return 2 * self.x + 2 * self.y def describe(self, text): self.description = text def authorname(self, text): self.author = text def scalesize(self, scale): self.x = self.x * scale self.y = self.y * scale rectangle = Shape(100,45) #initiate an object of rectangle long_rectangle = Shape(120,10) #initiate an object of long_rectangle fat_rectangle = Shape(130, 120) #initiate an object of fat_rectangle print(rectangle.area()) #utilze the area function in the class. print(rectangle.perimeter()) print(long_rectangle.area()) print(fat_rectangle.area()) Instantiate objects in this area with a number of properties. Definitions inside the class. Initiate objects. Rectangle is the name of the object, Shape is the class name, 100,45 are the values to be passed into the class of Shape. Use print to print the result of the object of rectangle by the function of area. Page 5 (5/8/2016)

6 Arch534 Spring Execution B: This is the portion of the codes saved on CallShape.py file, which will call the class definition defined on the file of ShapeClass.py class by the statement of from ShapeClass import*. The ShapeClass is the file name of the class to be imported. This form will import the needed item (Shape), or all of them (represented by * sign) to the file. The import all might be troublesome if there are objects in the program with the same name as some items in the module. A better way is to import a module in the normal way (without the from operator) and then assign items to a local name. from ShapeClass import* # Shapeclass is the class filename and import all the items here. rectangle = Shape(100,45) #initiate an object of rectangle long_rectangle = Shape(120,10) #initiate an object of long_rectangle fat_rectangle = Shape(130, 120) #initiate an object of fat_rectangle print(rectangle.area()) print(rectangle.perimeter()) print(long_rectangle.area()) print(fat_rectangle.area()) #utilize the function of area defined in the class. Note: Object-oriented programming has a set of lingo that is associated with it as listed below for your information. 1. When we first describe a class, we are defining it (like with functions). 2. The word 'class' can be used when describing the code where the class is defined (like how a function is defined), and it can also refer to an instance of that class - this can get confusing, so make sure you know in which form we are talking about classes. 3. In class, the functions are frequently referred to as method when defined within a class. 4. The ability to group similar functions and variables together is called encapsulation. 5. A variable inside a class is known as an Attribute. 6. A function inside a class is known as a method. 7. A class is in the same category of things as variables, lists, dictionaries, etc. That is, they are objects. 8. A class is known as a 'data structure' - it holds data, and the methods to process that data. Note on coding techniques and conventions: 1. Class names are usually capitalized, EachWordLikeThis, but this is only one of the Python coding conventions, not a requirement. 2. The second aspect is the values of variables inside a function. When a variable locates inside a function, it is a local variable and the value it carries only exists inside this function. 3. The third convention on coding is to control the flow of executing the codes. Sometimes, the pass statement in Python would be used. It is used when a statement is required syntactically but you do not want any command or code to execute, which is like an empty set of braces (), [], or {}. It is a null operation, nothing happens when it executes. It also is useful in places where your code will eventually go, but has not been written yet. Here is one example: for letter in 'python': if letter == 'h': pass #or (),[], {} print('this is pass block') print('current letter is:', letter) print("bye") 4. Fundamental methods of developing good coding skills are to make the coding shorter and more efficient. Logical thinking and systematic processing are the critical thinking style that you have to develop for developing computational codes for architectural designs. Page 6 (5/8/2016)

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 31 Static Members Welcome to Module 16 of Programming in C++.

More information

S206E Lecture 19, 5/24/2016, Python an overview

S206E Lecture 19, 5/24/2016, Python an overview S206E057 Spring 2016 Copyright 2016, Chiu-Shui Chan. All Rights Reserved. Global and local variables: differences between the two Global variable is usually declared at the start of the program, their

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

class objects instances Fields Constructors Methods static

class objects instances Fields Constructors Methods static Class Structure Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance variables)that hold the data for each object Constructors that

More information

Object Oriented Programming in Python 3

Object Oriented Programming in Python 3 Object Oriented Programming in Python 3 Objects Python 3 Objects play a central role in the Python data model All the types we ve seen until now are in-fact objects Numeric types, strings, lists, tuples,

More information

Week 8 Lecture: Programming

Week 8 Lecture: Programming Week 8 Lecture: Introduction to Object Oriented Programming Introduction to Programming for GIS & Remote Sensing GEO6938 1469 GEO4938 147A Objects: You ve Been Exposed! Modularization, you know about it

More information

PREPARING FOR THE FINAL EXAM

PREPARING FOR THE FINAL EXAM PREPARING FOR THE FINAL EXAM CS 1110: FALL 2017 This handout explains what you have to know for the final exam. Most of the exam will include topics from the previous two prelims. We have uploaded the

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

CS558 Programming Languages Winter 2013 Lecture 8

CS558 Programming Languages Winter 2013 Lecture 8 OBJECT-ORIENTED PROGRAMMING CS558 Programming Languages Winter 2013 Lecture 8 Object-oriented programs are structured in terms of objects: collections of variables ( fields ) and functions ( methods ).

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

A A B U n i v e r s i t y

A A B U n i v e r s i t y A A B U n i v e r s i t y Faculty of Computer Sciences O b j e c t O r i e n t e d P r o g r a m m i n g Week 4: Introduction to Classes and Objects Asst. Prof. Dr. M entor Hamiti mentor.hamiti@universitetiaab.com

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

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

3 ADT Implementation in Java

3 ADT Implementation in Java Object-Oriented Design Lecture 3 CS 3500 Spring 2010 (Pucella) Tuesday, Jan 19, 2010 3 ADT Implementation in Java Last time, we defined an ADT via a signature and a specification. We noted that the job

More information

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

CPS122 Lecture: From Python to Java last revised January 4, Objectives: Objectives: CPS122 Lecture: From Python to Java last revised January 4, 2017 1. To introduce the notion of a compiled language 2. To introduce the notions of data type and a statically typed language 3.

More information

Privacy and Security in Online Social Networks Department of Computer Science and Engineering Indian Institute of Technology, Madras

Privacy and Security in Online Social Networks Department of Computer Science and Engineering Indian Institute of Technology, Madras Privacy and Security in Online Social Networks Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 08 Tutorial 2, Part 2, Facebook API (Refer Slide Time: 00:12)

More information

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007 Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007 Java We will be programming in Java in this course. Partly because it is a reasonable language, and partly because you

More information

by Pearson Education, Inc. All Rights Reserved. 2

by Pearson Education, Inc. All Rights Reserved. 2 Suppose you want to drive a car and make it go faster by pressing down on its accelerator pedal. Before you can drive a car, someone has to design it and build it. A car typically begins as engineering

More information

DSC 201: Data Analysis & Visualization

DSC 201: Data Analysis & Visualization DSC 201: Data Analysis & Visualization Classes Dr. David Koop Tuple, List, Dictionary, or Set? [1,2,"abc"] 2 Tuple, List, Dictionary, or Set? {"a", 1, 2} 3 Tuple, List, Dictionary, or Set? {} 4 Tuple,

More information

JAVA: A Primer. By: Amrita Rajagopal

JAVA: A Primer. By: Amrita Rajagopal JAVA: A Primer By: Amrita Rajagopal 1 Some facts about JAVA JAVA is an Object Oriented Programming language (OOP) Everything in Java is an object application-- a Java program that executes independently

More information

Week - 01 Lecture - 04 Downloading and installing Python

Week - 01 Lecture - 04 Downloading and installing Python Programming, Data Structures and Algorithms in Python Prof. Madhavan Mukund Department of Computer Science and Engineering Indian Institute of Technology, Madras Week - 01 Lecture - 04 Downloading and

More information

S206E Lecture 22, 5/26/2016, Python and Rhino interface

S206E Lecture 22, 5/26/2016, Python and Rhino interface S206E057 Spring 2016 Copyright 2016, Chiu-Shui Chan. All Rights Reserved. Rhino and Python interface: An example How to run Rhino model by executing Python programs? And what kind of information is needed

More information

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014 Lesson 10A OOP Fundamentals By John B. Owen All rights reserved 2011, revised 2014 Table of Contents Objectives Definition Pointers vs containers Object vs primitives Constructors Methods Object class

More information

CPS122 Lecture: From Python to Java

CPS122 Lecture: From Python to Java Objectives: CPS122 Lecture: From Python to Java last revised January 7, 2013 1. To introduce the notion of a compiled language 2. To introduce the notions of data type and a statically typed language 3.

More information

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG 1 Notice Class Website http://www.cs.umb.edu/~jane/cs114/ Reading Assignment Chapter 1: Introduction to Java Programming

More information

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;

More information

Using Dreamweaver. 5 More Page Editing. Bulleted and Numbered Lists

Using Dreamweaver. 5 More Page Editing. Bulleted and Numbered Lists Using Dreamweaver 5 By now, you should have a functional template, with one simple page based on that template. For the remaining pages, we ll create each page based on the template and then save each

More information

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG 1 Notice Reading Assignment Chapter 1: Introduction to Java Programming Homework 1 It is due this coming Sunday

More information

Fundamental Concepts and Definitions

Fundamental Concepts and Definitions Fundamental Concepts and Definitions Identifier / Symbol / Name These terms are synonymous: they refer to the name given to a programming component. Classes, variables, functions, and methods are the most

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

Fundamentals of Programming (Python) Object-Oriented Programming. Ali Taheri Sharif University of Technology Spring 2018

Fundamentals of Programming (Python) Object-Oriented Programming. Ali Taheri Sharif University of Technology Spring 2018 Fundamentals of Programming (Python) Object-Oriented Programming Ali Taheri Sharif University of Technology Outline 1. Python Data Types 2. Classes and Objects 3. Defining Classes 4. Working with Objects

More information

CS 115 Lecture 21. Classes, data structures, and C++ Neil Moore

CS 115 Lecture 21. Classes, data structures, and C++ Neil Moore CS 115 Lecture 21 Classes, data structures, and C++ Neil Moore Department of Computer Science University of Kentucky Lexington, Kentucky 40506 neil@cs.uky.edu 3 December 2015 Files versus lists Differences

More information

egrapher Language Reference Manual

egrapher Language Reference Manual egrapher Language Reference Manual Long Long: ll3078@columbia.edu Xinli Jia: xj2191@columbia.edu Jiefu Ying: jy2799@columbia.edu Linnan Wang: lw2645@columbia.edu Darren Chen: dsc2155@columbia.edu 1. Introduction

More information

CPS122 Lecture: Defining a Class

CPS122 Lecture: Defining a Class Objectives: CPS122 Lecture: Defining a Class last revised January 14, 2016 1. To introduce structure of a Java class 2. To introduce the different kinds of Java variables (instance, class, parameter, local)

More information

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension. Java How to Program, 9/e Education, Inc. All Rights Reserved. } Covered in this chapter Classes Objects Methods Parameters double primitive type } Create a new class (GradeBook) } Use it to create an object.

More information

Introduction to Programming Style

Introduction to Programming Style Introduction to Programming Style Thaddeus Aid The IT Learning Programme The University of Oxford, UK 30 July, 2013 Abstract Programming style is the part of the program that the human reads and the compiler

More information

1 Getting used to Python

1 Getting used to Python 1 Getting used to Python We assume you know how to program in some language, but are new to Python. We'll use Java as an informal running comparative example. Here are what we think are the most important

More information

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes CS111: PROGRAMMING LANGUAGE II Lecture 1: Introduction to classes Lecture Contents 2 What is a class? Encapsulation Class basics: Data Methods Objects Defining and using a class In Java 3 Java is an object-oriented

More information

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda Functional abstraction Readings: HtDP, sections 19-24. Language level: Intermediate Student With Lambda different order used in lecture section 24 material introduced much earlier sections 22, 23 not covered

More information

Functional abstraction

Functional abstraction Functional abstraction Readings: HtDP, sections 19-24. Language level: Intermediate Student With Lambda different order used in lecture section 24 material introduced much earlier sections 22, 23 not covered

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

Relationship of class to object

Relationship of class to object Relationship of class to object Writing and programming Writing a program is similar to many other kinds of writing. The purpose of any kind of writing is to take your thoughts and let other people see

More information

Functions Structure and Parameters behind the scenes with diagrams!

Functions Structure and Parameters behind the scenes with diagrams! Functions Structure and Parameters behind the scenes with diagrams! Congratulations! You're now in week 2, diving deeper into programming and its essential components. In this case, we will talk about

More information

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet. Mr G s Java Jive #2: Yo! Our First Program With this handout you ll write your first program, which we ll call Yo. Programs, Classes, and Objects, Oh My! People regularly refer to Java as a language that

More information

Construction: High quality code for programming in the large

Construction: High quality code for programming in the large Construction: High quality code for programming in the large Paul Jackson School of Informatics University of Edinburgh What is high quality code? High quality code does what it is supposed to do......

More information

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 08 Constants and Inline Functions Welcome to module 6 of Programming

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

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

Section 1. How to use Brackets to develop JavaScript applications

Section 1. How to use Brackets to develop JavaScript applications Section 1 How to use Brackets to develop JavaScript applications This document is a free download from Murach books. It is especially designed for people who are using Murach s JavaScript and jquery, because

More information

Students Guide. Requirements of your homework

Students Guide. Requirements of your homework Students Guide Requirements of your homework During the SQL labs you should create SQL scripts, which correspond to the SQL script skeleton provided. In the case of the SQL1 lab, you should also hand in

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

Object Oriented Programming

Object Oriented Programming Classes and Objects Object Oriented Programming Represent self-contained things using classes. A class consists of: Data (stored in variables) Operations on that data (written as functions) Represent individual

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

Functions: Decomposition And Code Reuse

Functions: Decomposition And Code Reuse Programming: problem decomposition into functions 1 Functions: Decomposition And Code Reuse This section of notes shows you how to write functions that can be used to: decompose large problems, and to

More information

1. BASICS OF PYTHON. JHU Physics & Astronomy Python Workshop Lecturer: Mubdi Rahman

1. BASICS OF PYTHON. JHU Physics & Astronomy Python Workshop Lecturer: Mubdi Rahman 1. BASICS OF PYTHON JHU Physics & Astronomy Python Workshop 2017 Lecturer: Mubdi Rahman HOW IS THIS WORKSHOP GOING TO WORK? We will be going over all the basics you need to get started and get productive

More information

7. C++ Class and Object

7. C++ Class and Object 7. C++ Class and Object 7.1 Class: The classes are the most important feature of C++ that leads to Object Oriented programming. Class is a user defined data type, which holds its own data members and member

More information

Zipf's Law. This preliminary discussion gives us our first ideas about what the program we're writing needs to do.

Zipf's Law. This preliminary discussion gives us our first ideas about what the program we're writing needs to do. Zipf's Law In this unit, we'll be verifying the pattern of word frequencies known as Zipf's Law. Zipf's idea was pretty simple. Imagine taking a natural language corpus and making a list of all the words

More information

Working with Objects. Overview. This chapter covers. ! Overview! Properties and Fields! Initialization! Constructors! Assignment

Working with Objects. Overview. This chapter covers. ! Overview! Properties and Fields! Initialization! Constructors! Assignment 4 Working with Objects 41 This chapter covers! Overview! Properties and Fields! Initialization! Constructors! Assignment Overview When you look around yourself, in your office; your city; or even the world,

More information

Chapter 8: Creating Your Own Type Classes

Chapter 8: Creating Your Own Type Classes Chapter 8: Creating Your Own Type Classes What we will learn: Object-oriented programming What is a class How to create a class Assigning values to a class What you need to know before: Data types Methods

More information

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n) Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Module 10A Lecture - 20 What is a function?

More information

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science Language Design COMS W4115 Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science Language Design Issues Syntax: how programs look Names and reserved words Instruction

More information

CPS122 Lecture: Course Intro; Introduction to Object-Orientation

CPS122 Lecture: Course Intro; Introduction to Object-Orientation Objectives: CPS122 Lecture: Course Intro; Introduction to Object-Orientation 1. To introduce the course requirements and procedures. 2. To introduce fundamental concepts of OO: object, class Materials:

More information

Classes and Objects 1

Classes and Objects 1 Classes and Objects 1 Built-in objects You are already familiar with several kinds of objects: strings, lists, sets, tuples, and dictionaries An object has two aspects: Some fields (or instance variables)

More information

The Pyth Language. Administrivia

The Pyth Language. Administrivia Administrivia The Pyth Language Lecture 5 Please make sure you have registered your team, created SSH keys as indicated on the admin page, and also have electronically registered with us as well. Prof.

More information

6.009 Fundamentals of Programming

6.009 Fundamentals of Programming 6.009 Fundamentals of Programming Lecture 5: Custom Types Adam Hartz hz@mit.edu 6.009: Goals Our goals involve helping you develop as a programmer, in multiple aspects: Programming: Analyzing problems,

More information

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1) Technology & Information Management Instructor: Michael Kremer, Ph.D. Class 4 Professional Program: Data Administration and Management JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1) AGENDA

More information

Object-Oriented Programming in Processing

Object-Oriented Programming in Processing Object-Oriented Programming in Processing Object-Oriented Programming We ve (kinda) been doing this since Day 1: Python is a deeply object oriented language Most of the data types we were using (strings,

More information

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs. Lesson 2 VARIABLES Aim Understanding how computer programs store values, and how they are accessed and used in computer programs. WHAT ARE VARIABLES? When you input data (i.e. information) into a computer

More information

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes Based on Introduction to Java Programming, Y. Daniel Liang, Brief Version, 10/E 1 Creating Classes and Objects Classes give us a way of defining custom data types and associating data with operations on

More information

Notes from a Short Introductory Lecture on Scala (Based on Programming in Scala, 2nd Ed.)

Notes from a Short Introductory Lecture on Scala (Based on Programming in Scala, 2nd Ed.) Notes from a Short Introductory Lecture on Scala (Based on Programming in Scala, 2nd Ed.) David Haraburda January 30, 2013 1 Introduction Scala is a multi-paradigm language that runs on the JVM (is totally

More information

CSE 12 Abstract Syntax Trees

CSE 12 Abstract Syntax Trees CSE 12 Abstract Syntax Trees Compilers and Interpreters Parse Trees and Abstract Syntax Trees (AST's) Creating and Evaluating AST's The Table ADT and Symbol Tables 16 Using Algorithms and Data Structures

More information

OBJECT ORIENTED PROGRAMMING

OBJECT ORIENTED PROGRAMMING Classes and Objects So far you have explored the structure of a simple program that starts execution at main() and enables you to declare local and global variables and constants and branch your execution

More information

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation Review from Lectures 7 Algorithm Analysis, Formal Definition of Order Notation Simple recursion, Visualization

More information

Programming for Engineers in Python

Programming for Engineers in Python Programming for Engineers in Python Lecture 5: Object Oriented Programming Autumn 2011-12 1 Lecture 4 Highlights Tuples, Dictionaries Sorting Lists Modular programming Data analysis: text categorization

More information

What is a Class? Short Answer. Responding to Messages. Everything in Python is an Object 11/8/2010

What is a Class? Short Answer. Responding to Messages. Everything in Python is an Object 11/8/2010 The Practice of Computing Using PYTHON William Punch Richard Enbody Chapter 11 Introduction to Classes class Student(object): """Simple Student class.""" def init (self,first='', last='', id=0): # init

More information

Scala Style Guide Spring 2018

Scala Style Guide Spring 2018 CS18 Integrated Introduction to Computer Science Fisler, Nelson Scala Style Guide Spring 2018 Contents 1 Introduction 1 2 Naming 1 3 Formatting 2 4 Class Declarations 3 5 Functional Paradigms 4 6 Comments

More information

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW Prepared by: Dr. Abdallah Mohamed, AOU-KW 1 1. Introduction 2. Objects and classes 3. Information hiding 4. Constructors 5. Some examples of Java classes 6. Inheritance revisited 7. The class hierarchy

More information

(Refer Slide Time: 06:01)

(Refer Slide Time: 06:01) Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 28 Applications of DFS Today we are going to be talking about

More information

Final thoughts on functions F E B 2 5 T H

Final thoughts on functions F E B 2 5 T H Final thoughts on functions F E B 2 5 T H Ordering functions in your code Will the following code work? Here the function is defined after the main program that is calling it. print foo() def foo(): return

More information

CPS122 Lecture: Introduction to Java

CPS122 Lecture: Introduction to Java CPS122 Lecture: Introduction to Java last revised 10/5/10 Objectives: 1. To introduce the notion of a compiled language 2. To introduce the notions of data type and a statically typed language 3. To introduce

More information

C++ Style Guide. 1.0 General. 2.0 Visual Layout. 3.0 Indentation and Whitespace

C++ Style Guide. 1.0 General. 2.0 Visual Layout. 3.0 Indentation and Whitespace C++ Style Guide 1.0 General The purpose of the style guide is not to restrict your programming, but rather to establish a consistent format for your programs. This will help you debug and maintain your

More information

Lessons on Python Classes and Objects

Lessons on Python Classes and Objects Lessons on Python Classes and Objects Walter Didimo [ 120 minutes ] Outline We will introduce basic concepts about classes and objects in Python a comprehensive lesson on this topic would require much

More information

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Dr. Marenglen Biba Laboratory Session: Exercises on classes Analogy to help you understand classes and their contents. Suppose you want to drive a car and make it go faster by pressing down

More information

Introduction to Python (All the Basic Stuff)

Introduction to Python (All the Basic Stuff) Introduction to Python (All the Basic Stuff) 1 Learning Objectives Python program development Command line, IDEs, file editing Language fundamentals Types & variables Expressions I/O Control flow Functions

More information

Example: Fibonacci Numbers

Example: Fibonacci Numbers Example: Fibonacci Numbers Write a program which determines F n, the (n + 1)-th Fibonacci number. The first 10 Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34. The sequence of Fibonacci numbers

More information

CS/ENGRD 2110 SPRING Lecture 3: Fields, getters and setters, constructors, testing

CS/ENGRD 2110 SPRING Lecture 3: Fields, getters and setters, constructors, testing 1 CS/ENGRD 2110 SPRING 2019 Lecture 3: Fields, getters and setters, constructors, testing http://courses.cs.cornell.edu/cs2110 CS2110 Announcements 2 Take course S/U? OK with us. Check with your advisor/major.

More information

This course supports the assessment for Scripting and Programming Applications. The course covers 4 competencies and represents 4 competency units.

This course supports the assessment for Scripting and Programming Applications. The course covers 4 competencies and represents 4 competency units. This course supports the assessment for Scripting and Programming Applications. The course covers 4 competencies and represents 4 competency units. Introduction Overview Advancements in technology are

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

MITOCW watch?v=kz7jjltq9r4

MITOCW watch?v=kz7jjltq9r4 MITOCW watch?v=kz7jjltq9r4 PROFESSOR: We're going to look at the most fundamental of all mathematical data types, namely sets, and let's begin with the definitions. So informally, a set is a collection

More information

Class 9: Static Methods and Data Members

Class 9: Static Methods and Data Members Introduction to Computation and Problem Solving Class 9: Static Methods and Data Members Prof. Steven R. Lerman and Dr. V. Judson Harward Goals This the session in which we explain what static means. You

More information

OBJECTS. An object is an entity around us, perceivable through our senses. Types of Object: Objects that operate independently.

OBJECTS. An object is an entity around us, perceivable through our senses. Types of Object: Objects that operate independently. OBJECTS An object is an entity around us, perceivable through our senses. Types of Object: Objects that operate independently. Objects that work in associations with each others. Objects that frequently

More information

CSE 374 Programming Concepts & Tools

CSE 374 Programming Concepts & Tools CSE 374 Programming Concepts & Tools Hal Perkins Fall 2017 Lecture 8 C: Miscellanea Control, Declarations, Preprocessor, printf/scanf 1 The story so far The low-level execution model of a process (one

More information

This lecture presents ordered lists. An ordered list is one which is maintained in some predefined order, such as alphabetical or numerical order.

This lecture presents ordered lists. An ordered list is one which is maintained in some predefined order, such as alphabetical or numerical order. 6.1 6.2 This lecture presents ordered lists. An ordered list is one which is maintained in some predefined order, such as alphabetical or numerical order. A list is numerically ordered if, for every item

More information

Key Differences Between Python and Java

Key Differences Between Python and Java Python Python supports many (but not all) aspects of object-oriented programming; but it is possible to write a Python program without making any use of OO concepts. Python is designed to be used interpretively.

More information

DSC 201: Data Analysis & Visualization

DSC 201: Data Analysis & Visualization DSC 201: Data Analysis & Visualization Arrays Dr. David Koop Class Example class Rectangle: def init (self, x, y, w, h): self.x = x self.y = y self.w = w self.h = h def set_corner(self, x, y): self.x =

More information

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture 04 Programs with IO and Loop We will now discuss the module 2,

More information

Object Oriented Programming

Object Oriented Programming Object Oriented Programming Objectives To review the concepts and terminology of object-oriented programming To discuss some features of objectoriented design 1-2 Review: Objects In Java and other Object-Oriented

More information

Assignment Marking Criteria

Assignment Marking Criteria Assignment Marking Criteria Analysis Your analysis documentation must meet the following criteria: All program inputs, processing, and outputs. Each input and output must be given a name and description

More information

And Even More and More C++ Fundamentals of Computer Science

And Even More and More C++ Fundamentals of Computer Science And Even More and More C++ Fundamentals of Computer Science Outline C++ Classes Special Members Friendship Classes are an expanded version of data structures (structs) Like structs, the hold data members

More information

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology.

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology. Guide to and Hi everybody! In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology. This guide focuses on two of those symbols: and. These symbols represent concepts

More information