Chapter 6 Functions -- QuickStart. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a function?

Slides:



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

CSE 251 Dr. Charles B. Owen Programming in C1 Functions.
Order of operators: x ** y x * y, x / y, x // y, x % y x + y, x - y
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Chapter 1. The Phases of Software Development. Data Structure 2 Chapter outline  Objectives  Use Javadoc to write a method’s complete specification.
Chapter 4 Working with Strings. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Sequence of characters.
Introduction to C Programming
Chapter 10 Algorithmic Thinking. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Learning Objectives List the five essential.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Introduction to C Programming
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.
Group practice in problem design and problem solving
Guide to Programming with Python Chapter Two Basic data types, Variables, and Simple I/O: The Useless Trivia Program.
Chapter 1 Beginnings.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
An Introduction to Textual Programming
Introduction to Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Introduction to Python
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Chapter 11 Introduction to Classes. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 11.1.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 7 Lists and Tuples. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Data Structures.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
Introduction to programming in the Java programming language.
Built-in Data Structures in Python An Introduction.
Chapter 8 More On Functions. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. First cut, scope.
Characters. Character Data char data type – Represents one character – char literals indicated with ' '
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Chapter 9 Dictionaries and Sets.
Chapter 5 Files and Exceptions I. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a file? A.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
XP New Perspectives on XML, 2 nd Edition Tutorial 7 1 TUTORIAL 7 CREATING A COMPUTATIONAL STYLESHEET.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
CSE 251 Dr. Charles B. Owen Programming in C1 Functions.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
A Python Tour: Just a Brief Introduction "The only way to learn a new programming language is by writing programs in it." -- B. Kernighan and D. Ritchie.
Python 1 SIGCS 1 Intro to Python March 7, 2012 Presented by Pamela A Moore & Zenia C Bahorski 1.
Functions CSE 1310 – Introduction to Computers and programming Alexandra Stefan University of Texas at Arlington 1.
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
Today… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Chapter 6 Functions The Tic-Tac-Toe Game. Chapter Content In this chapter you will learn to do the following: 0 Write your own functions 0 Accept values.
A Python Tour: Just a Brief Introduction
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Organization of Programming Languages
Functions.
Strings Part 1 Taken from notes by Dr. Neil Moore
Learning to Program in Python
Number and String Operations
Functions, Procedures, and Abstraction
An Introduction to Python
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Computer Science
Functions, Procedures, and Abstraction
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

chapter 6 Functions -- QuickStart

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a function?

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Functions From Mathematics we know that functions perform some operation and return one value. They "encapsulate" the performance of some particular operation, so it can be used by others (for example, the sqrt() function)

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Why have them? Support divide-and-conquer strategy Abstraction of an operation Reuse. Once written, use again Sharing. If tested, others can use Security. Well tested, then secure for reuse Simplify code. More readable.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Mathematical Notation Consider a function which converts temperatures in Celsius to temperatures in Fahrenheit. –Formula: F = C * –Functional notation: F ~ celsius_to_Fahrenheit(C) where celsius_to_Fahrenheit(C) = C *

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Python Invocation Math: F = celsius_to_Fahrenheit(C) Python, the invocation is much the same F = celsius_to_Fahrenheit(cel_float) Terminology: cel_float is the argument

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Function defintion Math: g(C) = C* Python def celsius_to_Fahrenheit(param_float): return param_float * Terminology: cel_float is the parameter

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. return statement The return statement indicates the value that is returned by the function The statement is optional (the function can return nothing). If no return, function is often called a procedure.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 6.1 Temp convert

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Triple quoted string in function A triple quoted string just after the def is called a docstring docstring is documentation of the function's purpose, to be used by other tools to tell the user what the function is used for. More on that later

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Operation def celsius_to_Fahrenheit (param): return param * F = celsius_to_fahrenheit(C) 1. Call copies argument C to parameter Temp 2. Control transfers to function

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Operation (con't) 3. Expression in function is evaluated 4. Value of expression is returned to the invoker F = celsius_to_fahrenheit(C) def celsius_to_Fahrenheit (param): return param *

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 6.2 Full Temp Program

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 6.3 re-implement len

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 6.4 Count letters in string

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. check membership in lowercase import string use string.ascii_lowercase, string of lowercase english letters –'abcdefghijklmnopqrstuvwxyz' check if each char is a member (using in operator) of string.ascii_lowercase char.lower() before membership (catch Capital Letters that way)

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Word Puzzle Find an English language word that has the vowels 'a', 'e', 'i', 'o', and 'u' in sequence

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Reading a file of Text Remember how to work with text files The open function takes a string (a file name) and a mode ('r' for reading) and returns a file object. You can use a for loop on the file object to fetch one line of text at a time (a line ends with a carriage return)

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 6.5 Open a file to read

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Need a list of words We use a dictionary file (easily found on the web) of english words, one word per line open the file process each line (a single word) this example just prints them all

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 6.6 Clean a word

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. clean the word strip method removes white space characters from the beginning and end of a string (can remove other chars as well) –beginning and end only, not the middle –all such characters from either end –file line likely has returns or tabs of spaces which might hurt compares lower method so case won't matter

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 6.8 Extract Vowels

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. collect vowels collect only vowels as a string, in order from the word, and compare against the reference "aeiou" –use in operator for membership –use + operator to concat vowels together

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listion 6.9 Solution to word problem

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Did functions help? Made our problem solving easier (solved smaller problems as functions) main program very readable (details hid in the functions)

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. How to write a function Does one thing. If it does too many things, it should be broken down into multiple functions (refactored) Readable. How often should we say this? If you write it, it should be readable Reusable. If it does one thing well, then when a similar situation (in another program) occurs, use it there as well.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. More on functions Complete. A function should check for all the cases where it might be invoked. Check for potential errors. Not too long. Kind of synonymous with do one thing. Use it as a measure of doing too much.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Rule 8 A function should do one thing

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Procedures Functions that have no return statements are often called procedures. Procedures are used to perform some duty (print output, store a file, etc.) Remember, return is not required.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Multiple returns in a function A function can have multiple return statements. Remember, the first return statement executed ends the function. Multiple returns can be confusing to the reader and should be used judiciously.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Reminder, rules so far 1.Think before you program! 2.A program is a human-readable essay on problem solving that also happens to execute on a computer. 3.The best way to imporve your programming and problem solving skills is to practice! 4.A foolish consistency is the hobgoblin of little minds 5.Test your code, often and thoroughly 6.If it was hard to write, it is probably hard to read. Add a comment. 7.All input is evil, unless proven otherwise. 8.A function should do one thing.