SlideShare a Scribd company logo
Lecture 1
Introduction to Python
Programming
Jeffery S. Horsburgh
Hydroinformatics
Fall 2014
This work was funded by National Science
Foundation Grants EPS 1135482 and EPS
1208732
Objectives
• Introduction to the Python programming
language
• Write and execute computer code to
automate repetitive tasks
• Retrieve and use data from common
hydrologic data sources
This Week’s Schedule
• Today
– Introduction to Python
– Key Python coding concepts and conventions
– Introduction to the coding challenge
• Thursday
– Group work on coding challenge
Why Python?
• Python is generally:
– Comparatively easy to learn
– Freely available
– Cross-platform (Windows, Mac, Linux)
– Widely used – extensive capabilities, documentation,
and support
– Access to advanced math, statistics, and database
functions
– Integrated into ArcGIS and other applications
– Simple, interpreted language – no compilation step
http://www.python.org
What is Python?
Python Basics
• To run Python interactively, we will use Idle (the
Python GUI)
– Command line interpreter – evaluates whatever you type
in
– Text editor with syntax highlighting
– Menu commands for changing settings and running files
• Windows: Start  All Programs  Python 2.7  Idle
(Python GUI)
• Mac: Applications  Python 2.7  Idle
Idle on Windows
Simple Arithmetic
>>>print 1 + 2
3
>>>
Try typing some mathematical expressions
Manipulate Strings
>>>print ‘charles’ + ‘darwin’
charlesdarwin
>>>
Try typing some string expressions
Variables
• Variables are names for values
• Created by use – no declaration necessary
>>>planet = ‘Pluto’
>>>print planet
Pluto
>>>
variable value
planet ‘Pluto’
Variables
• Variables are names for values
• Created by use – no declaration necessary
>>>planet = ‘Pluto’
>>>print planet
Pluto
>>>moon = ‘Charon’
>>>
variable value
planet ‘Pluto’
moon ‘Charon’
Variables
• Variables can be assigned to other variables
>>>p = planet
>>> variable value
planet ‘Pluto’
moon ‘Charon’
p
Variables
• Variables can be assigned to other variables
>>>p = planet
>>>print p
Pluto
>>>
variable value
planet ‘Pluto’
moon ‘Charon’
p
Variables
• In Python, variables are just names
• Variables do not have data types
>>>planet = ‘Pluto’
>>>
variable value
planet ‘Pluto’
string
Variables
• In Python, variables are just names
• Variables do not have data types
>>>planet = ‘Pluto’
>>>planet = 9
>>>
variable value
planet ‘Pluto’
integer
9
Variables
• In Python, variables are just names
• Variables do not have data types
>>>planet = ‘Pluto’
>>>planet = 9
>>>
variable value
planet ‘Pluto’
integer
9
Python collects the garbage and
recycles the memory (e.g., ‘Pluto’)
Variables
• You must assign a value to a variable before
using it
>>>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’
Division
/ 3.0 / 2 1.5
3 / 2 1
Exponentiation
** 2 ** 0.5 1.41421356…
Comparisons
>>>3 < 5
True
>>> Comparisons turn
numbers or
strings into True
or False
Comparisons
3 < 5 True Less than
3 != 5 True Not equal to
3 == 5 False Equal to (Notice double ==)
3 >= 5 False Greater than or equal to
1 < 3 < 5 True Multiple comparisons
Single ‘=‘ is assignment
Double ‘==‘ is comparison
Python Lists
• A container that holds a number of other
objects in a given order
• To create a list, put a number of expressions
in square brackets:
>>> L1 = [] # This is an empty list
>>> L2 = [90,91,92] # This list has 3 integers
>>> L3 = [‘Captain America’, ‘Iron Man’, ‘Spider Man’]
• Lists do not have to be homogenous
>>>L4 = [5, ‘Spider Man’, 3.14159]
Accessing Elements in a List
• Access elements using an integer index
item = List[index]
• List indices are zero based
>>> L3 = [‘Captain America’, ‘Iron Man’, ‘Spider Man’]
>>> print ‘My favorite superhero is’ + L3[2]
My favorite superhero is Spider Man
• To get a range of elements from a list use:
>>>L3[0:2] #Get the first two items in a list
['Captain America', 'Iron Man']
>>>len(L3) #Returns the number of elements in a list
3
>>L3[-1] #Get the last item in a list
'Spider Man'
Dictionaries
• A collection of pairs (or items) where each pair
has a key and a value
>>> D = {‘Jeff’: ‘a’, ‘Steve’:‘b’, ‘Jon’:‘c’}
>>> D[‘Jeff’]
‘a’
>>> D[‘Steve’] = ‘d’ #update value for key ‘Steve’
>>> D[‘Steve’]
‘d’
Behold the Power of Programming!
• The real power of programming comes from
the ability to perform:
– Selection – the ability to do one thing rather than
another
– Repetition – the ability to automatically do
something many times
Selection – if, elif, and else
moons = 3
if moons < 0:
print ‘less’
elif moons == 0:
print ‘equal’
else:
print ‘greater’
Always starts with if and a condition
There can be 0 or more elif clauses
The else clause has no condition
and is executed if nothing else is
done
Tests are always tried in order
Since moons is not less than 0 or
equal to zero, neither of the first
two blocks is executed
Selection – if, elif, and else
>>>moons = 3
>>>if moons < 0:
print ‘less’
elif moons == 0:
print ‘equal’
else:
print ‘greater’
greater
>>>
Indentation
• Python uses indentation to show which
statements are in an if, elif, else statement
• Any amount of indentation will work, but the
standard is 4 spaces (and you must be
consistent)
Repetition - Loops
• Simplest form of repetition is the while loop
numMoons = 3
while numMoons > 0:
print numMoons
numMoons -= 1
Repetition - Loops
• Simplest form of repetition is the while loop
numMoons = 3
while numMoons > 0:
print numMoons
numMoons -= 1
While this is true
Do this
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”
• In the interpreter type, help(‘modules’) to
get a list of all currently installed modules.
Import a Module into a Script
Some Python Resources
• Python 2.7.8 documentation:
https://docs.python.org/2/index.html
• Software Carpentry: http://software-
carpentry.org/index.html
• Python in Hydrology:
http://www.greenteapress.com/pythonhydro/
pythonhydro.html
And many others…
Coding Challenge
Get the Data
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
“The most recent streamflow value for
USGS Gage 10109000 was 109 cfs on
2014-08-25 3:15.”
Some Hints
• Develop your solution as a script so you can save it as a
file.
• The USGS website returns the data as a file, but you
have to request it using a URL in your code. The
Python module called “urllib2” is one option for
downloading a file using a URL.
• The data are returned in a text file where each new line
represents a new date/time and data value. The
Python module called “re” is one option for splitting a
string using delimiters such as tabs or new-line
characters.
• Also – check out the Python “split” method
Credits
• Some instructional materials adapted from
the Software Carpentry website
http://software-carpentry.org
Copyright © Software Carpentry
Support:
EPS 1135482

More Related Content

PPT
Python
PPTX
Lecture-2-Python-Basic-Elements-Sep04-2018.pptx
PPTX
Python_Haegl.powerpoint presentation. tx
PDF
Introduction to Python for Bioinformatics
PDF
Sessisgytcfgggggggggggggggggggggggggggggggg
PPTX
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
PPT
programming with python ppt
Python
Lecture-2-Python-Basic-Elements-Sep04-2018.pptx
Python_Haegl.powerpoint presentation. tx
Introduction to Python for Bioinformatics
Sessisgytcfgggggggggggggggggggggggggggggggg
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
programming with python ppt

Similar to Computer 10 Quarter 3 Lesson .ppt (20)

PDF
Python Programming unit5 (1).pdf
PDF
python-160403194316.pdf
PPTX
Python knowledge ,......................
PPTX
Python Seminar PPT
PPTX
Python
PPTX
Chapter1 python introduction syntax general
PDF
PPTX
Learning python
PPTX
Learning python
PPTX
Learning python
PPTX
Learning python
PPTX
Learning python
PPTX
Learning python
PPTX
Learning python
PDF
Python bootcamp - C4Dlab, University of Nairobi
PPTX
Python basics
PPTX
Python basics
PPTX
Python basics
PPTX
Python basics
PPTX
Python basics
Python Programming unit5 (1).pdf
python-160403194316.pdf
Python knowledge ,......................
Python Seminar PPT
Python
Chapter1 python introduction syntax general
Learning python
Learning python
Learning python
Learning python
Learning python
Learning python
Learning python
Python bootcamp - C4Dlab, University of Nairobi
Python basics
Python basics
Python basics
Python basics
Python basics
Ad

More from RedenOriola (13)

PPTX
21st Century Literature_Unit 2_Lesson 1_Sound Devices.pptx
PPTX
GMTC 1401 Q2 FPF.pptxGMTC 1401 Q2 FPF.pptx presentation slides
PPTX
GMTC 1401 Q2 FPF.pptx presentation slides
PPTX
Presentation1.pptx
PPTX
mpm powerpoint.pptx
PDF
MIL Lesson 8.pdf
PDF
MIL lesson 7.pdf
PPTX
Math LP 03-04-19.pptx
PPT
python1.ppt
PPTX
E-Tech Lesson 10.pptx
PDF
ICT 9 LESSON 1.pdf
PPTX
clinic for video.pptx
DOCX
Op siena system consensus map 2019 7
21st Century Literature_Unit 2_Lesson 1_Sound Devices.pptx
GMTC 1401 Q2 FPF.pptxGMTC 1401 Q2 FPF.pptx presentation slides
GMTC 1401 Q2 FPF.pptx presentation slides
Presentation1.pptx
mpm powerpoint.pptx
MIL Lesson 8.pdf
MIL lesson 7.pdf
Math LP 03-04-19.pptx
python1.ppt
E-Tech Lesson 10.pptx
ICT 9 LESSON 1.pdf
clinic for video.pptx
Op siena system consensus map 2019 7
Ad

Recently uploaded (20)

PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
RMMM.pdf make it easy to upload and study
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
Complications of Minimal Access Surgery at WLH
PDF
Classroom Observation Tools for Teachers
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Pre independence Education in Inndia.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Institutional Correction lecture only . . .
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Cell Structure & Organelles in detailed.
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Basic Mud Logging Guide for educational purpose
Supply Chain Operations Speaking Notes -ICLT Program
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
RMMM.pdf make it easy to upload and study
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Complications of Minimal Access Surgery at WLH
Classroom Observation Tools for Teachers
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Pre independence Education in Inndia.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Institutional Correction lecture only . . .
01-Introduction-to-Information-Management.pdf
Cell Structure & Organelles in detailed.
Week 4 Term 3 Study Techniques revisited.pptx
Basic Mud Logging Guide for educational purpose

Computer 10 Quarter 3 Lesson .ppt

  • 1. Lecture 1 Introduction to Python Programming Jeffery S. Horsburgh Hydroinformatics Fall 2014 This work was funded by National Science Foundation Grants EPS 1135482 and EPS 1208732
  • 2. Objectives • Introduction to the Python programming language • Write and execute computer code to automate repetitive tasks • Retrieve and use data from common hydrologic data sources
  • 3. This Week’s Schedule • Today – Introduction to Python – Key Python coding concepts and conventions – Introduction to the coding challenge • Thursday – Group work on coding challenge
  • 4. Why Python? • Python is generally: – Comparatively easy to learn – Freely available – Cross-platform (Windows, Mac, Linux) – Widely used – extensive capabilities, documentation, and support – Access to advanced math, statistics, and database functions – Integrated into ArcGIS and other applications – Simple, interpreted language – no compilation step http://www.python.org
  • 6. Python Basics • To run Python interactively, we will use Idle (the Python GUI) – Command line interpreter – evaluates whatever you type in – Text editor with syntax highlighting – Menu commands for changing settings and running files • Windows: Start  All Programs  Python 2.7  Idle (Python GUI) • Mac: Applications  Python 2.7  Idle
  • 8. Simple Arithmetic >>>print 1 + 2 3 >>> Try typing some mathematical expressions
  • 9. Manipulate Strings >>>print ‘charles’ + ‘darwin’ charlesdarwin >>> Try typing some string expressions
  • 10. Variables • Variables are names for values • Created by use – no declaration necessary >>>planet = ‘Pluto’ >>>print planet Pluto >>> variable value planet ‘Pluto’
  • 11. Variables • Variables are names for values • Created by use – no declaration necessary >>>planet = ‘Pluto’ >>>print planet Pluto >>>moon = ‘Charon’ >>> variable value planet ‘Pluto’ moon ‘Charon’
  • 12. Variables • Variables can be assigned to other variables >>>p = planet >>> variable value planet ‘Pluto’ moon ‘Charon’ p
  • 13. Variables • Variables can be assigned to other variables >>>p = planet >>>print p Pluto >>> variable value planet ‘Pluto’ moon ‘Charon’ p
  • 14. Variables • In Python, variables are just names • Variables do not have data types >>>planet = ‘Pluto’ >>> variable value planet ‘Pluto’ string
  • 15. Variables • In Python, variables are just names • Variables do not have data types >>>planet = ‘Pluto’ >>>planet = 9 >>> variable value planet ‘Pluto’ integer 9
  • 16. Variables • In Python, variables are just names • Variables do not have data types >>>planet = ‘Pluto’ >>>planet = 9 >>> variable value planet ‘Pluto’ integer 9 Python collects the garbage and recycles the memory (e.g., ‘Pluto’)
  • 17. Variables • You must assign a value to a variable before using it >>>planet = ‘Sedna’ >>>
  • 18. Variables • You must assign a value to a variable before using it >>>planet = ‘Sedna’ >>>print plant #Note the deliberate misspelling
  • 19. 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
  • 20. Values Do Have Types >>>string = ‘two’ >>>number = 3 >>>print string * number
  • 21. Values Do Have Types >>>string = ‘two’ >>>number = 3 >>>print string * number #Repeated concatenation twotwotwo >>>
  • 22. 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’
  • 23. 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
  • 24. Use Functions to Convert Between Types >>>print int(‘2’) + 3 5 >>>
  • 25. Arithmetic in Python Addition + 35 + 22 57 ‘Py’ + ‘thon’ ‘Python’ Subtraction - 35 - 22 13 Multiplication * 3 * 2 6 ‘Py’ * 2 ‘PyPy’ Division / 3.0 / 2 1.5 3 / 2 1 Exponentiation ** 2 ** 0.5 1.41421356…
  • 26. Comparisons >>>3 < 5 True >>> Comparisons turn numbers or strings into True or False
  • 27. Comparisons 3 < 5 True Less than 3 != 5 True Not equal to 3 == 5 False Equal to (Notice double ==) 3 >= 5 False Greater than or equal to 1 < 3 < 5 True Multiple comparisons Single ‘=‘ is assignment Double ‘==‘ is comparison
  • 28. Python Lists • A container that holds a number of other objects in a given order • To create a list, put a number of expressions in square brackets: >>> L1 = [] # This is an empty list >>> L2 = [90,91,92] # This list has 3 integers >>> L3 = [‘Captain America’, ‘Iron Man’, ‘Spider Man’] • Lists do not have to be homogenous >>>L4 = [5, ‘Spider Man’, 3.14159]
  • 29. Accessing Elements in a List • Access elements using an integer index item = List[index] • List indices are zero based >>> L3 = [‘Captain America’, ‘Iron Man’, ‘Spider Man’] >>> print ‘My favorite superhero is’ + L3[2] My favorite superhero is Spider Man • To get a range of elements from a list use: >>>L3[0:2] #Get the first two items in a list ['Captain America', 'Iron Man'] >>>len(L3) #Returns the number of elements in a list 3 >>L3[-1] #Get the last item in a list 'Spider Man'
  • 30. Dictionaries • A collection of pairs (or items) where each pair has a key and a value >>> D = {‘Jeff’: ‘a’, ‘Steve’:‘b’, ‘Jon’:‘c’} >>> D[‘Jeff’] ‘a’ >>> D[‘Steve’] = ‘d’ #update value for key ‘Steve’ >>> D[‘Steve’] ‘d’
  • 31. Behold the Power of Programming! • The real power of programming comes from the ability to perform: – Selection – the ability to do one thing rather than another – Repetition – the ability to automatically do something many times
  • 32. Selection – if, elif, and else moons = 3 if moons < 0: print ‘less’ elif moons == 0: print ‘equal’ else: print ‘greater’ Always starts with if and a condition There can be 0 or more elif clauses The else clause has no condition and is executed if nothing else is done Tests are always tried in order Since moons is not less than 0 or equal to zero, neither of the first two blocks is executed
  • 33. Selection – if, elif, and else >>>moons = 3 >>>if moons < 0: print ‘less’ elif moons == 0: print ‘equal’ else: print ‘greater’ greater >>>
  • 34. Indentation • Python uses indentation to show which statements are in an if, elif, else statement • Any amount of indentation will work, but the standard is 4 spaces (and you must be consistent)
  • 35. Repetition - Loops • Simplest form of repetition is the while loop numMoons = 3 while numMoons > 0: print numMoons numMoons -= 1
  • 36. Repetition - Loops • Simplest form of repetition is the while loop numMoons = 3 while numMoons > 0: print numMoons numMoons -= 1 While this is true Do this
  • 37. Repetition - Loops >>>numMoons = 3 >>>while numMoons > 0: print numMoons numMoons -= 1 3 2 1 >>>
  • 38. Combine Looping and Selection numMoons = 0 while numMoons < 5: if numMoons > 0: print numMoons numMoons += 1
  • 39. Combine Looping and Selection >>>numMoons = 0 >>>while numMoons < 5: if numMoons > 0: print numMoons numMoons += 1 1 2 3 4 >>>
  • 40. 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
  • 41. 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.
  • 42. 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” • In the interpreter type, help(‘modules’) to get a list of all currently installed modules.
  • 43. Import a Module into a Script
  • 44. Some Python Resources • Python 2.7.8 documentation: https://docs.python.org/2/index.html • Software Carpentry: http://software- carpentry.org/index.html • Python in Hydrology: http://www.greenteapress.com/pythonhydro/ pythonhydro.html And many others…
  • 47. 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
  • 48. Example Solution “The most recent streamflow value for USGS Gage 10109000 was 109 cfs on 2014-08-25 3:15.”
  • 49. Some Hints • Develop your solution as a script so you can save it as a file. • The USGS website returns the data as a file, but you have to request it using a URL in your code. The Python module called “urllib2” is one option for downloading a file using a URL. • The data are returned in a text file where each new line represents a new date/time and data value. The Python module called “re” is one option for splitting a string using delimiters such as tabs or new-line characters. • Also – check out the Python “split” method
  • 50. Credits • Some instructional materials adapted from the Software Carpentry website http://software-carpentry.org Copyright © Software Carpentry Support: EPS 1135482