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

Modules and Objects Introduction to Computing Science and Programming I.
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
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
CHAPTER 4: INTRODUCTION TO COMPUTER ORGANIZATION AND PROGRAMMING DESIGN Lec. Ghader Kurdi.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CS0004: Introduction to Programming Variables – Numbers.
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.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Java Programming, Second Edition Chapter One Creating Your First Java Program.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
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.
Working With Objects Tonga Institute of Higher Education.
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.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
HIGH-LEVEL LANGUAGE PROGRAMMING PARADIGMS. Programming languages come in many forms or 'paradigms'. Each form of language offers advantages over other.
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.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
CSCI120 Introduction to Computer Science I using Python 3
Greenfoot.
Fundamentals of Programming I Overview of Programming
Chapter 2 Basic Computation
Topics Introduction Hardware and Software How Computers Store Data
Introduction to Python
Object Oriented Programming
Topic: Python’s building blocks -> Variables, Values, and Types
Design & Technology Grade 7 Python
Primitive Data, Variables, Loops (Maybe)
A451 Theory – 7 Programming 7A, B - Algorithms.
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
Introduction CSE 1310 – Introduction to Computers and Programming
Object Oriented Programming
TRANSLATORS AND IDEs Key Revision Points.
CS 100: Roadmap to Computing
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Learning to Program in Python
CompSci 101 Introduction to Computer Science
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Building Java Programs Chapter 2
Introduction to Python
Number and String Operations
Functions, Procedures, and Abstraction
Lesson 1: Fundamentals of Programming
CS 100: Roadmap to Computing
Java Programming Arrays
Topics Introduction Hardware and Software How Computers Store Data
Building Java Programs
Last Class We Covered What makes “good code” good Top down design
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
15-110: Principles of Computing
Introduction to Primitives
Introduction to Primitives
Data Types and Maths Programming Guides.
Functions, Procedures, and Abstraction
String Objects & its Methods
Working with dates and times
Presentation transcript:

Introduction to Computing Science and Programming I 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. 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.

Modules 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.

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. 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. 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.

Importing Modules If you only want to import a single function from with the module you can use this syntax from modulename import function 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 * from modulename import *

Documentation Online Documentation 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.

Documentation The help function 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. 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. 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. 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. 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 A real-world analogy. A DVD Player as an object Class – DVD Player 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 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

Objects 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) 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

Objects 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.

Objects 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 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 today.day today.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

Objects Classes may define how to be used with mathematical operators. 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.