Determine $r_{p}$ for $a = 1$, $b=4$, and $c=3$.
In [2]:
## Code here
import math
(-4 + math.sqrt(4**2 - 4*1*3))/(2*1)
Out[2]:
What about for $a = 2$, $b = 8$, and $c = 1$?
In [7]:
## Code here
# gonna make this saner
a = 2
b = 8
c = 1
(-b + math.sqrt(b**2 - 4*a*c))/(2*a)
Out[7]:
In [5]:
def square(x):
"""
This function will square x.
"""
return x*x
s = square(5)
help(square)
In [ ]:
import math
def hypotenuse(y,theta):
"""
Return a hypotenuse given y and theta in radians.
"""
return math.sin(theta)*y
h = hypotenuse(1,math.pi/2)
print(h)
def FUNCTION_NAME(ARGUMENT_1,ARGUMENT_2,...,ARGUMENT_N):
"""
Description of function.
"""
do_stuff
do_other_stuff
do_more_stuff
return VALUE_1, VALUE_2, ... VALUE_N
In [ ]:
def some_function(ARGUMENT):
"""
Print out ARGUMENT.
"""
return 1
print(ARGUMENT)
some_function(10)
some_function("test")
You get information into the function via arguments defined in the function name.
In [ ]:
def some_function(a):
"""
print out a
"""
print(a)
In [ ]:
def some_function(a,b):
"""
print out a and b
"""
print(a,b)
In [ ]:
def some_function(a,b=5,c=7):
"""
Print a and b.
"""
print(a,b,c)
some_function(1,c=2)
some_function(1,2)
some_function(a=5,b=4)
b=5 allows you to define a default value for the argument.
In [ ]:
def some_function(a):
"""
Multiply a by 5.
"""
return a*5
print(some_function(2))
print(some_function(80.5))
x = some_function(5)
print(x)
By putting return at the end of a function, you can capture output.
In [ ]:
def some_function(a,b):
"""
Sum up a and b.
"""
v = a + b
return v
v = some_function(1,2)
print(v)
In [ ]:
def some_function(a,b):
"""
Sum up a and b.
"""
v = a + b
return v
To return multiple values, use commas:
In [ ]:
def some_function(a):
"""
Multiply a by 5 and 2.
"""
return a*5, a*2
x, y = some_function(5)
print(x)
In [8]:
## Code here
def get_root(a,b,c):
return (-b + math.sqrt(b**2 - 4*a*c))/(2*a)
print(get_root(1,4,3))
print(get_root(2,8,1))