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

Similar documents
Lecture 3. Functions & Modules

Lecture 3. Functions & Modules

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

Lecture 4. Defining Functions

Lecture 3: Functions & Modules

Lecture 4: Basic I/O

CS1110 Lab 1 (Jan 27-28, 2015)

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

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

Lecture 3. Strings, Functions, & Modules

Scripting Languages. Python basics

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

Introduction to Python Code Quality

A simple interpreted language

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

Programming for Engineers in Python. Recitation 1

Python for Non-programmers

Getting Started with Python

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

ENGR 101 Engineering Design Workshop

Introduction to Python

Constants. Variables, Expressions, and Statements. Variables. x = 12.2 y = 14 x = 100. Chapter

Python language: Basics

Programming for Engineers in Python. Autumn

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

Getting Started Values, Expressions, and Statements CS GMU

Variables, Expressions, and Statements

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

Introduction to computers and Python. Matthieu Choplin

61A Lecture 2. Friday, August 28, 2015

Fundamentals: Expressions and Assignment

Laboratory Exercise #0

CMPT 120 Basics of Python. Summer 2012 Instructor: Hassan Khosravi

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

Lecture 5: Strings

Fundamentals of Programming (Python) Getting Started with Programming

The current topic: Python. Announcements. Python. Python

CSE 111 Bio: Program Design I Lecture 4: Variables, Functions, Strings, Genbank

PREPARING FOR PRELIM 1

7. MODULES. Rocky K. C. Chang October 17, (Based on Dierbach)

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

2. Modules, Scripts, and I/O

Functions and Decomposition

Using ImageJ for Manual and Automated Image Analysis. Jeff LeDue and Claire Bomkamp November

Programming in Python 3

Introduction to Computation for the Humanities and Social Sciences. CS 3 Chris Tanner

Lecture 3. Input, Output and Data Types

COMP 204: Sets, Commenting & Exceptions

Modules and Programs 1 / 14

CSE : Python Programming

COMP 204: Sets, Commenting & Exceptions

CS1 Lecture 3 Jan. 22, 2018

MEIN 50010: Python Introduction

ENGR (Socolofsky) Week 02 Python scripts

Ch.2: Loops and lists

Notes on Chapter 1 Variables and String

CSE : Python Programming

Lecture 1. Types, Expressions, & Variables

Introduction to Python

Exceptions CS GMU

Programming to Python

Fun with functions! Built-in and user-defined functions

Lecture 5 8/24/18. Writing larger programs. Comments. What are we going to cover today? Using Comments. Comments in Python. Writing larger programs

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Python Day 3 11/28/16

IE 555 Programming for Analytics

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

Babes-Bolyai University

from scratch A primer for scientists working with Next-Generation- Sequencing data Chapter 1 Text output and manipulation

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

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

S206E Lecture 19, 5/24/2016, Python an overview

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

Lecture 6: Specifications & Testing

PHP. Interactive Web Systems

Part III Appendices 165

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

Algorithms and Programming I. Lecture#12 Spring 2015

Python Input, output and variables

Chapter 2 Getting Started with Python

Computer Lab 1: Introduction to Python

MEIN 50010: Python Flow Control

Python Input, output and variables. Lecture 22 COMPSCI111/111G SS 2016

8. Control statements

My First Python Program

Lecture 4. Defining Functions

Types, lists & functions

Impera've Programming

Python 2 Conditionals and loops Matthew Egbert CS111

Announcements for this Lecture

10/9/07. < Moo? > \ ^ ^ \ (oo)\ ( )\ )\/\ ----w

CS1 Lecture 3 Jan. 18, 2019

Lecture 4: Simple Input-Calculate-Output Programs

1

MEIN 50010: Python Strings

Lecture 1. basic Python programs, defining functions

Using echo command in shell script

Lecture 7. Memory in Python

1 Classes. 2 Exceptions. 3 Using Other Code. 4 Problems. Sandeep Sadanandan (TU, Munich) Python For Fine Programmers May 16, / 19

Lecture 10: Lists and Sequences

Transcription:

Not-So-Mini-Lecture 6 Modules & Scripts

Interactive Shell vs. Modules Launch in command line Type each line separately Python executes as you type Write in a code editor We use Atom Editor But anything will work Load module with import 9/7/18 Modules & Scripts 2

Using a Module Module Contents A simple module. This file shows how modules work x 9/7/18 Modules & Scripts 3

Using a Module Module Contents A simple module. This file shows how modules work x Single line comment (not executed) 9/7/18 Modules & Scripts 4

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 x Single line comment (not executed) 9/7/18 Modules & Scripts 5

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 x Commands Executed on import Single line comment (not executed) 9/7/18 Modules & Scripts 6

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 x Commands Executed on import Not a command. import ignores this Single line comment (not executed) 9/7/18 Modules & Scripts 7

Using a Module Module Contents A simple module. This file shows how modules work Python Shell >>> import module >>> x x 9/7/18 Modules & Scripts 8

Using a Module Module Contents A simple module. This file shows how modules work x Python Shell >>> import module >>> x Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'x' is not defined 9/7/18 Modules & Scripts 9

Using a Module Module Contents A simple module. This file shows how modules work 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 >>> 9 module.x 9/7/18 Modules & Scripts 10

Using a Module Module Contents A simple module. This file shows how modules work 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 >>> 9 >>> module.x help(module) 9/7/18 Modules & Scripts 11

Modules Must be in Active Directory! Module you want is in this folder 9/7/18 Modules & Scripts 12

Modules Must be in Active Directory! Module you want is in this folder Have to navigate to folder BEFORE running Python 9/7/18 Modules & Scripts 13

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 9/7/18 Modules & Scripts 14

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. 9/7/18 Modules & Scripts 15

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 x print(x) 9/7/18 Modules & Scripts 16

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 x Only difference print(x) 9/7/18 Modules & Scripts 17

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) 9/7/18 Modules & Scripts 18

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! When you run a script, only statements are executed Python did the following: Executed the assignments Executed the last line (Prints the contents of x) 9/7/18 Modules & Scripts 19

>>> input('type something') Type somethingabc 'abc' >>> input('type something: ') Type something: abc 'abc' User Input >>> x = input('type something: ') Type something: abc >>> x 'abc' No space after the prompt. Proper space after prompt. Assign result to variable. 9/7/18 Modules & Scripts 20

Making a Script Interactive A script showing off input. This file shows how to make a script interactive. x = input("give me a something: ") print("you said: "+x) [wmw2] folder> python script.py Give me something: Hello You said: Hello [wmw2] folder> python script.py Give me something: Goodbye You said: Goodbye [wmw2] folder> Not using the interactive shell 9/7/18 Modules & Scripts 21

Numeric Input input returns a string Even if looks like int It cannot know better You must convert values int(), float(), bool(), etc. Error if cannot convert One way to program But it is a bad way Cannot be automated >>> x = input('number: ') Number: 3 >>> x '3' >>> x + 1 TypeError: must be str, not int >>> x = int(x) >>> x+1 4 Value is a string. Must convert to int. 9/7/18 Modules & Scripts 22