Lecture 3. Functions & Modules

Similar documents
Lecture 3. Functions & Modules

Not-So-Mini-Lecture 6. Modules & Scripts

Lecture 3: Functions & Modules

Lecture 3. Strings, Functions, & Modules

Lecture 3: Functions & Modules (Sections ) CS 1110 Introduction to Computing Using Python

Lecture 4. Defining Functions

Lecture 7. Memory in Python

CS1110 Lab 1 (Jan 27-28, 2015)

Lecture 9. Memory and Call Stacks

CS 1110, LAB 2: FUNCTIONS AND ASSIGNMENTS

Lecture 4. Defining Functions

Lecture 4. Defining Functions

PREPARING FOR PRELIM 1

CS 1110 SPRING 2016: GETTING STARTED (Jan 27-28) First Name: Last Name: NetID:

CSC 120 Computer Science for the Sciences. Week 1 Lecture 2. UofT St. George January 11, 2016

Lecture 4: Defining Functions

2. Modules, Scripts, and I/O. Quick Note on print. The Windchill Calculation. Script Mode. Motivating Script Mode 1/22/2016

2. Modules, Scripts, and I/O

Lecture 5. Defining Functions

Lecture 2. Variables & Assignment

CS 1110, LAB 3: MODULES AND TESTING First Name: Last Name: NetID:

Lecture 2: Variables & Assignments

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

CS 1110, LAB 2: ASSIGNMENTS AND STRINGS

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

Lecture 1. Course Overview Types & Expressions

CSE 115. Introduction to Computer Science I

How To Think Like A Computer Scientist, chapter 3; chapter 6, sections

CS1 Lecture 4 Jan. 23, 2019

Programming for Engineers in Python. Recitation 1

Copied from: on 3/20/2017

Downloaded from Chapter 2. Functions

Lecture 4: Defining Functions (Ch ) CS 1110 Introduction to Computing Using Python

CS1 Lecture 4 Jan. 24, 2018

CSE : Python Programming

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Semester 2, 2018: Lab 1

nostarch.com/pfk For bulk orders, please contact us at

Modules and Programs 1 / 14

Starting. Read: Chapter 1, Appendix B from textbook.

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

CS 1110, LAB 1: EXPRESSIONS AND ASSIGNMENTS First Name: Last Name: NetID:

Fundamentals: Expressions and Assignment

CSC148: Week 1

Python 1: Intro! Max Dougherty Andrew Schmitt

Lecture 1. Course Overview, Python Basics

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Ch.2: Loops and lists

CSC209. Software Tools and Systems Programming.

What you get When you install Python for your computer, you get a number of features:

Part III Appendices 165

ENGR (Socolofsky) Week 02 Python scripts

South Dakota Board of Regents

ECS Baruch Lab 5 Spring 2019 Name NetID (login, like , not your SUID)

Parent Student Portal User Guide. Version 3.1,

CS1110 Lab 6 (Mar 17-18, 2015)

Lecture 1. Course Overview, Python Basics

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

Python for Finance. Introduction and Basics of Python. Andras Niedermayer

Genome Sciences 373: Genome Informatics. Quiz section #1 March 29, 2018

CSCA20 Worksheet Strings

Lecture 3. Input, Output and Data Types

CSE : Python Programming

TTh 9.25 AM AM Strain 322

Week 1: Introduction to R, part 1

CSC209. Software Tools and Systems Programming.

Programming for Engineers in Python. Autumn

Table of Contents EVALUATION COPY

Lecture 1. Types, Expressions, & Variables

COMP 250 Winter 2011 Reading: Java background January 5, 2011

CS 3030 Scripting Languages Syllabus

Lab 1: Course Intro, Getting Started with Python IDLE. Ling 1330/2330 Computational Linguistics Na-Rae Han

Babes-Bolyai University

Computer and Programming: Lab 1

Introduction to: Computers & Programming: Review prior to 1 st Midterm

COMP 401 COURSE OVERVIEW

Functions and Decomposition

1.00/1.001 Tutorial 1

CS 141, Lecture 3. Please login to the Math/Programming profile, and look for IDLE (3.4 or the unnumbered. <-- fine <-- fine <-- broken

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Boot Camp. Dave Eckhardt Bruce Maggs

ECE 364 Software Engineering Tools Lab. Lecture 3 Python: Introduction

CMSC 201 Spring 2018 Lab 01 Hello World

CS 102 Lab 3 Fall 2012

Lecture 6: Specifications & Testing

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

Writing and Running Programs

CMSC 201 Spring 2017 Lab 01 Hello World

CS 1110, LAB 1: PYTHON EXPRESSIONS.

Lecture 13. For-Loops

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

EECS 1001 and EECS 1030M, lab 01 conflict

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

CSE 374: Programming Concepts and Tools. Eric Mullen Spring 2017 Lecture 4: More Shell Scripts

Python for Non-programmers

ENGG1811 Computing for Engineers Week 1 Introduction to Programming and Python

Get JAVA. I will just tell you what I did (on January 10, 2017). I went to:

Molecular Statistics Exercise 1. As was shown to you this morning, the interactive python shell can add, subtract, multiply and divide numbers.

Transcription:

Lecture 3 Functions & Modules

Labs this Week Lab 1 is due at the beginning of your lab If it is not yet by then, you cannot get credit Only exception is for students who added late (Those students should talk to me) Should spend time entirely on Lab 2 Getting behind this early is bad We are getting you ready for Assignment 1 8/29/17 Functions & Modules 2

(Optional) Readings Thursday Reading Chapter 3 in the text But can skip section 3.9 Browse the Python API Will learn what that is today Do not need to read all of it PLive: Activities 3-3.1, 3-3.2, 3-3.4 (not 3-3.3), 3-4.1, 3-4.2. (Old) Lecture on VideoNote Next Week Sections 8.1, 8.2, 8.5, 8.8 [xkcd.com] 8/29/17 Functions & Modules 3

Reminder: AEWs Small discussion study group for class Graded on attendance only Can really help if you have no experience Enrollment is extremely low right now Each section has room for up to 25 people Recommend it if you have the time Recommended to help with exams 8/31/17 Defining Functions 4

Function Calls Python supports expressions with math-like functions A function in an expression is a function call Will explain the meaning of this later Function expressions have the form fun(x,y, ) Examples (math functions that work in Python): round(2.34) max(a+3,24) function name argument 8/29/17 Functions & Modules 5

Function Calls Python supports expressions with math-like functions A function in an expression is a function call Will explain the meaning of this later Function expressions have the form fun(x,y, ) Examples (math functions that work in Python): round(2.34) max(a+3,24) Arguments can be any expression function name argument 8/29/17 Functions & Modules 6

Built-In Functions You have seen many functions already Type casting functions: int(), float(), bool() Dynamically type an expression: type() Help function: help() Quit function: quit() One of the most important functions is print() print(exp) displays value of exp on screen Will see later why this is important Arguments go in (), but name() refers to function in general 8/29/17 Functions & Modules 7

Built-in Functions vs Modules The number of built-in functions is small http://docs.python.org/3/library/functions.html Missing a lot of functions you would expect Example: cos(), sqrt() Module: file that contains Python code A way for Python to provide optional functions To access a module, the import command Access the functions using module as a prefix 8/29/17 Functions & Modules 8

Example: Module math >>> import math >>> math.cos(0) 1.0 >>> cos(0) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'cos' is not defined >>> math.pi 3.141592653589793 >>> math.cos(math.pi) -1.0 8/29/17 Functions & Modules 9

>>> import math >>> math.cos(0) 1.0 >>> cos(0) Traceback (most recent call last): Example: Module math File "<stdin>", line 1, in <module> NameError: name 'cos' is not defined >>> math.pi 3.141592653589793 >>> math.cos(math.pi) -1.0 To access math functions 8/29/17 Functions & Modules 10

>>> import math >>> math.cos(0) 1.0 >>> cos(0) Traceback (most recent call last): Example: Module math File "<stdin>", line 1, in <module> NameError: name 'cos' is not defined >>> math.pi 3.141592653589793 >>> math.cos(math.pi) -1.0 To access math functions Functions require math prefix! 8/29/17 Functions & Modules 11

>>> import math >>> math.cos(0) 1.0 >>> cos(0) Traceback (most recent call last): Example: Module math File "<stdin>", line 1, in <module> NameError: name 'cos' is not defined >>> math.pi 3.141592653589793 >>> math.cos(math.pi) -1.0 To access math functions Functions require math prefix! Module has variables too! 8/29/17 Functions & Modules 12

Example: Module math >>> import math >>> math.cos(0) 1.0 >>> cos(0) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'cos' is not defined >>> math.pi 3.141592653589793 >>> math.cos(math.pi) -1.0 To access math functions Functions require math prefix! Module has variables too! io Read/write from files random Generate random numbers Can pick any distribution string sys Other Modules Useful string functions Information about your OS 8/29/17 Functions & Modules 13

Reading the Python Documentation http://docs.python.org/library 8/29/17 Functions & Modules 14

Reading the Python Documentation http://docs.python.org/library 8/29/17 Functions & Modules 15

Reading the Python Documentation Function name Possible arguments Module What the function evaluates to http://docs.python.org/library 8/29/17 Functions & Modules 16

Interactive Shell vs. Modules Launch in command line Type each line separately Python executes as you type Write in a text editor We use Komodo Edit But anything will work Load module with import 8/29/17 Functions & Modules 17

Using a Module Module Contents """ A simple module. This file shows how modules work """ # This is a comment x = 1+2 x = 3*x x 8/29/17 Functions & Modules 18

Using a Module Module Contents """ A simple module. This file shows how modules work """ # This is a comment x = 1+2 x = 3*x x Single line comment (not executed) 8/29/17 Functions & Modules 19

Using a Module Module Contents """ A simple module. This file shows how modules work """ Docstring (note the Triple Quotes) Acts as a multiple-line comment Useful for code documentation # This is a comment x = 1+2 x = 3*x x Single line comment (not executed) 8/29/17 Functions & Modules 20

Using a Module Module Contents """ A simple module. This file shows how modules work """ Docstring (note the Triple Quotes) Acts as a multiple-line comment Useful for code documentation # This is a comment x = 1+2 x = 3*x x Commands Executed on import Single line comment (not executed) 8/29/17 Functions & Modules 21

Using a Module Module Contents """ A simple module. This file shows how modules work """ Docstring (note the Triple Quotes) Acts as a multiple-line comment Useful for code documentation # This is a comment x = 1+2 x = 3*x x Commands Executed on import Not a command. import ignores this Single line comment (not executed) 8/29/17 Functions & Modules 22

Using a Module Module Contents """ A simple module. This file shows how modules work """ Python Shell >>> import module >>> x # This is a comment x = 1+2 x = 3*x x 8/29/17 Functions & Modules 23

Using a Module Module Contents """ A simple module. This file shows how modules work """ # This is a comment x = 1+2 x = 3*x x Python Shell >>> import module >>> x Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'x' is not defined 8/29/17 Functions & Modules 24

Using a Module Module Contents """ A simple module. This file shows how modules work """ # This is a comment x = 1+2 x = 3*x x Module data must be prefixed by module name Python Shell >>> import module >>> x Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'x' is not defined >>> module.x 9 8/29/17 Functions & Modules 25

Using a Module Module Contents """ A simple module. This file shows how modules work """ # This is a comment x = 1+2 x = 3*x x Module data must be prefixed by module name Prints docstring and module contents Python Shell >>> import module >>> x Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'x' is not defined >>> module.x 9 >>> help(module) 8/29/17 Functions & Modules 26

Modules Must be in Working Directory! Module you want is in this folder 8/29/17 Functions & Modules 27

Modules Must be in Working Directory! Module you want is in this folder Have to navigate to folder BEFORE running Python 8/29/17 Functions & Modules 28

Using the from Keyword >>> import math >>> math.pi 3.141592653589793 >>> from math import pi >>> pi 3.141592653589793 >>> from math import * >>> cos(pi) -1.0 Must prefix with module name No prefix needed for variable pi No prefix needed for anything in math Be careful using from! Using import is safer Modules might conflict (functions w/ same name) What if import both? Example: Turtles Used in Assignment 4 2 modules: turtle, tkturtle Both have func. Turtle() 8/29/17 Functions & Modules 29

Modules vs. Scripts Module Provides functions, variables Example: temp.py import it into Python shell >>> import temp >>> temp.to_fahrenheit(100) 212.0 >>> Script Behaves like an application Example: helloapp.py Run it from command line: python helloapp.py 8/29/17 Functions & Modules 30

Modules vs. Scripts Module Provides functions, variables Example: temp.py import it into Python shell >>> import temp >>> temp.to_fahrenheit(100) 212.0 >>> Script Behaves like an application Example: helloapp.py Run it from command line: python helloapp.py Files look the same. Difference is how you use them. 8/29/17 Functions & Modules 31

Scripts and Print Statements module.py """ A simple module. script.py """ A simple script. This file shows how modules work """ This file shows why we use print """ # This is a comment x = 1+2 x = 3*x x # This is a comment x = 1+2 x = 3*x print(x) 8/29/17 Functions & Modules 32

Scripts and Print Statements module.py """ A simple module. script.py """ A simple script. This file shows how modules work """ This file shows why we use print """ # This is a comment x = 1+2 x = 3*x x Only difference # This is a comment x = 1+2 x = 3*x print(x) 8/29/17 Functions & Modules 33

Scripts and Print Statements module.py script.py Looks like nothing happens Python did the following: Executed the assignments Skipped the last line ( x is not a statement) We see something this time! Python did the following: Executed the assignments Executed the last line (Prints the contents of x) 8/29/17 Functions & Modules 34

Scripts and Print Statements module.py script.py Looks like nothing happens Python did the following: Executed the assignments Skipped the last line ( x is not a statement) We see something this time! Python did the following: Executed the assignments Executed the last line (Prints the contents of x) 8/29/17 Functions & Modules 35

Next Time: Defining Functions Function Call Command to do the function Can put it anywhere In the Python shell Inside another module Function Definition Command to do the function Belongs inside a module 8/29/17 Functions & Modules 36

Next Time: Defining Functions Function Call Command to do the function Can put it anywhere In the Python shell Inside another module Function Definition Command to do the function Belongs inside a module But only define function ONCE Can call as many times as you want 8/29/17 Functions & Modules 37

Next Time: Defining Functions Function Call Command to do the function Can put it anywhere In the Python shell Inside another module arguments inside () Function Definition Command to do the function Belongs inside a module But only define function ONCE Can call as many times as you want 8/29/17 Functions & Modules 38

Functions and Modules Purpose of modules is function definitions Function definitions are written in module file Import the module to call the functions Your Python workflow (right now) is 1. Write a function in a module (a.py file) 2. Open up the Terminal/Command Prompt 3. Move to the directory with this file 4. Start Python (type python) 5. Import the module 6. Try out the function 8/29/17 Functions & Modules 39