Main.Functions History
Hide minor edits - Show changes to markup
January 26, 2010, at 12:47 PM MST
by -
Added lines 1-57:
(:source lang=python:)
"""Using some of python's built in functions, the math module, and our celsius to fahrenheit conversion function """
- Most functions have arguments that provide the function data on
- which to work
- Example: the absolute value function:
print "the absolute value of 5 is", abs(5) print "the absolute value of -5 is", abs(-5)
- notice that this function RETURNS a value.
- you can reassign that value to a variable:
a = abs(-5)
- some functions take more than one argument:
print pow(2, 3)
- pow is a more flexible version of the exponentiation operator
- the max function returns the maximum number among its arguments
print max(1, 10)
- it can take as many arguments as you want
print max(1, 10, 3)
- let's use the celsius2fahrenheit function we wrote
- to do that we first need to IMPORT the module in which it exists:
import celsius2fahrenheit
- now we can access the function using dot notation
celsius2fahrenheit.celsius2fahrenheit(21)
import math
- the math module is described at
- http://docs.python.org/library/math.html
- it provides all kinds of mathematical functions for example:
- sqrt
print math.sqrt(9) print math.log10(1000)
- it also includes constants like e and pi
print math.pi print math.e
- the sys module is a module that provides system specific functions
- and constants. see http://docs.python.org/library/sys.html
import sys
- sys.path is the set of directories where the interpreter
- looks for modules
print sys.path
(:sourceend:)
