python functions : chapter 3

10
PYTHON FUNCTIONS : CHAPTER 3 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST

Upload: rupert

Post on 21-Mar-2016

84 views

Category:

Documents


0 download

DESCRIPTION

Python Functions : chapter 3. From Think Python How to Think Like a Computer Scientist. Type Conversion Functions. A function in Python is a named sequence of instructions that perform a computation. When a function is called it returns the result of this computation. >>> type ( 23.4) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Python Functions : chapter 3

PYTHONFUNCTIONS : CHAPTER 3

FROMTHINK PYTHONHOW TO THINK LIKE A COMPUTER SCIENTIST

Page 2: Python Functions : chapter 3

TYPE CONVERSION FUNCTIONSA function in Python is a named sequence of instructions that perform a computation. When a function is called it returns the result of this computation.>>> type ( 23.4)<class ‘float’)> returned value>>> int(23.4)23 returned value is now an integer>>>float(45)45.0 returns a float>>>str(456)‘456’

Page 3: Python Functions : chapter 3

MATHEMATICAL FUNCTIONS>>> import math>>> math.sqrt(45)6.708203932499369>>> r=5>>>3/4.0*math.pi*r**3294.5243112740431>>> math.log(100,2) log of 100 base 26.643856189774725>>> math.sin(math.pi)1.2246467991473532e-16 what in the world is this?

Study the trig example in exercise 3

Page 4: Python Functions : chapter 3

NEW FUNCTIONS YOU WRITEType in the following script within the editor and run itimport mathdef vol_of_sphere(radius): : is REQUIRED vol = 4/3.0*math.pi*radius**3 indention is required return volprint vol_of_sphere(5)

>>> 523.598775598 output that you’ve seen before>>>

Page 5: Python Functions : chapter 3

FUNCTION PARTS

def vol_of_sphere(radius): header vol = 4/3.0*math.pi*radius**3 return vol

Argument(sent in)radius is called a parameter.

Function definition

Value to return

body

Use four spaces for the indention

Page 6: Python Functions : chapter 3

FUNCTION TO FIND THE AREA OF A TRIANGLESee Herons formula (http://mathworld.wolfram.com/HeronsFormula.html)# Herons formula as a scriptimport mathdef area(a, b, c): “”” a, b and c are the sides of a triangle “”” function info s = 1/2.0*(a+b+c) a = math.sqrt(s*(s-a)*(s-b)*(s-c)) return aprint area(1,1,1)>>> 0.433012701892>>>

Page 7: Python Functions : chapter 3

FUNCTION FLOW OF EXECUTIONInstInstInstInstprint area(5,5,6)InstInstInst

def area(a, b, c): s = 1/2.0*(a+b+c) a = math.sqrt(s*(s-a)*(s-b)*(s-c)) return a

Be sure and define your functions before you use them!

Page 8: Python Functions : chapter 3

FUNCTION VARIABLES AND PARAMETERS ARE LOCALimport math#Define the function heredef area(a, b, c): """ a, b and c are the sides of a triangle """ s = 1/2.0*(a+b+c) area = math.sqrt(s*(s-a)*(s-b)*(s-c)) return area

#The linear part of this program starts heres = 25a=2print area(1,1,1)print s,a

#here is the output>>> 0.43301270189225 2>>>

Page 9: Python Functions : chapter 3

IN CLASS PROBLEM

1. Write a function called FtoC that will convert Fahrenheit to Celsius. You will need a single parameter. Run the program to test it with some know values. C = 5/9(F -32) F=9/5C + 32

2. Write a function to return the volume of a sphere of radius r

Page 10: Python Functions : chapter 3

WHY FUNCTIONS?• Lets you name a group of statements, which makes your

program easier to read.• Functions make programs smaller by being able to reuse

the same code.• Dividing programs into functions allow you to debug

sections at a time.• Well defined functions can be used in other programs that

you write.####################################################Note: if you do this from math import * instead of import math you will not need to include the math. prefix