Skip to content

Instantly share code, notes, and snippets.

@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
@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 / 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: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 / 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: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:d4a08365e7d711fa56b7
Last active August 29, 2015 14:03
Learning Python: Generic import, Function Imports and Universal Imports
"""
Here is a Python module named math that includes a number of useful variables and functions,
and sqrt() is one of those functions. In order to access math, all you need is the import keyword.
"""
#Generic import: Ask Python to print sqrt(25)
import math
print math.sqrt(25)
@stenof
stenof / gist:ee2760c6493c0ccd06f7
Last active August 29, 2015 14:03
Learning Python: what is available in the math module
import math # Imports the math module
everything = dir(math) # Sets everything to a list of things from math
print everything # Prints 'em all!
@stenof
stenof / gist:0a98f30f68753b56a5c2
Last active August 29, 2015 14:03
Learning Python: Functions example
## Don't forget the colon at the end of the function definition!
def shut_down(s):
if s=="yes":
return("Shutting down")
elif s=="no":
return("Shutdown aborted")
@stenof
stenof / gist:206c56d331dad744c013
Last active August 29, 2015 14:05
Learning python: Function example 2
# What if we went to Los Angeles for 5 days and brought an extra 600 dollars of spending money?
def hotel_cost(nights):
return 140 * nights
def plane_ride_cost(city):
if city == "Charlotte":
return (183)