import math

math.sqrt(25)

math.log(32,2)

math.log(32.0, 10)

math.floor(math.log(32.0, math.e))

math.factorial(10)

math.cos(math.pi)


### Creating new functions in Python

def fname (p1, p2, ..., pN):

  statement1

   statement2

• Indent each statement the same amount For function to return a value, include return answer
• where answer is the value the function produces
• If no return statement, the function produces None

## Design Recipe

### Purpose statement:

• Explicitly indicate what the function does, including how the parameters are used

### Contract

• Types of consumed and produced values
• Include any needed requirements on consumed
• Most type names are the same as in Racket, except for Num; Use Nat, Int, Float as appropriate

## Testing in Python

Put a copy in the same folder as your assignment .py files for each assignment.

Add the following line to each assignment file:
import check
You do NOT need to submit check.py when you submit your assignment files.

### check.expect

## Question 1, Test 1: description check.expect( “Q1T1”, expr, value_expected)

• This function performs the test: Does expr exactly equal value_expected?
• Use for checking exact values (integer or strings).

### check.within

## Question 2, Test 2: description check.within( “Q2T2”, expr, value_expected, tolerance) • This function performs the test: abs(expr – value_expected) <= tolerance • Use for checking inexact values (floating point numbers only).

Example of check.expect and check.within:

## Test 1: middle is first value

check.expect("Q1T1",middle(3,10,1),3)


## Q2, Test 2: positive radius (1.0)

check.within("Q2T2", area_circle(1.0), 3.14159, 0.00001)


## More Casting and Conversion Functions

float: Int -> Float
float(1) => 1.0
float(10) => 10.0

float: Str -> Float
float(“34.1”) => 34.1
float(“2.7.2”) => Error
float(“23”) => 23.0

int: (anyof Float Str Int)-> Int
int(4.7) => 4
int(3.0/4) => 0
int(-12.4) => -12

This is a truncation operation (not rounding)
int(“23”) => 23
int(“2.3”) => Error

str: (anyof Int Float Str) -> Str
str(3) => “3”
str(42.9) => “42.9”