Docsity

UCF Physics: Advanced Astronomical Data Analysis Lecture Notes - Functions in Python, Study notes of Astronomy

Toggle sidebar
bg1
UCF Physics: AST 5765/4762: (Advanced) Astronomical Data Analysis
Fall 2009 Lecture Notes: 7. Python Functions
1 Check In: 10:30 — 10:35:, 5 min
•Do you have VNC up, python running, and the lecture demo ready to go?
•Who has done the matplotlib tutorial? How much of pydatatut?
•Any problems with system?
•Examples/templates in python directory
•HW3 is out, there is a handout called coding.pdf
•Questions before we start?
2 Go over HW2: 10:35 — 10:55, 20 min
3 Functions in General: 10:55 — 11:10, 15 min
•Computer programs can be very long and complicated
•Manage complexity by “boxing away” detail in functions
•Difference between “black box” and “glass brick”
•A function is a program unit that can be called by name
•Also called “routine” or “procedure” or “subprogram”, w/ nuances
•Contains program statements
•Executes same way each time it is called
•Programmer says name of function rather than inserting whole block of statements in the
main routine
•Example:
def triple(x):
y=3*x
return y
•If you now say triple(4), it prints 12
•Variables outside function can have different names from those inside:
1
pf3
pf4

Partial preview of the text

Download UCF Physics: Advanced Astronomical Data Analysis Lecture Notes - Functions in Python and more Study notes Astronomy in PDF only on Docsity!

UCF Physics: AST 5765/4762: (Advanced) Astronomical Data Analysis

Fall 2009 Lecture Notes: 7. Python Functions

1 Check In: 10:30 — 10:35:, 5 min

  • Do you have VNC up, python running, and the lecture demo ready to go?
  • Who has done the matplotlib tutorial? How much of pydatatut?
  • Any problems with system?
  • Examples/templates in python directory
  • HW3 is out, there is a handout called coding.pdf
  • Questions before we start?

2 Go over HW2: 10:35 — 10:55, 20 min

3 Functions in General: 10:55 — 11:10, 15 min

  • Computer programs can be very long and complicated
  • Manage complexity by “boxing away” detail in functions
  • Difference between “black box” and “glass brick”
  • A function is a program unit that can be called by name
  • Also called “routine” or “procedure” or “subprogram”, w/ nuances
  • Contains program statements
  • Executes same way each time it is called
  • Programmer says name of function rather than inserting whole block of statements in the main routine
  • Example:

def triple(x): y = 3 * x return y

  • If you now say triple(4), it prints 12
  • Variables outside function can have different names from those inside:

z = 5 triple(z) 15

  • It works, even though we used z and not x.

4 When and Why to Write Functions: 11:10 — 11:25, 15 min

  • Avoids repetition if those statements will be called more than once
  • Avoids bugs in that case
  • Sure it will run same way each time with same inputs
  • Generally functions have few inputs and outputs
  • Functions may have many intermediate variables
  • Often write functions that will be called only once
  • This simplifies code, like an outline
  • If a function calculates something, it may return it to the caller
  • If so, we say the function returns a value
  • sin(23.4), for example, has a numerical value
  • You can put such a function anywhere that type of value can be used
  • For example, you can say x = sin(cos(x)), just as in math notation

5 Python Functions: 11:25 — 11:40, 15 min

  • Start with “def”
  • Arguments in parens, comma-separated
  • Optional arguments have assignments
  • Statements are indented
  • Non-paren continued lines have \
  • Have return values (even if None)
  • print max.py example
  • Docstring
  • Documentation with example.py (see python/websources)

- Knowing subject can be hard! (algorithms and data can be complex) - Recipients: computer, you (sooner &later), colleagues, students (examples) - Must make all happy!

  • Documentation with template.py
  • look at goodcode.pro, badcode.pro (using IDL code because IDL lets you not indent) - check input values - documentation - no gotos, no globals - indent for structure and readability - use horizontal and vertical spaces for clarity and ease of verification - Try not to hard-code values except 0 and 1! Put at top in variables if necessary, or pass into functions. (There are limits to this.) - Query data for items like image sizes that vary from one analysis to another, or pass them if necessary
  • Document it if you modify an input (ok for arrays, otherwise avoid)