Functions

I Introduction to Functions

i. What Good are Functions? 1/19

def tax(bill):
    """Adds 8% tax to a restaurant bill."""
    bill *= 1.08
    print "With tax: %f" % bill
    return bill

def tip(bill):
    """Adds 15% tip to a restaurant bill."""
    bill *= 1.15
    print "With tip: %f" % bill
    return bill
    
meal_cost = 100
meal_with_tax = tax(meal_cost)
meal_with_tip = tip(meal_with_tax)

II Function Syntax

i. Function Junction 2/19

# Define your spam function starting on line 5. You
# can leave the code on line 11 alone for now--we'll
# explain it soon!

def spam():
    '''Prints 'Eggs' to the console'''
    print 'Eggs!'
# Define the spam function above this line.
spam()

ii. Call and Response 3/19

def square(n):
    """Returns the square of a number."""
    squared = n**2
    print "%d squared is %d." % (n, squared)
    return squared
    
# Call the square function on line 9! Make sure to
# include the number 10 between the parentheses.
print square(10)

iii. Parameters and Arguments 4/19

def power(base, exponent):  # Add your parameters here!
    result = base**exponent
    print "%d to the power of %d is %d." % (base, exponent, result)
power(37,4)  # Add your arguments here!

iv. Functions Calling Functions 5/19

def one_good_turn(n):
    return n + 1
    
def deserves_another(n):
    return one_good_turn(n) + 2

v. Practice Makes Perfect 6/19

def cube(n):
    cube = n*n*n
    return cube
def by_three(n):
    if n % 3 == 0 :
        return cube(n)
    else :
        return False
print by_three(2)

III Importing Modules
i. I Know Kung Fu 7/19

# Ask Python to print sqrt(25) on line 3.

print sqrt(25)

ii. Generic Imports 8/19

# Ask Python to print sqrt(25) on line 3.
import math
print math.sqrt(25)

iii. Function Imports 9/19

# Import *just* the sqrt function from math on line 3!

from math import sqrt

iv. Universal Imports 10/19

# Import *everything* from the math module on line 3!

from math import *

v. Here Be Dragons 11/19

import math            # Imports the math module
everything = dir(math) # Sets everything to a list of things from math
print everything       # Prints 'em all!

IV Built-In Functions

i. On Beyond Strings 12/19

def biggest_number(*args):
    print max(args)
    return max(args)
    
def smallest_number(*args):
    print min(args)
    return min(args)

def distance_from_zero(arg):
    print abs(arg)
    return abs(arg)


biggest_number(-10, -5, 5, 10)
smallest_number(-10, -5, 5, 10)
distance_from_zero(-10)

ii. max() 13/19

# Set maximum to the max value of any set of numbers on line 3!
maximum = max(3,4,5)
print maximum

iii. min() 14/19

# Set minimum to the min value of any set of numbers on line 3!
minimum = min(1.5,5,6)
print minimum

iv. abs() 15/19

absolute = abs(-42)
print absolute

v. type() 16/19

# Print out the types of an integer, a float,
# and a string on separate lines below.
print type(1.533)
print type('nonu')
print type(3)

III Review

i. Review: Functions 17/19

def shut_down(s):    
    if s == 'yes' :
           return "Shutting down"
    elif s == 'no' :
           return "Shutdown aborted"
    else :
           return "Sorry"

ii. Review: Built-In Functions 18/19

from math import sqrt
print sqrt(13689)

iii. Lambda Expressions 19/19

def distance_from_zero(s):
     if type(s) == int or type(s) == float:
         return abs(s)
     else:
         return "Nope"
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s