Not-So-Mini-Lecture 6 Modules & Scripts
Interactive Shell vs. Modules • Write in a code editor • Launch in command line § We use Atom Editor • Type each line separately § But anything will work • Python executes as you type • Load module with import 9/7/18 Modules & Scripts 2
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 9/7/18 Modules & Scripts 3
Using a Module Module Contents """ A simple module. This file shows how modules work """ Single line comment (not executed) # This is a comment x = 1+2 x = 3*x x 9/7/18 Modules & Scripts 4
Using a Module Module Contents """ A simple module. Docstring (note the Triple Quotes) Acts as a multiple-line comment Useful for code documentation This file shows how modules work """ Single line comment (not executed) # This is a comment x = 1+2 x = 3*x x 9/7/18 Modules & Scripts 5
Using a Module Module Contents """ A simple module. Docstring (note the Triple Quotes) Acts as a multiple-line comment Useful for code documentation This file shows how modules work """ Single line comment (not executed) # This is a comment x = 1+2 Commands Executed on import x = 3*x x 9/7/18 Modules & Scripts 6
Using a Module Module Contents """ A simple module. Docstring (note the Triple Quotes) Acts as a multiple-line comment Useful for code documentation This file shows how modules work """ Single line comment (not executed) # This is a comment x = 1+2 Commands Executed on import x = 3*x x Not a command. import ignores this 9/7/18 Modules & Scripts 7
Using a Module Module Contents Python Shell """ A simple module. >>> import module x >>> This file shows how modules work """ # This is a comment x = 1+2 x = 3*x x 9/7/18 Modules & Scripts 8
Using a Module Module Contents Python Shell """ A simple module. >>> import module x >>> This file shows how modules work Traceback (most recent call last): """ File "<stdin>", line 1, in <module> NameError: name 'x' is not defined # This is a comment x = 1+2 x = 3*x x 9/7/18 Modules & Scripts 9
Using a Module Module Contents Python Shell """ A simple module. >>> import module x >>> This file shows how modules work Traceback (most recent call last): """ File "<stdin>", line 1, in <module> NameError: name 'x' is not defined # This is a comment module.x >>> “ Module data ” must be x = 1+2 9 prefixed by module name x = 3*x x 9/7/18 Modules & Scripts 10
Using a Module Module Contents Python Shell """ A simple module. >>> import module x >>> This file shows how modules work Traceback (most recent call last): """ File "<stdin>", line 1, in <module> NameError: name 'x' is not defined # This is a comment module.x >>> “ Module data ” must be x = 1+2 9 prefixed by module name x = 3*x help(module) >>> Prints docstring and x module contents 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 Script • Provides functions, variables • Behaves like an application § Example : temp.py § Example : helloApp.py • import it into Python shell • Run it from command line: >>> import temp python helloApp.py >>> temp.to_fahrenheit(100) 212.0 >>> 9/7/18 Modules & Scripts 14
Modules vs. Scripts Module Script • Provides functions, variables • Behaves like an application § Example : temp.py § Example : helloApp.py • import it into Python shell • Run it from command line: >>> import temp python helloApp.py >>> temp.to_fahrenheit(100) 212.0 >>> Files look the same. Difference is how you use them. 9/7/18 Modules & Scripts 15
Scripts and Print Statements module.py script.py """ A simple module. """ A simple script. This file shows how modules work This file shows why we use print """ """ # This is a comment # This is a comment x = 1+2 x = 1+2 x = 3*x x = 3*x x print(x) 9/7/18 Modules & Scripts 16
Scripts and Print Statements module.py script.py """ A simple module. """ A simple script. This file shows how modules work This file shows why we use print """ """ # This is a comment # This is a comment x = 1+2 x = 1+2 x = 3*x x = 3*x x print(x) Only difference 9/7/18 Modules & Scripts 17
Scripts and Print Statements module.py script.py • Looks like nothing happens • We see something this time! • Python did the following: • Python did the following: § Executed the assignments § Executed the assignments § Skipped the last line § Executed the last line (‘ x ’ is not a statement) (Prints the contents of x ) 9/7/18 Modules & Scripts 18
Scripts and Print Statements module.py script.py When you run a script, • Looks like nothing happens • We see something this time! only statements are executed • Python did the following: • Python did the following: § Executed the assignments § Executed the assignments § Skipped the last line § Executed the last line (‘ x ’ is not a statement) (Prints the contents of x ) 9/7/18 Modules & Scripts 19
User Input >>> input('Type something') Type somethingabc No space after the prompt. 'abc' >>> input('Type something: ') Type something: abc Proper space after prompt. 'abc' >>> x = input('Type something: ') Type something: abc Assign result to variable. >>> x 'abc' 9/7/18 Modules & Scripts 20
Making a Script Interactive """ A script showing off input. [wmw2] folder> python script.py Give me something: Hello You said: Hello This file shows how to make a script interactive. [wmw2] folder> python script.py """ Give me something: Goodbye You said: Goodbye x = input("Give me a something: ") [wmw2] folder> print("You said: "+x) Not using the interactive shell 9/7/18 Modules & Scripts 21
Numeric Input >>> x = input('Number: ') • input returns a string Number: 3 § Even if looks like int § It cannot know better >>> x Value is a string. '3' • You must convert values >>> x + 1 § int() , float() , bool() , etc. § Error if cannot convert TypeError: must be str, not int >>> x = int(x) • One way to program >>> x+1 § But it is a bad way Must convert to int. § Cannot be automated 4 9/7/18 Modules & Scripts 22
Recommend
More recommend