Professional Documents
Culture Documents
Variables: - in Python, Variables Are Just Names - Variables Do Not Have Data Types Planet Planet 9
Variables: - in Python, Variables Are Just Names - Variables Do Not Have Data Types Planet Planet 9
integer
Variables
• In Python, variables are just names
• Variables do not have data types
>>>planet = ‘Sedna’
>>>
Variables
• You must assign a value to a variable before
using it
>>>planet = ‘Sedna’
>>>print plant #Note the deliberate misspelling
Variables
• You must assign a value to a variable before
using it
>>>planet = ‘Sedna’
>>>print plant #Note the deliberate misspelling
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
print plant
NameError: name 'plant' is not defined
Unlike some languages – Python does not initialize variables with a default value
Values Do Have Types
>>>string = ‘two’
>>>number = 3
>>>print string * number
Values Do Have Types
>>>string = ‘two’
>>>number = 3
>>>print string * number #Repeated concatenation
twotwotwo
>>>
Values Do Have Types
>>>string = ‘two’
>>>number = 3
>>>print string * number #Repeated concatenation
twotwotwo
>>>print string + number
‘two3’ ????
If so, then what is the result of ‘2’ + ‘3’
• Should it be the string ‘23’
• Should it be the number 5
• Should it be the string ‘5’
Values Do Have Types
>>>string = ‘two’
>>>number = 3
>>>print string * number #Repeated concatenation
twotwotwo
>>>print string + number
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
print string + number
TypeError: cannot concatenate 'str' and 'int' objects
Use Functions to Convert Between Types
>>>print int(‘2’) + 3
5
>>>
Arithmetic in Python
Addition 35 + 22 57
+
‘Py’ + ‘thon’ ‘Python’
Subtraction 35 - 22 13
-
Multiplication 3*2 6
*
‘Py’ * 2 ‘PyPy’
greater
>>>
Indentation
• Python uses indentation to show which
statements are in an if, elif, else statement
numMoons = 3
while numMoons > 0:
print numMoons
numMoons -= 1
Repetition - Loops
• Simplest form of repetition is the while loop
numMoons = 3
while numMoons > 0: While this is true
print numMoons
Do this
numMoons -= 1
Repetition - Loops
>>>numMoons = 3
>>>while numMoons > 0:
print numMoons
numMoons -= 1
3
2
1
>>>
Combine Looping and Selection
numMoons = 0
while numMoons < 5:
if numMoons > 0:
print numMoons
numMoons += 1
Combine Looping and Selection
>>>numMoons = 0
>>>while numMoons < 5:
if numMoons > 0:
print numMoons
numMoons += 1
1
2
3
4
>>>
Saving and Executing Code
• Writing and executing complex code is too
difficult to do line by line at the command line
• As soon as you close the Python interpreter,
all of your work is gone…
• Instead
– Write code using a text editor
– Save the code as a text file with a “.py” extension
– Execute code in the file from the command line
Using a Script File
• Start a new Python text file in Idle. You can write Python in
Notepad, but if you use a formal editor, you get color coding!
• Click “File/New File”. This will open the script editor.
• Write your script and save it as a “*.py” file. Then click
“Run/Run Module” to test it….
Results appear in the “shell” window.
Modules
• You may want to reuse a function that you have
written in multiple scripts without copying the
definition into each one
• Save the function(s) as a module
• Import the module into other scripts
• It’s like an “extension” or “plug-in”
http://waterdata.usgs.gov/nwis/uv?cb_00060=on&forma
t=rdb&site_no=10109000&period=&begin_date=2014-
08-18&end_date=2014-08-25
Coding Challenge
1. Divide into small groups of no more than 3-4 people
2. Choose a real-time streamflow gage from the USGS
that you are interested in. It could be a nearby gage
or one that is near and dear to your heart. To see an
interactive map of gage locations, go to:
http://maps.waterdata.usgs.gov/mapper/index.html
3. Create a Python script that does the following:
a. Download the most recent data from the USGS website
b. Read the file
c. Extract the most recent streamflow value from the file
d. Print the most recent streamflow value and the date at
which it occurred to the screen
Example Solution
Support:
EPS 1135482