Modules and Objects Introduction to Computing Science and Programming I.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Advertisements

CS 111: Introduction to Programming Midterm Exam NAME _________________ UIN __________________ 10/30/08 1.Who is our hero? 2.Why is this person our hero?
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Introduction to Python
Programming Introduction November 9 Unit 7. What is Programming? Besides being a huge industry? Programming is the process used to write computer programs.
1 Flash Actionscript Actionscript and Objects. 2 Actionscript and Objects ActionScript is what's known as an object-oriented programming language. Object-oriented.
Introduction to a Programming Environment
9-Aug-15 Vocabulary. Programming Vocabulary Watch closely, you might even want to take some notes. There’s a short quiz at the end of this presentation!
Activity 1 - WBs 5 mins Go online and spend a moment trying to find out the difference between: HIGH LEVEL programming languages and LOW LEVEL programming.
Python quick start guide
Computer Science 101 Introduction to Programming.
CHAPTER 4: INTRODUCTION TO COMPUTER ORGANIZATION AND PROGRAMMING DESIGN Lec. Ghader Kurdi.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Introduction CSE 1310 – Introduction to Computers and Programming
CS0004: Introduction to Programming Variables – Numbers.
Introduction to Python
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Computer Science 101 Introduction to Programming.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Introduction CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
JavaScript, Fourth Edition
Python – Part 1 Python Programming Language 1. What is Python? High-level language Interpreted – easy to test and use interactively Object-oriented Open-source.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
Java Programming, Second Edition Chapter One Creating Your First Java Program.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
Classes and Objects The basics. Object-oriented programming Python is an object-oriented programming language, which means that it provides features that.
Robert Crawford, MBA West Middle School.  Explain how the binary system is used by computers.  Describe how software is written and translated  Summarize.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
CSC1030 HANDS-ON INTRODUCTION TO JAVA Loop, Primitive type & Object basics.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming.
I Power Higher Computing Software Development Development Languages and Environments.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Working With Objects Tonga Institute of Higher Education.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
Introduction CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
OCR A Level F453: The function and purpose of translators Translators a. describe the need for, and use of, translators to convert source code.
Chapter 1: Introduction to Computers and Programming.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Introduction to Computing Science and Programming I
Fundamentals of Programming I Overview of Programming
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
Object Oriented Programming
TRANSLATORS AND IDEs Key Revision Points.
Learning to Program in Python
Building Java Programs Chapter 2
Functions, Procedures, and Abstraction
Topics Introduction Hardware and Software How Computers Store Data
Introduction to Programming with Python
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
Functions, Procedures, and Abstraction
String Objects & its Methods
Presentation transcript:

Modules and Objects Introduction to Computing Science and Programming I

Modules We already know about several of Python’s built- in functions that are available for use at any point in any program. We already know about several of Python’s built- in functions that are available for use at any point in any program. int(), str(), round(), range(), etc. int(), str(), round(), range(), etc. Python provides a much larger library of useful functions. However, if Python made all of these available at all times speed would become an issue as Python would have to load the information on every function every time a program was run. Python provides a much larger library of useful functions. However, if Python made all of these available at all times speed would become an issue as Python would have to load the information on every function every time a program was run.

Modules These functions are organized into what are called modules that each contain a set of functions that are related. These functions are organized into what are called modules that each contain a set of functions that are related. For example there is a module called ‘math’ that contains a number of additional mathematical functions. For example there is a module called ‘math’ that contains a number of additional mathematical functions.

Importing Modules Before you can use the functions in a module you need to use an import statement that tells Python to read the module into memory. Before you can use the functions in a module you need to use an import statement that tells Python to read the module into memory. After importing the module, you access its functions by using the syntax modulename.function. After importing the module, you access its functions by using the syntax modulename.function.

Importing Modules Here’s a short program that uses the math module. Here’s a short program that uses the math module. import math angle = float(raw_input(“Enter an angle in radians: “)) print “The sine of the angle is“, print math.sin(angle) Note the import statement at the beginning of the program. For simplicity, you should place your import statements at the start of your programs. Note the import statement at the beginning of the program. For simplicity, you should place your import statements at the start of your programs.

Importing Modules If you only want to import a single function from with the module you can use this syntax If you only want to import a single function from with the module you can use this syntax from modulename import function from modulename import function This allows you to call the function without using the module’s name every time This allows you to call the function without using the module’s name every time from math import sin angle = float(raw_input(“Enter an angle in radians: “)) print “The sine of the angle is“, print sin(angle) You can also import all of the functions for use like this by using the * You can also import all of the functions for use like this by using the * from modulename import * from modulename import *

Documentation Online Documentation Online Documentation There is online documentation for all of Python’s modules. There is online documentation for all of Python’s modules. It isn’t always easy to understand, but you will become used to the descriptions with practice. It isn’t always easy to understand, but you will become used to the descriptions with practice.

Documentation The help function The help function You can access a lot of the documentation from within the Python interpreter. You can access a lot of the documentation from within the Python interpreter. Just type in help(name) where name is the name of a module or function that you want to see the documentation for. Just type in help(name) where name is the name of a module or function that you want to see the documentation for. If after Python has read a function you’ve defined and stored it, you can use the help function on it. Notice that the help you see is primarily the docstring you wrote for the function. If after Python has read a function you’ve defined and stored it, you can use the help function on it. Notice that the help you see is primarily the docstring you wrote for the function.

Objects So far while programming we’ve just been dealing with simple pieces of information such as strings, Booleans, or numbers. So far while programming we’ve just been dealing with simple pieces of information such as strings, Booleans, or numbers. Complex information and functions that can be used on them are available in objects. Complex information and functions that can be used on them are available in objects.

Objects We aren’t going to spend a great deal of time with objects in this course. However, if you continue on you will likely spend a lot of time with them. We aren’t going to spend a great deal of time with objects in this course. However, if you continue on you will likely spend a lot of time with them. Object-Oriented Programming is a powerful tool that languages such as Python, C++, and Java allow you to take advantage of. Object-Oriented Programming is a powerful tool that languages such as Python, C++, and Java allow you to take advantage of.

Objects There are different types of objects. Each type of object is known as a class. There are different types of objects. Each type of object is known as a class. Objects have properties that give you basic information on the object. Objects have properties that give you basic information on the object. Objects have methods that can be invoked to have the object perform some sort of task. Objects have methods that can be invoked to have the object perform some sort of task.

Objects A real-world analogy. A real-world analogy. A DVD Player as an object A DVD Player as an object Class – DVD Player Class – DVD Player Properties – Information displayed on the DVD player such as the time within the movie being played Properties – Information displayed on the DVD player such as the time within the movie being played You can change the values of properties just like variables You can change the values of properties just like variables Methods – The buttons on the DVD player give you access to methods such as Play, Pause, etc. Methods – The buttons on the DVD player give you access to methods such as Play, Pause, etc. Each occurrence of a class is known as an instance of that class Each occurrence of a class is known as an instance of that class

Objects Objects in Python Objects in Python Classes are defined in many of Python’s modules and can be written by the programmer. (though we won’t be in this course) Classes are defined in many of Python’s modules and can be written by the programmer. (though we won’t be in this course) When you need an object of a certain type, you instantiate it (create an instance of it) When you need an object of a certain type, you instantiate it (create an instance of it) To instantiate an object, you call the constructor, a special method that each class defines To instantiate an object, you call the constructor, a special method that each class defines

Objects The datetime module contains a class called date that holds information about a particular day The datetime module contains a class called date that holds information about a particular day The constructor for a class has the same name as the class and objects are stored with variables, just like simple data types. The constructor for a class has the same name as the class and objects are stored with variables, just like simple data types.

Objects To create a date object you use code like this To create a date object you use code like this import datetime today = datetime.date(2007,6,11) today.strftime(“%B %d, %Y”) As with accessing functions in modules, you use the dot operator to access methods from an instance of a class As with accessing functions in modules, you use the dot operator to access methods from an instance of a class Like the time module which we looked at in class and is mentioned in the study guide, datetime has strftime() available Like the time module which we looked at in class and is mentioned in the study guide, datetime has strftime() available

Objects The date object has the properties year, month, and day which can also be accessed using the dot operator The date object has the properties year, month, and day which can also be accessed using the dot operatortoday.daytoday.year Depending on how a class was written you may be able to assign values to properties as with variables, but this is not true of the date class Depending on how a class was written you may be able to assign values to properties as with variables, but this is not true of the date class

Objects Classes may define how to be used with mathematical operators. Classes may define how to be used with mathematical operators. date cannot be added, but can be subtracted date cannot be added, but can be subtracted import datetime today = datetime.date(2007,6,11) yesterday = datetime.date(2007,6,10) print today-yesterday #works print today+yesterday #causes an error

Objects The date class also defines how it can be converted into a string. This allows it to be used with the print command. Not all classes will have this defined. The date class also defines how it can be converted into a string. This allows it to be used with the print command. Not all classes will have this defined.