2. ModulesAndScripts.pdf - 2 Modules Scripts and I\/O Topics Script Mode Modules The print and input statements Formatting First look at importing stuff

2. ModulesAndScripts.pdf - 2 Modules Scripts and I/O Topics...

This preview shows page 1 - 13 out of 46 pages.

2. Modules, Scripts, and I/O Topics: Script Mode Modules The print and input statements Formatting First look at importing stuff from other modules
Image of page 1
The Windchill Calculation Let’s compute the windchill temperature given that the air temperature is T = 32F and the wind is W = 20mph. Here is the formula courtesy of the National Weather Service: The formula only applies if T <= 50F and W>=3mph. 16 . ) 4275 . 0 75 . 35 ( ) 6215 . 0 74 . 35 ( W T T W chill
Image of page 2
Use Python in Interactive Mode >>> Temp = 32 >>> Wind = 20 >>> A = 35.74 >>> B = .6215 >>> C = -35.75 >>> D = .4275 >>> e = .16 >>> WC = (A+B*Temp)+(C+D*Temp)*Wind**e >>> print WC 19.9855841878 The print statement is used for displaying values in variables.
Image of page 3
Quick Note on print The line >>> print WC results in the display of the value currently housed in the variable WC More on the print statement later.
Image of page 4
Motivating “Script Mode” What is the new windchill if the wind is increased from 20mph to 30mph? Looks like we have to type in the same sequence of statements. Tedious. Wouldn’t it be nice if we could store the sequence of statements in a file and then have Python “run the file” after we changed Wind = 20 to Wind = 30 ?
Image of page 5
Script Mode Instead of running Python in interactive mode , we run Python in script mode . The code to be run (called a script ) is entered into a file (called a module ). We then ask Python to “run the script”.
Image of page 6
What is a Module? A module is a . py file that contains Python code. In CS 1110, these are created using Komodo Edit.
Image of page 7
The Module WindChill.py Temp = 32 Wind = 20 A = 35.74 B = .6215 C = -35.74 D = .4275 e = .16 WC = (A+B*Temp)+(C+D*Temp)*Wind**e print WC Produced using Komodo Edit. This is our first draft. WindChill.py
Image of page 8
Running the Module Here are the steps to follow in the command shell: 1. Navigate the file system so that you are “in” the same diretory that houses WindChill.py 2. Type: python WindChill.py
Image of page 9
Details Suppose I have a directory on my desktop called TODAY where I keep all my python files for today’s lecture. I navigate the file system until I get this prompt: C:\Users\cv\Desktop\TODAY>
Image of page 10
Asking Python to Run the Code in WindChill.py C:\Users\cv\Desktop\TODAY> Python WindChill.py 19.6975841877955 To save space in subsequent slides, we will refer to C:\Users\cv\Desktop\TODAY> as Blah\Blah>
Image of page 11
Multiple Statements on a Line Temp = 32 Wind = 20 A=35.74;B=.6215;C=-35.74;D=.4275;e=.16 WC = (A+B*Temp)+(C+D*Temp)*Wind**e print WC Can put multiple statements on a line. Separate the statements with semicolons.
Image of page 12
Image of page 13

You've reached the end of your free preview.

Want to read all 46 pages?

  • Fall '19
  • Irrational number, Wind chill

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes