Functions

Lesson 01/19 – What Good are Functions?!

 
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)

Lesson 02/19 – Function Junction

 
# 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"""
    print "Eggs!"





# Define the spam function above this line.
spam()

Lesson 03/19 – Call and Response

 
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.
square(10)

Lesson 04/19 – Parameters and Arguments

 
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!

Lesson 05/19 – Functions Calling Functions

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

Lesson 06/19 – Practice Makes Perfect

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

Lesson 07/19 – I Know Kung Fu

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

print sqrt(25)

Lesson 08/19 – Generic Imports

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

Lesson 09/19 – Function Imports

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

from math import sqrt

Lesson 10/19 – Universal Imports

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

from math import *

Lesson 11/19 – Here Be Dragons

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

Lesson 12/19 – On Beyond Strings

 
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)

Lesson 13/19 – max()

 
# Set maximum to the max value of any set of numbers on line 3!

maximum = max(2, 5)

print maximum

Lesson 14/19 – min()

 
# Set minimum to the min value of any set of numbers on line 3!

minimum = min(12, 256, 568161)

print minimum

Lesson 15/19 – abs()

 

absolute = abs(-42)

print absolute

Lesson 16/19 – type()

 

# Print out the types of an integer, a float,
# and a string on separate lines below.
print type(42)
print type(4.2)
print type("spam")

Lesson 17/19 – Review: Functions

 

def shut_down (s):
    if s.upper() == "YES":
        return "Shutting down..."
    elif s.upper() == "NO":
        return "Shutdown aborted!"
    else:
        return "Sorry, I didn't understand you."

Lesson 18/19 – Review: Modules

 

import math
print math.sqrt(13689)

Lesson 19/19 – Review: Built-In Functions

 

def distance_from_zero(x):
    if type (x) == int:
        return abs(x)
    elif type(x) == float:
        return abs(x)
    else:
        return "Not an integer or float!"