Codecademy Python Answers Functions


Functions 1/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)

Functions 2/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():
    """hello friends this is for the function """
    print "Eggs!"

# Define the spam function above this line.

Functions 3/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.

Functions 4/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!

Functions 5/19 – Functions Calling Functions

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

Functions 6/19 – Practice Makes Perfect

def cube(number):
    return number**3
def by_three(number):
    if(number%3 == 0):
        return cube(number)
        return False

Functions 7/19 – I know Kungfu

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

print sqrt(25)

Functions 8/19 – Generic Imports

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

Functions 9/19 – Function Imports

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

from math import sqrt
print math.sqrt(25)

Functions 10/19 – Universal Imports

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

from math import *
print sqrt(25)

Functions 11/19 – Here Be Dangerous

from datetime import datetime
print dir(datetime)

Functions 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)

Functions 13/19 – Max()

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

maximum = max(10,10.5,20.0,300)

print maximum

Functions 14/19 – Min()

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

minimum = min(-0.8,-1,-10,10)

print minimum

Functions 15/19 – abs()

absolute = abs(-42)

print absolute

Functions 16/19 – type()

# Print out the types of an integer, a float,
# and a string on separate lines below.
print type(20.2)
print type(20)
print type('prathap')

Functions 17/19 – Review:Functions

def shut_down(s):
    if s == "yes":
        return "shutting down"
    elif s == "no":
        return "shutdown aborted"
        return "sorry"

Functions 18/19 – Review:Modules

import math
print math.sqrt(13689)

Functions 19/19 – Review: Built-in Functions

def distance_from_zero(distance):
    if type(distance) == int or type(distance)==float:
        return abs(distance)
        return "Nope"

Leave a Reply

Your email address will not be published. Required fields are marked *