0% found this document useful (0 votes)
5 views63 pages

Python Windchill Calculation Guide

Uploaded by

a.ahgv1453
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views63 pages

Python Windchill Calculation Guide

Uploaded by

a.ahgv1453
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

SENG 113: Computer Programming I

Doç.Dr. Hilal ARSLAN

Week 3: Introduction to Python

Week 3: Introduction to Python 1 / 32


Computer Programming
Outline

• Script Mode Modules

• The print and input statements

• Strings
Example

Computer Programming
Computer Programming
Computer Programming
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 given by the National


Weather Service:

W  (35.74  0.6215T)  (35.75  0.4275T ) W 0.16


chill

The formula only applies if T <= 50F and W>=3mph.


Use Python in Interactive Mode
>>> Temp = 32
>>> Wind = 20
>>> A = 35.74
>>> B = 0.6215
>>> C = -35.75
>>> D = 0.4275
>>> e = 0.16
>>> WC = (A+B*Temp)+(C+D*Temp)*Wind**e
>>> print WC
19.9855841878

The print statement is used for displayingvalues in variables.


Motivating “Script Mode”

What is the new windchill if the wind is


increased from 20mph to 30mph?

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


What is a Module?

A module is a .py file that contains Python code.

In CS 1110, these are created using Komodo Edit.


The Module WindChill.py
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
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


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>
Asking Python to Run
the Code in WindChill.py

C:\Users\cv\Desktop\TODAY> python WindChill.py

19.6975841877955
Multiple Statements on a Line
Can put multiple statements on a line. Separate
the statements with semicolons.

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
Jupyter Notebook
Module Readability: Comments
Comments begin with a “#”

WindChill.py

Temp = 32
Wind = 20
# Model Parameters
A=35.74;B=.6215;C=-35.74;D=.4275;e=.16
# Compute and display the windchill
WC = (A+B*Temp)+(C+D*Temp)*Wind**e
print (WC)
Comments: Guidelines
Comments can also appear on the same line
as a statement:

Wind = 20 # wind speed in miles-per-hour

Everything to the right of the “#” is


part of the comment and not part of the
program.
Comments and Readability

Start each program (script) with a clear


description of what it does.

Define each important variable/constant.


Module Readability: docstrings
A special comment at the top of the module.
WindChill.py
“““Computes windchill as a function of
wind(mph)and temp (Fahrenheit).”””
Temp = 32
Wind = 20
# Model Parameters
A=35.74;B=.6215;C=-35.74;D=.4275;e=.16
# Compute and display the windchill
WC = (A+B*Temp)+(C+D*Temp)*Wind**e
print WC
Docstrings: Guidelines
Docstrings are multiline comments that are
delimited by triple quotes: “““

They are strategically located at the beginning


of “important” code sections.

Their goal is to briefly describe what the


code section is about.

One example of an “important” code section is a module.


Trying Different Inputs
WindChill.py
“““Computes windchill as a function of
wind(mph)and temp (Fahrenheit).”””
Temp = 32 Can we be more
Wind = 20 flexible here?

# Model Parameters
A=35.74;B=.6215;C=-35.74;D=.4275;e=.16
# Compute and display the windchill
WC = (A+B*Temp)+(C+D*Temp)*Wind**e
print (WC)
Handy Input

If we want to explore windchill as a


function of windspeed and temperature,
then it is difficult to proceed by editing
the module WindChill.py every time
we want to check out a new wind/temp
combination.

The input statement addresses this issue.


The input Statement

The input statement takes input


values via the keyboard:

input(< string that serves as a prompt > )

Later we will learn how to input data from a file.


Temp and Wind via input
WindChill2.py
“““Computes windchill as a function of wind(mph)and
temp (Fahrenheit).”””
Temp = float(input(‘Enter Temp (Fahrenheit):’))
Wind = float(input(‘Enter wind speed (mph):’))

# Model Parameters
A=35.74;B=.6215;C=-35.74;D=.4275;e=.16
# Compute and display the windchill
WC = (A+B*Temp)+(C+D*Temp)*Wind**e
print (WC)
A Sample Run
The prompt is displayed…

> Enter temp (Fahrenheit) :

And you respond…

> Enter temp (Fahrenheit) : 15


A Sample Run
The next prompt is displayed…

> Enter wind speed (mph) :

And you respond again…

> Enter wind speed (mph) : 50


A Sample Overall “Dialog”

BlahBlah> python WindChill.py


Enter temp (Fahrenheit) : 15
Enter wind speed (mph) : 50
-9.79781580448
More Readable Output

The print statement can be used to format


output in a way that facilitates the
communication of results.

We don’t need to show wind chill to the


12th decimal!
More Readable Output
WindChill.py
“““Computes windchill as a function of
wind(mph)and temp (Fahrenheit).”””

Temp = float(input(‘Enter temp (Fahrenheit):’))


Wind = float(input(‘Enter wind speed (mph):’))

# Model Parameters
A=35.74;B=.6215;C=-35.74;D=.4275;e=.16
# Compute and display the windchill
WC = (A+B*Temp)+(C+D*Temp)*Wind**e
print (‘Windchill :%4.0f' % WC)
The “Dialog” With Formatting
BlahBlah> WindChill
Enter temp (Fahrenheit) : 15 print
Enter wind speed (mph) : 50 without
-9.79781580448 formatting

BlahBlah> WindChill
Enter temp (Fahrenheit) : 15 print
Enter wind speed (mph) : 50 with
Windchill : -10 formatting
The print Statement

The print statement tries to intelligently


format the results that it is asked to
display.

print with formatting puts you in control.

Later we will learn how to direct output to a file


print w/o Formatting
Script: x = 2./5.
print (x)
x = 1./3.
print (x)
x = 1234.5678901234
print (x)

Output: 0.4
0.333333333333
1234.56789012
print w/o Formatting

Script: x = 1234
y = 12345678
print (x,y)

Output:

To display more then one value on a line, separate the references with commas.
A single blank is placed in between the displayed values.
print w/o Formatting

Script: x = 1234
y = 12345678
print (x,y)

Output:
1234 12345678

To display more then one value on a line, separate the references with commas.
A single blank is placed in between the displayed values.
print with the %f Format

x = 1234.123456789
print (‘x = %16.3f’ %x)
print (‘x = %16.6f’ %x)
print (‘x = %16.9f’ %x)

x = 1234.123
x = 1234.123457
x = 1234.123456789

Formatted print statements are developed by “trial and error.”


It not a topic for memorization and it does not show up on exams.
print with the %d Format

x = 1234
Print(‘x = %4d’ %x)
print ‘x = %7d’ %x)
print ‘x = %10d’ %x)

x = 1234
x = 1234
x = 1234
print with the %s Format

Script: Band = ‘The Beatles’


print( ‘%s in 1964’ % Band)

Output: The Beatles in 1964

Strings can be printed too


Formatted Print With More than
1 Source Value
Script:

y1 = 1964
Need parentheses
y2 = 1971 here.
Band = ‘The Beatles’
Print( ‘%s in %4d and %4d’ %(Band,y1,y2))

Output:

The Beatles in 1964 and 1971


print with Formatting
print < string with formats > %( < list-of-variables > )
A string that Comma-separated,
includes e.g., x,y,z. One
things like variable for each
%10.3f. %3d, format marker
%8.2s, etc in the string. The
Parentheses are
Required if more
than one variable.

Practice with the demo file ShowFormat.py


Another Detail
All modules that are submitted for grading
should begin with three comments.

WindChill.py
# WindChill.py Name of module
# Xavier Zanzibar (123) Your name and id
# January 1, 2023 Date

etc
A Final Example

Write a script that takes the area of


a circle as an input and prints out the
radius.
Preliminary Solution

Radius.py
A = input(‘Enter the circle area: ‘)
r = sqrt(A/3.14)
print (r)

The Math: solve A = pi*r*r for r.


We Get an Error

A = input(‘Enter the circle area ‘)


:
r = sqrt(A/3.14)
print (‘The radius is %6.3f’ % r)

r = sqrt(A/3.14)
NameError: name 'sqrt' is not defined

sqrt is NOT a built-in function


Final Solution

We are importing the function sqrt


from the math module.

The Math: solve A = pi*r*r for r.


The Idea Behind import
People write useful code and place it in
modules that can be accessed by others.
The import statement makes this possible.

One thing in the math module is the square


root function sqrt.

If you want to use it in your module just say

from math import sqrt


The Idea Behind import
Better Final Solution
Radius.py

We are importing the function sqrt and


the constant pifrom the math module.

Can import more than one thing from a module. Much more on import later.
Strings
Strings are quoted characters. Here are three
examples:

>>> s1 = ‘abc’
>>> s2 = ‘ABC’
>>> s3 = ‘ A B C ‘

s1, s2, and s3 are variables with string value.


Strings are Indexed

>>> s = ‘The Beatles’

s --> T h e B e a t l e s
0 1 2 3 4 5 6 7 8 9 10

The characters in a string can be referenced


through their indices. Called “subscripting”.

Subcripting from zero creates a disconnect: ‘T’ is not the first character.
Strings are Indexed

>>> s =‘The Beatles’


>>> t = s[4]

s --> T h e B e a t l e s

0 1 2 3 4 5 6 7 8 9 10
t --> B

0
The square bracket notation is used. Note, a single character is a string.
String Slicing

>>> s =‘The Beatles’


>>> t = s[4:8]

s --> T h e B e a t l e s
0 1 2 3 4 5 6 7 8 9 10

t --> B e a t

0 1 2 3
We say that “t is a slice of s”.
String Slicing

>>> s =‘The Beatles’


>>> t = s[4:]

s --> T h e B e a t l e s
0 1 2 3 4 5 6 7 8 9 10

t --> B e a t l e s

0 1 2 3 4 5 6
Same as s[4:11]. Handy notation when you want an “ending slice.”
String Slicing
>>> s =‘The Beatles’
>>> t = s[:4]

s --> T h e B e a t l e s

0 1 2 3 4 5 6 7 8 9 10

t --> T h e

0 1 2 3
Same as s[0:4]. Handy notation when you want a “beginning slice”.
String Slicing
>>> s =‘The Beatles’
>>> t = s[11]
IndexError: string index out of
range

s --> T h e B e a t l e s

0 1 2 3 4 5 6 7 8 9 10
The is no s[11]. An illegal to access.
Subscripting errors are EXTREMELY common.
String Slicing
>>> s =‘The Beatles’
>>> t = s[8:20]

s --> T h e B e a t l e s

0 1 2 3 4 5 6 7 8 9 10

t --> l e s

0 1 2
It is “OK” to shoot beyond the end of the source string.
Strings Can Be Combined

>>> s1 = ‘The’
>>> s2 = ‘Beatles’
>>> s = s1+s2

s --> T h e B e a t l e s

This is called concatenation.

Concatenation is the string analog of additionexcept


Concatenation

>>> s1 = ‘The’
>>> s2 = ‘Beatles’
>>> s = s1 + ‘ ‘ + s2

s --> T h e B e a t l e s

We “added” in a blank.

No limit to the number of input strings: s = s2+s2+s2+s2+s2


Types

Strings are a type: str


So at this point we introduced 3 types:
int for integers, e.g., -12
float for decimals, e.g., 9.12, -12.0
str for strings, e.g., ‘abc’, ’12.0’

Python has other built-in types. And we will learn to make up our own types.
A Type is a Set of Values and
Operations on Them
Values…

int 123, -123, 0


float 1.0, -.00123, -12.3e-5
str ‘abcde’, ‘123.0’

These are called “literals”


The “e” notation (a power-of-10 notation) is handy for very large or very small
A Type is a Set of Values and
Operations on Them

Operations…

int + - * / ** %
float + - * / **
str +

concatenation

You might also like