Skip to content

Instantly share code, notes, and snippets.

@phoikoi
Last active August 29, 2015 14:03
Show Gist options
  • Save phoikoi/8028114a371dcddb8dbb to your computer and use it in GitHub Desktop.
Save phoikoi/8028114a371dcddb8dbb to your computer and use it in GitHub Desktop.
pytut2
import sys
def a_function(multiply): # multiply should be True or False
if multiply: # the caller has asked for a multiplier function
def another_func(x): # define a new multiplier function (object)
return x*x
else: # the caller has asked for a non-multiplier function
def another_func(x): # define a new multiplier function (object)
return x+x
another_func.cheese = "yummy" # Functions are real objects, so they can have arbitrary properties
return another_func # return the newly created function object
fizzes = ['fizz', 'cat', 'fizz']
print len([x for x in fizzes if x=='fizz'])
double = lambda n: n*2 # Quick way to make a 1-argument, 1-result function
triple = lambda n: n*3
x = "string1"
x = x + "string2"
x = 3
"abcdef"[::-1] # reverse copy, result is "fedcba"
'string %s' % ("hello",)
'string {0}'.format("hello")
a_list = [1,2,3]
a_dictionary = {
'a': True,
'b': 2,
50: 'c', # integers are immutable, legal
False: "another value", # booleans are immutable, legal
(1,2): "tuple value", # tuples are immutable, this is legal
[1,2]: "list value", # lists are not immutable, this is illegal
}
costs = {
'Charlotte': 183,
'Tampa': 220,
'Pittsburgh': 222,
'Los Angeles': 475,
}
def planeRideCost(city):
if city in costs:
return costs[city]
def rentalCarCost(days):
total = days * 40
if days>7:
return totol - 50
elif days>3:
return total - 20
else:
return total
if __name__ == '__main__':
f = a_function()
f()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment