
|
Lesson 5 Notes: Pre-Made Functions
- So, whats a function?
- Function = defined set of instructions
- Set of code that you can use over and over
- May be created by you or imported from module
- Example
- Math calculations (log, sin, cos, tan, exp, hypot)
- Randomization, inputs, changing strings
- User made code to recycle
- Using Ready-Made Functions
- Find out what is available
- Import appropriate module
- Use function call to use function
- module_name.function(arguments)
- Making a Function Call
- import math
- math.hypot(8,9)
- math Module that contains math functions, including hypot() function to find the length of a hypotenuse.
- . Separates module from function
- hypot Name of function
- () Place for arguments. Required for all function calls, whether there are arguments or not.
- 8,9 Numbers being sent to function for processing. In this example, 8 and 9 are the length of the two legs of a triangle and the function will use the Euclidian formula to find the third.
- Look Familiar?
- input() is a built-in function
- input is the name of the function
- input() accepts a string in the () which it will display as the prompt
- Raw_input() is another built-in function
- raw_input() is the name of the function
- raw_input accepts a string in the () which it will display as the prompt
- For Best Results
- Some functions are built-in and do not need a module imported.
- Some functions take arguments in (), others do not, but all need the ().
- If you arent sure what goes in the (), type the first ( in Idle and wait a few seconds. A tip will show up to remind you.
- Functions can take variables as arguments
- Some Built In Functions
- float() Converts a string or other number to a floating point number
- int() Converts a string or other number to a plain integer.
- long() Converts a string or other number into a long number.
- str() Converts a number into a string
- Some More Built In Functions
- divmod(x,y) Returns the result of x/y and x%y
- len(s) Returns the length a string or other data type
- pow(x,y) Returns x to the power y
- range(start,stop,step) Returns a set of numbers from start to stop, counting by step.
- Round(x, n) Return the floating point value x rounded to n digits after the decimal point. If n is omitted, it defaults to zero.
- Some Math Module Functions
- acos(x), asin(x),atan(x) arc cosine, arc sine, and arc tangent of number.
- cos(x), sin(x), tan(x) cosine, sine, and tangent of number.
- log(x), log10(x) Natural log and base 10 log
- pow(x,y) X raised to the power Y
- hypot(x,y) Returns length of hypotenuse for triangle with sides of length x and y.
- Some String Module Functions
- capitalize() capitalize first word
- capwords() capitalize all words
- lower() all letters lower case
- upper() all letters upper case
- replace(string, old, new) replace all old words in string with new words
- center(string, width), rjust(string, width), ljust(string, width) center, right justify, or left justify the string in a field of a given width.
- Some Random Module Functions
- randrange(start,stop,step) get a random number between the start number and stop number. Step is optional.
- choice(sequence) randomly select an object in the sequence.
- Some Other Modules and Functions
- Time
- sleep(x) have computer sleep or pause for a given number of seconds.
- Calendar
- prcal(year) Print calendar for year
- prmonth (year,month) Print calendar for month
- Review
- What are functions?
- What are modules?
- What must you know about a function before you use it?
- What is the correct way to write a function call?
- Give an example of a pre-built function.
Restricted access |