Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
18 Cards in this Set
- Front
- Back
function |
A named sequence of statements that performs a computation. When you define a function you specify the name and the sequence of statements. Later you "call" the function by name, function call. |
|
function example: >>> type(32) <type 'int'> |
The name of the function is type. The expression in the parentheses is called the argument of the function. The argument is a value or variable that we are passing into the function as input to the function. |
|
return value |
It is common to say that a function "takes" an argument and "returns" a result. |
|
max and min functions |
Give us the largest and smallest values in a list. >>> max('Hello world') 'w' >>> min('Hello world') ' ' >>> |
|
function len() |
Built in Python function that tells us how many items are in its argument. >>> len('Hello world') 11 >>> |
|
function int() |
Converts to integer if it can, or returns an error if it can't. >>> int('32') |
|
>>> int(3.99) |
Would return: 3 It does not round, it just drops fraction part. |
|
function float() |
Converts integers and strings to floating-point numbers: >>> float(32) |
|
function str() |
Converts its argument to a string. >>> str(32) |
|
function random() |
Returns a random float between 0.0 and 1.0 including 0.0 and up to but not including 1.0. |
|
function random.randint(5, 10) |
The function randint takes parameters low and high and returns an interger between low and high (including both). |
|
function t = [1, 2, 3] random.choice(t) |
To choose an element from a sequence at random. Would return 1 2 or 3. |
|
function definition |
Specifies the name of a new |
|
function definition example def print_lyrics(): |
def is a keyword that indicates that this is a function definition. The name of the function is print_lyrics. The rules for function names are the same as Empty parentheses after the name indicates that it does not take any arguments. The first line of the function definition is called the header, the rest is called the body.The header has to end with a colon and the body has to be indented.
Defining a function creates a variable with the same name. |
|
Flow of execution |
Execution always begins at the first statement of the program. Statements are executed one at a time, in order from top to bottom. Functions definitions do not alter the flow of execution.
A function call is like a detour, instead of going to the next statement, the flow jumps to the body of the function, executes the statements there, than comes back to pick up after function call. |
|
fruitful functions |
Yield results like math functions. |
|
void functions |
Perform action but don't return a value, like print_twice
def print_twice(bruce): >>> print_twice('Spam')
Void functions might display something on the screen or have some other effect, |
|
return |
To return a result from a function, we use the return statement in the function. Example:
def addtwo(a, b):
x = addtwo(3, 5)
It would return "8"
|