Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Control Flow Statements: Repetition/Looping
Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1.
Introduction to OCaml Slides prepared by Matt Gruskin Some material borrowed from the CIS 500 lecture notes.
Python Programming Chapter 5: Fruitful Functions Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
Python Magic Select a Lesson: Why Learn to Code? Basic Python Syntax
Week 10 Recap CSE 115 Spring For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Fundamentals of Python: From First Programs Through Data Structures
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 9 Iteration: Recursion 5/02/09 Python Mini-Course: Day 3 - Lesson 9 1.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
60 Questions (review for final exam) CSC 161: The Art of Programming Prof. Henry Kautz 12/7/
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 13 Case study: Word play 05/02/09 Python Mini-Course: Day 4 – Lesson.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Iteration “for ??? in range(min,max,interval):” What if we do not know the number of repetitions ? “while (???):” (p.49) problem = input(“Enter a math.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
CIS 3301 C# Lesson 3 Control Statements - Selection.
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
Decisions in Python Boolean functions. A Boolean function This is a function which returns a bool result (True or False). The function can certainly work.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 10 Iteration: Loops 05/02/09 Python Mini-Course: Day 3 - Lesson 10 1.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Lesson thirteen Conditional Statement "if- else" ©
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
COMP Loop Statements Yi Hong May 21, 2015.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Iteration In Python new file, enter the following ‘for loop’ can be nested for a in range(3): for b in range(-2, 3): print(“ Inner loop: ”,a, b, a+b) print(“Outer.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Functions with Arguments and Return Values, Oh My! CS303E: Elements of Computers and Programming.
Python Basics.
Scientific Programming in Python -- Cheat Sheet
G. Pullaiah College of Engineering and Technology
Control Flow (Python) Dr. José M. Reyes Álamo.
CSC 458– Predictive Analytics I, Fall 2017, Intro. To Python
Computer Programming Fundamentals
Basic operators - strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
CSC 458– Predictive Analytics I, Fall 2018, Intro. To Python
CSC1018F: Intermediate Python
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Programming Using Python PART 2
Nate Brunelle Today: Conditional Decision Statements
Conditional Logic Presentation Name Course Name
Types, Truth, and Expressions (Part 2)
COMPUTER PROGRAMMING SKILLS
Another Example Problem
Types, Truth, and Expressions
Nate Brunelle Today: Conditional Decision Statements
Class code for pythonroom.com cchsp2cs
Types, Truth, and Expressions (Part 2)
Presentation transcript:

Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1

Lesson objectives 1. Understand how to return values from functions in Python 2. Create “fruitful” functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 2

The power function revisited def power(base, exponent): val = base for x in range(1,exponent): val = val * base return val power(3,4) 05/02/09 Python Mini-Course: Day 2 - Lesson 8 3

The return statement Syntax return [expression] Exists the enclosing function and returns the value of expression as a result of the call to the function If expression is omitted, returns None 05/02/09 Python Mini-Course: Day 2 - Lesson 8 4

The return statement You can only return one value That value can be any Python data type (including both built-in and custom data structures) You can also use the return statement to exit a function early 05/02/09 Python Mini-Course: Day 2 - Lesson 8 5

Exercise Create a function that computes the distance between any two points when given their coordinates (in 2-D space) 05/02/09 Python Mini-Course: Day 2 - Lesson 8 6

Distance function def distance(x1, y1, x2, y2): """Return the distance between points (x1, y1) and (x2, y2) as a float""" return /02/09 Python Mini-Course: Day 2 - Lesson 8 7

Distance function def distance(x1, y1, x2, y2): """Return the distance between points (x1, y1) and (x2, y2) as a float""" dx = x2 - x1 dy = y2 - y1 print 'dx is ', dx print 'dy is ', dy return /02/09 Python Mini-Course: Day 2 - Lesson 8 8

Distance function def distance(x1, y1, x2, y2): """Return the distance between points (x1, y1) and (x2, y2) as a float""" dx, dy = x2 - x1, y2 - y1 result = math.sqrt(dx**2 + dy**2) return result 05/02/09 Python Mini-Course: Day 2 - Lesson 8 9

Distance function def distance(x1, y1, x2, y2): """Return the distance between points (x1, y1) and (x2, y2) as a float""" result = \ math.sqrt((x2-x1)**2 + (y2-y1)**2) return result 05/02/09 Python Mini-Course: Day 2 - Lesson 8 10

Distance function def distance(x1, y1, x2, y2): """Return the distance between points (x1, y1) and (x2, y2) as a float""" return math.sqrt((x2-x1)**2 + (y2-y1)**2) 05/02/09 Python Mini-Course: Day 2 - Lesson 8 11

Creating Boolean functions Functions that return a Boolean value ( True or False ) can be used in conditional statements (i.e. if…else… ) 05/02/09 Python Mini-Course: Day 2 - Lesson 8 12

is_divisible function def is_divisible(x, y): if x % y == 0: return True else: return False is_divisible(16,4) is_divisible(16,3) 05/02/09 Python Mini-Course: Day 2 - Lesson 8 13

is_divisible function def is_divisible(x, y): return x % y == 0 is_divisible(16,4) is_divisible(16,3) x, y = 16, 5 if is_divisible(x, y): print x,'is divisible by',y 05/02/09 Python Mini-Course: Day 2 - Lesson 8 14

Next session Iteration methods: Recursion Loops and conditional execution The for loop The while loop Handling strings and text 05/02/09 Python Mini-Course: Day 2 - Lesson 8 15

Suggested exercises Exercise 5.1 – Fermat's Last Theorem Exercise 6.6 – Palindromes Exercise 6.7 – is_power function Turtle exercises from Chaps 4 & 5 05/02/09 Python Mini-Course: Day 2 - Lesson 8 16