Contents
This notebook is based on "Think Python, 2Ed" by Allen B. Downey
https://greenteapress.com/wp/think-python-2e/
In [1]:
type( 32 )
Out[1]:
typeint, which is short for integerint For integer datafloat For data with fractional parts expressed with a decimal point
In [2]:
# Convert an int to a float
float( 42 )
Out[2]:
In [3]:
# Convert a float to an int
int( 3.14159 )
Out[3]:
float to an int can result in the loss of datastring data is different
In [4]:
# Convert an int to a string
str( 42 )
Out[4]:
In [5]:
# Convert a float to a string
str( 3.14159 )
Out[5]:
'42' consists of two characters: 4 and 2''string in a mathematical expression, it must be converted to a numeric datatype
In [6]:
int( '42' )
Out[6]:
math moduleimport it into your code
In [7]:
import math
In [8]:
radians = math.pi / 2
height = math.sin( radians )
In [9]:
# Define the radius
radius = 3
# Calculate the circumference
circumference = 2 * radius * math.pi
print( circumference )
In [10]:
def print_star_wars_greeting():
print( 'May the force be with you' )
print( 'And also with you' )
def is a keyword that tells Python you are defininig a functiondef:TAB
In [11]:
print_star_wars_greeting()
In [12]:
def print_we_will():
print( 'We will' )
def print_lyrics():
print_we_will()
print_we_will()
print( 'Rock you' )
print_lyrics()
print_lyrics() to the topprint_we_will() in between the other two functions
In [13]:
def greet( name ):
print( 'Hello', name )
print( 'How are you doing?' )
greet( 'Boba Fett' )
robot = 'Vincent'
greet( robot )
name is the function's parameter
In [14]:
greet( 2 * 'Yo ' + 'Ma' )
z outside of the function, Python will fail to compile the code and generate an error
In [15]:
def do_something( x, y ):
z = x * y
print( 'z=[' + str(z) + ']' )
do_something( 3, 4 )
# print( z ) # Fails since z doesn't exist
x and y don't exist outside of the function
In [16]:
def do_something_else( x, y ):
x = x + 2
y = y - 1
z = x * y
print( 'Inside: x=[' + str( x ) + ']' )
print( 'Inside: y=[' + str( y ) + ']' )
print( 'Inside: z=[' + str(z) + ']' )
x = 3
y = 4
do_something_else( x, y )
print( 'Outside: x=[' + str( x ) + ']' )
print( 'Outside: y=[' + str( y ) + ']' )
__main__
In [17]:
def calculate_area_circle( radius ):
return math.pi * radius ** 2
def calculate_area_donut( radius_outer, radius_inner ):
area_outer = calculate_area_circle( radius_outer )
area_inner = calculate_area_circle( radius_inner )
return area_outer - area_inner
print( calculate_area_donut( 3, 2 ) )
None'None'There are a number of benefits of functions:
In [18]:
x = 55
print( 'x=[' + str( x ) + ']' )
In [19]:
def calc_circle_circumference( radius ):
# INSERT YOUR CODE HERE
return 0
def calc_circle_area( radius ):
# INSERT YOUR CODE HERE
return 0
In [20]:
def convert_fahrenheit_to_celsius_print( f_temp ):
# INSERT YOUR CODE HERE
print( '0' )
def convert_fahrenheit_to_celsius_return( f_temp ):
# INSERT YOUR CODE HERE
return 0
calculate_profit that takes a proposed price for his app and returns the predicted profit earned. You may assume a simple linear relationship between the purchase price and the number of purchases.| Purchase price | Number of purchases |
|---|---|
| \$2.00 | 1,000 |
| \$3.00 | 700 |
| Known costs | |
|---|---|
| Refund rate | 5\% |
| Transaction cost | \$0.50 |
In [21]:
# Function to calculate the profit
def calculate_profit( app_price ):
# ????
# Profit!
return 0