modules scripts interactive shell vs modules
play

Modules & Scripts Interactive Shell vs. Modules Write in a code - PowerPoint PPT Presentation

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


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

  2. 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

  3. 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

  4. 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

  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 x = 3*x x 9/7/18 Modules & Scripts 5

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

  7. 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

  8. 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

  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 x = 1+2 x = 3*x x 9/7/18 Modules & Scripts 9

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

  11. 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

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

  13. 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

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

  15. 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

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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend