Modules and Programs 1 / 14

Similar documents
CPSC 217 Midterm (Python 3 version)

Lecture 3. Functions & Modules

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

Lecture 3. Functions & Modules

Lecture 3: Functions & Modules

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

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

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

One of the hardest things you have to do is to keep track of three kinds of commands when writing and running computer programs. Those commands are:

LECTURE 2. Python Basics

CSE : Python Programming

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

Assignment 1: Communicating with Programs

Python for Non-programmers

Introduction to Python

CS150 - Sample Final

Table of Contents EVALUATION COPY

Reading and Writing Files on Your Computer

CS150 Sample Final. Name: Section: A / B

CS Programming Languages: Python

CS150 Sample Final Solution

9.2 Linux Essentials Exam Objectives

roboturtle Documentation

Introduction to Programming in Python (2)

CSE : Python Programming. Packages (Tutorial, Section 6.4) Announcements. Today. Packages: Concretely. Packages: Overview

Python Tutorial. Day 2

Com S 127 Lab 2. For the first two parts of the lab, start up Wing 101 and use the Python shell window to try everything out.

The Standard Library

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

PYTHON FOR KIDS A Pl ayfu l I ntrodu ctio n to Prog r am m i ng J a s o n R. B r i g g s

A function is a way to turn a bunch of related statements into a single "chunk"

Python 1: Intro! Max Dougherty Andrew Schmitt

Python Programming Exercises 1

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

Week 1: Introduction to R, part 1

Abstracting with Functions

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Using IDLE for

Compiling Software on UNIX. System Administration Decal Spring 2009 Lecture #4 George Wu Slides prepared by Joshua Kwan

CS1 Lecture 4 Jan. 24, 2018

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

I/O and Shell Scripting

Introduction to Python

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Introduction to MATLAB

Idioms and Anti-Idioms in Python

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

CS1 Lecture 4 Jan. 23, 2019

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

Computer Science AP/X CSCI-140/242 Polygons Lab 2 8/30/2018

Scientific Computing: Lecture 1

Programming in Python Advanced

Bash command shell language interpreter

Modules and scoping rules

61A Lecture 2. Friday, August 28, 2015

Chris Simpkins (Georgia Tech) CS 2316 Data Manipulation for Engineers Python Overview 1 / 9

Lecture 3. Strings, Functions, & Modules

Programming to Python

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Get It Interpreter Scripts Arrays. Basic Python. K. Cooper 1. 1 Department of Mathematics. Washington State University. Basics

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 4 Shell Variables, More Shell Scripts (Thanks to Hal Perkins)

24 Writing Your First Script

SCHEME AND CALCULATOR 5b

More Raspian. An editor Configuration files Shell scripts Shell variables System admin

CS1 Lecture 3 Jan. 22, 2018

A shell can be used in one of two ways:

Operating System Interaction via bash

B a s h s c r i p t i n g

PROGRAMMING FUNDAMENTALS

Introduction to Python

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

Downloaded from Chapter 2. Functions

STSCI Python Introduction. Class URL

Principles of Bioinformatics. BIO540/STA569/CSI660 Fall 2010

Lab 2: Booleans, Strings, Random Numbers, Recursion, Variables, Input function

CS Advanced Unix Tools & Scripting

Scripting With Jython

TECH 4272 Operating Systems

Lab 2: Booleans, Strings, Random Numbers, Recursion, Variables, Input function

NOTE: All references to Python on this exam mean Python 3, so you should answer accordingly.

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Remote Access to Unix Machines

Testing. UW CSE 160 Winter 2016

Programming for Engineers in Python. Recitation 3 Functions

Lab 5: File I/O CSE/IT 107. NMT Computer Science

Problem 1.1 (3 pts) :Python uses atomic data types and builds up from there. Give an example of: a. an int b. a double c. a string

Linux Software Installation. Qi Sun Bioinformatics Facility

Computational Physics. User Defined Modules

Computer Lab 1: Introduction to Python

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

Modules and Packages. CS 339R (Python) Chapter 8

ENCM 339 Fall 2017: Editing and Running Programs in the Lab

Git. Charles J. Geyer School of Statistics University of Minnesota. Stat 8054 Lecture Notes

Today. Review. Unix as an OS case study Intro to Shell Scripting. What is an Operating System? What are its goals? How do we evaluate it?

Control Structures 1 / 17

EECS 211 Lab 2. Getting Started. Getting the code. Windows. Mac/Linux

Starting Matlab. MATLAB Laboratory 09/09/10 Lecture. Command Window. Drives/Directories. Go to.

CSc 110, Autumn Lecture 10: return values and math

Exception Handling. Genome 559

Introduction to Computer Programming for Non-Majors

Transcription:

Modules and Programs 1 / 14

Python Programs Python code organized in modules, packages, and scripts. We ve already used some modules, now we ll learn what they are, how they re orgainized in packages, and how to write Python programs that can be run on their own, not just entered in the Python command shell. 2 / 14

Modules A module is a file with Python code. Python includes many standard modules, such as turtle: >>> import turtle >>> turtle.left(90) >>> turtle.forward(80) >>> turtle.right(90) >>> turtle.forward(80) >>> turtle.left(143.14) >>> turtle.forward(50) >>> turtle.left(73.73) >>> turtle.forward(50) >>> turtle.left(98.14) >>> turtle.forward(113.14) >>> turtle.left(135) >>> turtle.forward(80) >>> turtle.left(135) >>> turtle.forward(113.14) >>> turtle.left(135) >>> turtle.forward(80) You can see what this draws by running house.py. 3 / 14

Importing Modules ~import~ing a module means getting names from the module into scope. When you import a module, you can access the modules components with the dot operator as in the previous example. >>> import math >>> math.sqrt(64) 8.0 You can also import a module and give it an alias: import <module> as <local-name> >>> import math as m >>> m.sqrt(64) 8.0 4 / 14

Importing into Local Scope Remember that importing brings names into the scope of the import. Here we import the math module into one function. >>> def hypotenuse(a, b):... import math... return math.sqrt(a*a + b*b)... >>> hypotenuse(3, 4) 5.0 >>> math.sqrt(64) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name math is not defined 5 / 14

Importing Names from a Module You can choose to import only certain names from a module: >>> from math import sqrt >>> sqrt(64) 8.0 >>> floor(1.2) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name floor is not defined Or all names from a module: >>> from math import * >>> floor(1.2) 1 >>> sin(0) 0.0 >>> sin(.5 * pi) 1.0 Notice that with this syntax you don t have to use a fully-qualified name, e.g., module.name 6 / 14

Module Search Path Just as an operating system command shell searches for executable programs by searching the directories listed in the PATH environment variable, Python finds modules by searching directories. The module search path is stored in sys.path: >>> import sys >>> from pprint import pprint >>> pprint(sys.path) [, /home/chris/miniconda3/lib/python35.zip, /home/chris/miniconda3/lib/python3.5, /home/chris/miniconda3/lib/python3.5/plat-linux, /home/chris/miniconda3/lib/python3.5/lib-dynload, /home/chris/miniconda3/lib/python3.5/site-packages, /home/chris/miniconda3/lib/python3.5/site-packages/setuptools-23.0.0-py3.5.egg Notice that the current directory, represented by the ~ ~ at the beginning of the search path. Also, note use of pprint. 7 / 14

Packages Modules are just Python files. The directory path from some sys.path root to a Python source file is called a package. Make a directory called hanglib and download this file into it: draw.py >>> import hanglib.draw >>> hanglib.draw.stand() hanglib is a package and draw is a module within the hanglib package. Of course, you could import it as an alias to make coding more convenient: >>> import hanglib.draw >>> hanglib.draw.stand() >>> import hanglib.draw as draw >>> draw.head() Notice that, as in this example, we can make our own modules. 8 / 14

Python Scripts Take a look at the draw.py file. Notice the if statement at the bottom: # Is this the main (top-level) module? if name == main : stand() head() body() leftarm() rightarm() leftleg() rightleg() # Pause so the user can see the drawing before exiting. input( Press any key to exit. ) This makes the module a runnable Python program. It s similar to the main function or method from some other programming languages. With it we can import the file as a module to use its functions (or objects or variables), or run it from the command line. 9 / 14

Shebang! Another way to run a Python program (on Unix) is to tell the host operating system how to run it. We do that with a "shebang" line at the beginning of a Python program: #!/usr/bin/env python3 This line says "run python3 and pass this file as an argument." So if you have a program called foo with shebang line as above and which has been set executable (chmod +x foo.py), thse are equivalent: $ python3 foo.py $./foo.py 10 / 14

Interactive Programs The input() function Python reads all the characters typed into the console until the user presses ENTER and returns them as a string: >>> x = input() abcdefg1234567 >>> x abcdefg1234567 We can also supply a prompt for the user: >>> input( Give me a number: ) Give me a number: 3 3 And remember, input() returns a string that may need to be converted. >>> 2 * int(input("give me a number and I ll double it: ")) Give me a number and I ll double it: 3 6 11 / 14

Command Line Arguments $ python args.py one 2 two + one The python invocation above contains 6 command line arguments. 12 / 14

Command-line Arguments in Python When you run a Python program, Python collects the arguments to the program in a variable called sys.argv. Given a Python program (arguments.py): #!/usr/bin/env python3 import sys if len(sys.argv) < 2: print("you ve given me nothing to work with.") else: print(sys.argv[1] +"? Well I disagree!") $./arguments.py Pickles Pickles? Well I disagree! $./arguments.py You ve given me nothing to work with. 13 / 14

Conclusion Python Arguments 14 / 14