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  

Design Recipe

Purpose statement:

Contract

Testing in Python

Download the file: check.py from the CS116 web pages.
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)

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)  

Note: You should now include your examples with your tests.

## 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”