Python Functions : chapter 3

Slides:



Advertisements
Similar presentations
Python Basics: Statements Expressions Loops Strings Functions.
Advertisements

1 MATH METHODS THAT RETURN VALUES. 2 JAVA'S MATH CLASS.
Order of operators: x ** y x * y, x / y, x // y, x % y x + y, x - y
Introduction to C++ Functions Chapter 6 Sections 6.1 – 6.3 Computer II.
Functions. A set of statements (lines of code) that can be run repeatedly Goals: Learning Python by Lutz and Ascher –Code reuse –Procedural decomposition.
Chapter 2 Writing Simple Programs
Introduction to Computers and Programming Lecture 13: User defined methods Instructor: Evan Korth New York University.
Functions and abstraction Michael Ernst UW CSE 190p Summer 2012.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Introduction to Python and programming Michael Ernst UW CSE 190p Summer 2012.
Recitation 1 Programming for Engineers in Python.
Munster Programming Training
Programming Training Main Points: - Python Statements - Problems with selections.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
PYTHON CONDITIONALS AND RECURSION : CHAPTER 5 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Functions and subroutines – Computer and Programming.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”
Chapter 06 (Part I) Functions and an Introduction to Recursion.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON FUNCTIONS Jehan-François Pâris
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.
Introduction to Python and programming Ruth Anderson UW CSE 140 Winter
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Python Conditionals chapter 5
Python – Part 3 Functions 1. Getting help Start the Python interpreter and type help() to start the online help utility. Or you can type help(print) to.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
INLS 560 – F UNCTIONS Instructor: Jason Carter.
17 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
4.3 Functions. Functions Last class we talked about the idea and organization of a function. Today we talk about how to program them.
Exercise 1 #include int main() { printf(“Hello C Programming!\n”); return 0; } 1.Run your Visual Studio 2008 or Create a new “project” and add.
PYTHON VARIABLES : CHAPTER 2 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
PYTHON FUNCTIONS. What you should already know Example: f(x) = 2 * x f(3) = 6 f(3)  using f() 3 is the x input parameter 6 is the output of f(3)
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return values, Math, and double reading: 3.2,
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
Python Programming Module 3 Functions Python Programming, 2/e1.
Python Functions : Chapter 3 Working wit text: Chapter 4.
Chapter 2 Writing Simple Programs
Lesson 06: Functions Class Participation: Class Chat:
Main Points: - Python Statements - Problems with selections.
Exam #1 You will have exactly 30 Mins to complete the exam.
Introduction to Programming
Introduction to Python
COMPSCI 107 Computer Science Fundamentals
Introduction to Python and programming
Topic: Functions – Part 2
Functions CIS 40 – Introduction to Programming in Python
Structure of a C Program
Introduction to Python and programming
Introduction to Python and programming
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
Lesson 06: Functions Class Chat: Attendance: Participation
Introduction to Programming
Introduction to Python and programming
5. Functions Rocky K. C. Chang 30 October 2018
Loops and Simple Functions
COMPUTER PROGRAMMING SKILLS
Introduction to Computer Science
General Computer Science for Engineers CISC 106 Lecture 03
Python Functions.
Introduction to Programming
Introduction to Python and programming
def-ining a function A function as an execution control structure
Functions, Procedures, and Abstraction
Presentation transcript:

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) <class ‘float’)> returned value >>> int(23.4) returned value is now an integer >>>float(45) 45.0 returns a float >>>str(456) ‘456’

Mathematical Functions >>> import math >>> math.sqrt(45) 6.708203932499369 >>> r=5 >>>3/4.0*math.pi*r**3 294.5243112740431 >>> math.log(100,2)  log of 100 base 2 6.643856189774725 >>> math.sin(math.pi) 1.2246467991473532e-16 what in the world is this? Study the trig example in exercise 3

New Functions you write Type in the following script within the editor and run it import math def vol_of_sphere(radius):  : is REQUIRED vol = 4/3.0*math.pi*radius**3  indention is required return vol print vol_of_sphere(5) >>> 523.598775598  output that you’ve seen before

Function parts Argument(sent in) radius is called a parameter. def vol_of_sphere(radius): header vol = 4/3.0*math.pi*radius**3 return vol body Function definition Value to return Use four spaces for the indention

Function to find the area of a triangle See Herons formula (http://mathworld.wolfram.com/HeronsFormula.html) # Herons formula as a script import math def 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 a print area(1,1,1) >>> 0.433012701892

Function flow of execution Inst print area(5,5,6) 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!

Function Variables and Parameters are Local import math #Define the function here def 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 here s = 25 a=2 print area(1,1,1) print s,a #here is the output >>> 0.433012701892 25 2

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 Write a function to return the volume of a sphere of radius r 𝒗=𝟒/𝟑𝝅 𝒓 𝟑

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