• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/18

Click to flip

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')
32
>>> int('Hello')
ValueError: invalid literal for int(): Hello

>>> 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)
32.0
>>> float('3.14159')
3.14159

function


str()

Converts its argument to a string.


>>> str(32)
'32'
>>> str(3.14159)
'3.14159'

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 and the sequence of statements that execute when the function is called.
Once we define a function, we can reuse the function over and over throughout
our program.

function definition example


def print_lyrics():
print "I'm a lumberjack, and I'm okay."
print 'I sleep all night and I work all day.'

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
for variable names: letters, numbers and some punctuation marks are legal, but
the first character can’t be a number. You can’t use a keyword as the name of a
function, and you should avoid having a variable and a function with the same
name.


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 bruce
print bruce


>>> print_twice('Spam')
Spam
Spam



Void functions might display something on the screen or have some other effect,
but they don’t have a return value. If you try to assign the result to a variable, you
get a special value called None.
>>> result = print_twice('Bing')
Bing
Bing
>>> print result
None

return

To return a result from a function, we use the return statement in the function. Example:



def addtwo(a, b):
added = a + b
return added



x = addtwo(3, 5)
print x



It would return "8"