def f(x):
return x**2 - x + 1
def g(x, y):
return x**3 - y**2
>>> def f(x): return x**2 - x + 1 >>> f <function f at 0x114c14f30>
math module.
int, which takes some object and returns the equivalent integer, if this is possible.
s and returns a string consisting of s followed by a random integer between 0 and 99.
>>> def f(x):
return x**2 - x + 1
>>> def g(x, y):
return x**3 - y**2
>>> f(-2)
7
>>> g(-2, 1)
-9
>>> f(f(f(2))) 43 >>> g(f(-1), f(-2)) -22
g, that is, f(-1), evaluates to 3, and the parameter x in the definition of g gets bound to this value.
f and g have parameters called x.
This is not a problem because the parameter only has a value during the function call; once
the function returns a value the parameters of the function are no longer bound to anything.
_.
def print_name_ss(name, ss):
print('Name:', name, 'SS#:', ss)
>>> print_name_ss('John Brown', 555609843)
Name: John Brown SS#: 555609843
def names2ways(first, middle, last):
return first + ' ' + middle + ' ' + last, last + ', ' + first + ' ' + middle
>>> names2ways('John', 'Q.', 'Jones')
('John Q. Jones', 'Jones, John Q.')
>>> jqj = names2ways('John', 'Q.', 'Jones')
>>> jqj[0]
'John Q. Jones'
>>> jqj[1]
'Jones, John Q.'
>>> import random >>> random.random() 0.62219559535873636 >>> random.random <built-in method random of Random object at 0x92ae10>
type, which returns the type of its single argument.type applied to some object) is also a function.
These functions convert their single argument to that type, if this is possible.
>>> int('421')
421
>>> str(421)
'421'
>>> float(421)
421.0
>>> float('spam')
Traceback (most recent call last):
File "<pyshell#3>", line 1, in
float('spam')
ValueError: invalid literal for float(): spam
help (like print) is an example of a function that can take different numbers of arguments.
help starts up the interactive help system.help prints out information about the function, module, etc.
that has that name if something does.input is a function with no arguments that waits for user input and then returns this as a string, once it is entered. (It can also take a string argument, which behaves like a prompt for the user.)
len takes a string (or another kind of sequence) and returns its length.
def print_name_info(first, middle, last):
name = first + ' ' + middle + ' ' + last
print('Full name:', name, '# characters:', len(name))
>>> print_name_info('John', 'Q.', 'Smith')
Full name: John Q. Smith # characters: 13
>>> name
...
NameError: name 'name' is not defined

>>> move_x(100, 50, 45) 135 >>> move_x(100, 50, 180) 50 >>> move_x(100, 50, 300) 125
>>> 'Expecting the world to treat you fairly because you are good ' + \
'is like expecting the bull not to charge because you are a vegetarian.'
'Expecting the world to treat you fairly because you are good is like expecting the bull not to charge because you are a vegetarian.'
>>> input('A number? '); input('Try again? ')
A number? 11
'11'
Try again? no
'no'
import math
def sigmoid(x):
"""Squash x to a value between 0 and 1."""
return 1 / (1 + math.exp(-x))
def func_twice(func, arg1, arg2):
"""Print out the result of applying func to arg1 and then arg2."""
print(func(arg1))
print(func(arg2))
>>> func_twice(sigmoid, 1, 2)
0.73105857863
0.880797077978
help function.
>>> help(sigmoid)
Help on function sigmoid in module __main__:
sigmoid(x)
Squash x to a value between 0 and 1.
>>> help(func_twice)
Help on function func_twice in module __main__:
func_twice(func, arg1, arg2)
Print out the result of applying func to arg1 and then arg2.
Answers to problems in these notes