This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def square(n): | |
| """Returns the square of a number.""" | |
| squared = n**2 | |
| print "%d squared is %d." % (n, squared) | |
| return squared | |
| square(10) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import math # Imports the math module | |
| everything = dir(math) # Sets everything to a list of things from math | |
| print everything # Prints 'em all! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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) |
OlderNewer