Using Modules.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Advertisements

COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Introduction to Python
Chapter 6: User-Defined Functions I
Functions and abstraction Michael Ernst UW CSE 190p Summer 2012.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
TOPIC 3 INTRODUCTION TO PROGRAMMING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
Functions and Modules CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Programming Training kiddo Main Points: - Python Statements - Problems with selections.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
Python Libraries Importing and Calling functions.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Hello, little turtles. Hello, little turtles! There are many modules in Python that provide very powerful feature that we can use in our own program.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
Functions Chapter 4 Python for Informatics: Exploring Information
Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Chapter 4 Python for Informatics: Exploring Information Slightly modified by Recep Kaya Göktaş on March 2015.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
JavaScript and Ajax (JavaScript Functions) Week 5 Web site:
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Fundamentals of Programming I Overview of Programming
Lesson 06: Functions Class Participation: Class Chat:
Chapter 9: Value-Returning Functions
Exam #1 You will have exactly 30 Mins to complete the exam.
Topic: Python’s building blocks -> Variables, Values, and Types
Topics Introduction to Functions Defining and Calling a Void Function
Introduction to Python
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Introduction to Python
Topic: Python’s building blocks -> Statements
COMPSCI 107 Computer Science Fundamentals
© 2010 David A Watt, University of Glasgow
Functions CIS 40 – Introduction to Programming in Python
Functions.
JavaScript: Functions.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
CS 100: Roadmap to Computing
Functions, Procedures, and Abstraction
CS 100: Roadmap to Computing
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Lesson 06: Functions Class Chat: Attendance: Participation
Python for Informatics: Exploring Information
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
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Terminal-Based Programs
COMPUTER PROGRAMMING SKILLS
What is a Function? Takes one or more arguments, or perhaps none at all Performs an operation Returns one or more values, or perhaps none at all Similar.
CISC101 Reminders Assignment 3 due today.
Functions Taken from notes by Dr. Neil Moore & Dr. Debby Keen
def-ining a function A function as an execution control structure
Functions, Procedures, and Abstraction
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Defining Functions.
Presentation transcript:

Using Modules

Today's Goal Introduce important concepts: Functions Classes Objects Methods Modules

Python Standard Library Python consists of two parts: Language Standard Library The Python Standard Library contains code you can use in your programs to do things like: Compute mathematical functions Perform network communication Access files Draw with turtles

Standard Library Organization Code in the Standard Library is organized into Modules A module is a collection of Variables Functions Classes Python Standard Library math pi e sin() cos() sqrt() random randrange() turtle class Turtle class Screen

Functions A function is a named list of statements that performs a task. You've used functions like: print() randrange() [in the random module] The library contains many functions. Today, you'll start learning to write your own. def add(x, y): sum = x + y return sum

Classes and Data Types Python has a few built-in data types. Can you recall some? These data types are defined by classes in the Python Standard Library. A class is Python's mechanism for defining data types. The classes in the Standard Library define data types that you can use in your programs.

Standard Library Documentation docs.python.org describes contents of Standard Library Access the correct version Navigate to Library Reference We'll explore it today.

Using Modules Use the import statement to make the contents of a module available to your program. After importing a module, use its functions and variables by prefixing them with the name of the module. import math radius = 3.5 area = math.pi * (radius ** 2) sqrtOf4 = math.sqrt(4)

Functions

Using Functions To use a function, you write a statement like this: print("Hello") or this: x = math.sqrt(3) We call this a function invocation or function call To execute this statement, the Python interpreter Locates the definition of the function Executes the statements in the function

Using Functions A function is like a miniature program: It needs input to operate on It performs some calculation using the input It produces a result

Function Input and Output Functions usually get their information using parameters supplied by your program A parameter is an expression used in a function call Parameters are also called arguments Functions often return a result that you store in a variable use as a parameter to another function Example: round() is a built-in function that rounds a float to the nearest int y = 3.4 x = round(y + .3) output stored in x input parameter

Function Evaluation To execute x = round(y + .3), Python Evaluates parameter expression y + .3 Invokes round() function, passing in value 3.7 round() executes, producing result (4) Stores the result in x y + .3 3.7 round() 4 x

Aside: print() is an atypical function It sends its output to the screen (most functions don't) It does not return a value to be used in an expression

Built-in Functions The Python Standard Library contains several "built-in" functions You don't have to import a module to use these:

Let's Practice Meet the len() function: Suppose we have a variable: name = "Fred" Write a statement to invoke the len() function to determine the length of name

Let's Survey Some Modules math module os module Variables pi e Functions fabs() sqrt() sin() Activity: Survey Library Documentation Variables name Functions getlogin() getcwd()

Function Documentation Function documentation specifies Required and optional parameters Return value

Caution About Googling Python has been around a long time. Many Internet pages present techniques for Python 2. We're using Python 3. Some things that work for Python 2 don't work in Python 3.

Classes and Objects

Classes, continued Data values in Python are called objects. The makeup and behavior of objects are defined by classes. We say that an object is an instance of a class. Each object can perform operations and methods defined by its class. A class defines attributes - one or more data values that each object contains methods - functions that the objects perform

Classes in Turtle Module The turtle module contains two classes: Turtle and Screen. Turtle attributes: Location Heading Shape Whether pen is up or down Pen color Turtle methods: You know these... Screen attributes: State of each pixel on the screen Screen methods: clear() screensize()

Using the Turtle Module import turtle # makes contents of turtle module available wn = turtle.Screen() # creates instance of Screen class in turtle module wn.title("Hello") # sets window title of Screen wn alex = turtle.Turtle() # creates instance of Turtle class in turtle module alex.forward(150) # invokes forward() method on alex instance

Creating Objects Create some objects, like numbers and strings, by writing literal values x = 10 s = "Hello" Create other objects by writing module-name.class-name() wn = turtle.Screen() alex = turtle.Turtle()

Using Objects Use objects by passing them to functions: y = math.fabs(x) size = len(s) And invoking their methods: wn.title("Hello") alex.forward(10) Activity: Explore using the Turtle methods in the Standard Library x = 10 s = "Hello" wn = turtle.Screen() alex = turtle.Turtle()

Instances and State The state of an object is the combined values of its attributes. Each object of a given class has its own distinct state. alex = turtle.Turtle() alex.forward(150) gerta = turtle.Turtle() gerta.forward(10)