Lesson 01/19 – What Good are Functions?!
[code lang=”python”]
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)
[/code]
Lesson 02/19 – Function Junction
[code lang=”python”]
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()
[/code]
Lesson 03/19 – Call and Response
[code lang=”python”]
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)
[/code]
Lesson 04/19 – Parameters and Arguments
[code lang=”python”]
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!
[/code]
Lesson 05/19 – Functions Calling Functions
[code lang=”python”]
def one_good_turn(n):
return n + 1
def deserves_another(n):
return one_good_turn(n) + 2
[/code]
Lesson 06/19 – Practice Makes Perfect
[code lang=”python”] def cube(n):
return n ** 3
def by_three(n):
if n % 3 == 0:
return cube(n)
else:
return "False"
by_three(9)
[/code]
Lesson 07/19 – I Know Kung Fu
[code lang=”python”]
Ask Python to print sqrt(25) on line 3.
print sqrt(25)
[/code]
Lesson 08/19 – Generic Imports
[code lang=”python”]
Ask Python to print sqrt(25) on line 3.
import math
print math.sqrt(25)
[/code]
Lesson 09/19 – Function Imports
[code lang=”python”]
Import just the sqrt function from math on line 3!
from math import sqrt
[/code]
Lesson 10/19 – Universal Imports
[code lang=”python”]
Import everything from the math module on line 3!
from math import *
[/code]
Lesson 11/19 – Here Be Dragons
[code lang=”python”]
import math # Imports the math module
everything = dir(math) # Sets everything to a list of things from math
print everything # Prints ’em all!
[/code]
Lesson 12/19 – On Beyond Strings
[code lang=”python”]
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)
[/code]
Lesson 13/19 – max()
[code lang=”python”]
Set maximum to the max value of any set of numbers on line 3!
maximum = max(2, 5)
print maximum
[/code]
Lesson 14/19 – min()
[code lang=”python”]
Set minimum to the min value of any set of numbers on line 3!
minimum = min(12, 256, 568161)
print minimum
[/code]
Lesson 15/19 – abs()
[code lang=”python”]
absolute = abs(-42)
print absolute
[/code]
Lesson 16/19 – type()
[code lang=”python”]
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")
[/code]
Lesson 17/19 – Review: Functions
[code lang=”python”]
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."
[/code]
Lesson 18/19 – Review: Modules
[code lang=”python”]
import math
print math.sqrt(13689)
[/code]
Lesson 19/19 – Review: Built-In Functions
[code lang=”python”]
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!"
[/code]