Skip to content

Instantly share code, notes, and snippets.

@stenof
stenof / gist:6ad72540a4ad980b2051
Created July 2, 2014 15:56
Learning Python: Practice Makes Perfect
"""
First, function called cube that takes an argument called number.
Make that function return the cube of that number.
Define a second function called by_three that takes an argument called number.
if that number is divisible by 3, by_three should call cube(number) and return its result.
Otherwise, by_three should return False.
"""
def cube(number):
return number**3
@stenof
stenof / gist:8b859371c31266783342
Last active August 29, 2015 14:03
Learning Python: Functions Calling Functions
"""
deserves_another always adds 2 to the output of one_good_turn.
"""
def one_good_turn(n):
return n + 1
def deserves_another(n):
return one_good_turn(n) + 2
@stenof
stenof / gist:ed219646791e96def4b1
Created July 2, 2014 15:34
Power function: raise the base to the power of the exponent.
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!
@stenof
stenof / square of a number
Created July 2, 2014 15:18
Learning Python: Returns the square of a number
def square(n):
"""Returns the square of a number."""
squared = n**2
print "%d squared is %d." % (n, squared)
return squared
square(10)
@stenof
stenof / gist:ecee859b90257c0c3fb9
Created July 2, 2014 15:11
Learning Python: Function Junction
"""You might have considered the situation where you would like to reuse a
piece of code, just with a few different values. Instead of rewriting the
whole code, it's much cleaner to define a function,
which can then be used repeatedly."""
""" Function Junction """
"""Functions are defined with three components:
01.
The header,
@stenof
stenof / gist:a5a0f0413435e2ea3748
Last active August 29, 2015 14:03
Learning Python: calculating tax and tip on a bill
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