UCSD Basic Python Worksheet
Description
In this exercise you will be defining several different functions
- For each function below, a) define the function, then b) evaluate and print out its return value to test that it gives the correct output. There are also some additional constraints:
- All variable and parameter names should use
snake_case
for names with multiple words. - All
print()
ing should happen outside the functions.
- All variable and parameter names should use
Exercise Functions
-
bool_to_int
Define a
lambda
function and assign it to thebool_to_int
variable. This function should have one parameter namedvalue
, which will receive a boolean argument. The function should convert that boolean argument to an integer and return it.Examples:
bool_to_int(True) # returns 1
bool_to_int(False) # returns 0
-
get_smaller
Define a
lambda
function and assign it to theget_smaller
variable. This function should have two parameters nameda
andb
, which will each receive a numeric argument. The function should return the smaller of those two arguments.Examples:
get_smaller(16, 31) # returns 16
get_smaller(253, 223) # returns 223
-
cube
Define a
def
function namedcube
. This function should have one parameter namedbase
, which will receive a numeric argument. The function should return the value of the argument raised to the 3rd power. Call it with positional arguments only.Examples:
cube(2) # returns 8
cube(5) # returns 125
-
absolute_difference
Define a
def
function namedabsolute_difference
. This function should have two parameters nameda
andb
, which will each receive a numeric argument. The function should return the absolute value of the difference of the arguments. Call it with positional arguments only.Examples:
absolute_difference(14, 11) # returns 3
absolute_difference(13, 40) # returns 27
-
squared_difference
Define a
def
function namedsquared_difference
. This function should have two parameters nameda
andb
, which will each receive a numeric argument. The function should return the square of the difference of the arguments. Call it with positional arguments only.Examples:
squared_difference(14, 11) # returns 9
squared_difference(13, 40) # returns 729
-
hours_to_minutes
Define a
def
function namedhours_to_minutes
. This function should have one parameter namedhours
, which will receive a numeric argument. The function should return the number of minutes equal to argument’s number of hours. Call it with keyword arguments only.Examples:
hours_to_minutes(hours = 3.5) # returns 210.0
hours_to_minutes(hours = 12) # returns 720
-
get_circumference
Define a
def
function namedget_circumference
. This function should have one parameter namedradius
, which will receive a numeric argument. The function should return the circumference of a circle with that radius. Call it with keyword arguments only.Examples:
get_circumference(radius = 1) # returns 6.283185307179586
get_circumference(radius = 9.2) # returns 57.8053048260521912
-
linear_transform
Define a
def
function namedlinear_transform
. This function should have three parameters namedx
,slope
, andintercept
; which will each receive a numeric argument. The function should return the vertical position of a straight line with slopeslope
and vertical-axis-inteceptintercept
at horizontal positionx
(in other words, it should evaluate a linear equation). Call it with keyword arguments only, and use a different argument order with each function call.Examples:
linear_transform(x=5.0, slope=3.0, intercept=-8.5) # returns 6.5
linear_transform(slope=-2.1, intercept=17.0, x=2.5) # returns 11.75
-
standardize
Define a
def
function namedstandardize
. This function should have three parameters namedx
,x_center
, andscale_size
; which will each receive a numeric argument, andscale_size
will always be positive. The function should return the difference betweenx
andx_center
, divided by thescale_size
. Call it with keyword arguments only, and use a different argument order with each function call.Examples:
standardize(x=8.2, x_center=13.8, scale_size=4.83) # returns -1.1594202898550727
standardize(scale_size=24.63, x=2.89, x_center=-72.813) # returns 3.073609419407227
Have a similar assignment? "Place an order for your assignment and have exceptional work written by our team of experts, guaranteeing you A results."