Lecture Notes on Python Math Module and Fruitful Functions (CPSC 121, Fall 2011), Study notes of Computer Science

These lecture notes cover the use of the math module in python, focusing on various mathematical functions and constants. Additionally, the concept of 'fruitful' versus 'void' functions is explained, with examples and exercises provided for better understanding.

Typology: Study notes

2012/2013

Uploaded on 09/28/2013

noob
noob 🇮🇳

4.4

(25)

105 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture Notes CPSC 121 (Fall 2011)
Today’s Topics ...
Math module
Fruitful functions
Homework
HW 4 due
HW 5 out
S. Bowers 1 of 6
pf3
pf4
pf5

Partial preview of the text

Download Lecture Notes on Python Math Module and Fruitful Functions (CPSC 121, Fall 2011) and more Study notes Computer Science in PDF only on Docsity!

Today’s Topics ...

  • Math module
  • Fruitful functions

Homework

  • HW 4 due
  • HW 5 out

The math module

The math module contains many often used mathematical functions

import math help(math) # to browse functions available math.ceil(3.9999) # round up to next highest int 4 math.floor(3.9999) # round down to next lowest int 3 math.hypot(3, 4) # square root of a2 + b

math.log10(100) # the log base 10

math.log(16, 2) # the log base 2

math.sqrt(25) # the square root

  • and so on ...
  • The math module also includes math.pi and math.e constants

To round a float, use the built-in round function

round(3.2) 3 round(3.6) 4

When importing modules ...

  • Function calls are prefixed with module name
  • E.g., math.sqrt(25) instead of just sqrt(25)
  • Can use “from math import *” instead to avoid this

Void functions actually return the special None value

result = print("Hi") Hi print(result) None type(result) <type ’NoneType’>

Defining fruitful functions

Return values using the return keyword

def succ(x): x = x + 1 return x

succ(1) 2 y = succ(2) print(y) 3

  • A function can return at most one value
  • A return statement can occur anywhere in the function

... but no statements after it is called will be executed

Q: What is returned?

z = 1 def f(x, y, z): result = x + y return result new_result = result + z return new_result