Python Programming Module 3 Functions Python Programming, 2/e1.

Slides:



Advertisements
Similar presentations
Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and.
Advertisements

Python Basics: Statements Expressions Loops Strings Functions.
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
Solving Problems with Repetition. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C# program Correctly.
Adapted from John Zelle’s Book Slides
Python Programming: An Introduction to Computer Science
CSC 110 Writing simple programs [Reading: chapter 2] CSC 110 C 1.
Vahé Karamian Python Programming CS-110 CHAPTER 2 Writing Simple Programs.
 Draft timetable has the same times as this semester: - ◦ Monday 9:00 am to 12:00 noon, 1:00 pm to 3:00 pm. ◦ Tuesday 9:00 am to 12:00 noon, 1:00 pm.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Intro to Robots Robots are Everywhere. Intro to Robots Various robots in use today lawnmower pet baby seal for convalescents gutter cleaner home security.
Chapter 20 Thinking Big: Functions. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Anatomy of a Function Functions are packages for.
Computer Programming 1 Functions. Computer Programming 2 Objectives Take a first look at building functions Study how a function is called Investigate.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
CS 201 Functions Debzani Deb.
Chapter 2 Writing Simple Programs
Wed/Fri Week 2 Functions! What are they? What do they look like in JavaScript? What are they good for? How do I use them? Some examples… Mini-Lab 1!!!
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
Software Engineering 1 (Chap. 1) Object-Centered Design.
Functions.
1 Programming and Problem Solving — Software Engineering (Read Chap. 2)
Programming and Problem Solving — Software Engineering (Read Chap. 2) 1.
Python Programming Fundamentals
Introduction to Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
GIRLS Robotic Camp. Let’s Begin Meet and Greet – Camp leaders introduce themselves – Students introduce themselves.
Introduction to Python
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
COMP 171: Functions, for loops and range John Barr Section 03 Slides by Toby Dragon.
Chapter 6 Functions -- QuickStart. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a function?
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
1 Functions Lecfture Abstraction abstraction is the process of ignoring minutiae and focusing on the big picture in modern life, we are constantly.
More on Functions; Some File I/O UW CSE 190p Summer 2012.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
Basics of Most C++ Programs // Programmer: Clayton Price date: 9/4/ // File: fahr2celc.cpp 03. // Purpose:
Python Functions : chapter 3
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.
ME 142 Engineering Computation I Using Subroutines Effectively.
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.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
Controlling Program Flow with Decision Structures.
Mindstorm NXT-G Introduction Towson University Robotics.
Solving Problems with Repetition Version 1.0. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C#
COSC 1223 Computer Science Concepts I Joe Bryan. What is a function? Like a mini-program that performs a specific task Two types: Built-in functions Functions.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
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.
Python Programming Module 4 Sensors and Loops Python Programming, 2/e1.
Python Programming Module 1 Computers and Programs Python Programming, 2/e1.
Chapter 2 Writing Simple Programs
User-Written Functions
Introduction to Programming
Python: Experiencing IDLE, writing simple programs
Python Programming Module 3 Functions Python Programming, 2/e.
ROBOTC for VEX On-Site Professional Development
Python Programming: An Introduction to Computer Science
Functions, Procedures, and Abstraction
Python 19 Mr. Husch.
Introduction to Computer Science
Chopin’s Nocturne.
Python 19 Mr. Husch.
Functions, Procedures, and Abstraction
Presentation transcript:

Python Programming Module 3 Functions Python Programming, 2/e1

Functions Problems we need to solve over and over: – Calculate sales tax. – Turn the robot 90 degrees to the left. – Convert Celsius to Fahrenheit It would be a pain to include a complete copy of the code every time we wanted to do one of these jobs. We can “package” the commands up as a function and give it a name. Python Programming, 2/e2

Functions A function is like a subprogram, a small program inside of a program. The basic idea – we write a sequence of statements and then give that sequence a name. We can then execute this sequence at any time by referring to the name. Python Programming, 2/e3

Functions The part of the program that creates a function is called a function definition. When the function is used in a program, we say the definition is called or invoked. Python Programming, 2/e4

Programming Design: Temperature Converter Problem – the temperature is given in Celsius by the user; it should be converted and displayed in degrees Fahrenheit. Specification – Input – temperature in Celsius – Output – temperature in Fahrenheit – Output = 9/5(input) + 32 Python Programming, 2/e5

Example Program: Temperature Converter Design – Input, Process, Output (IPO) – Prompt the user for input (Celsius temperature) – Process it to convert it to Fahrenheit by calling a function that implements F = 9/5(C) + 32 – Output the result by displaying it on the screen Python Programming, 2/e6

Creating a Function When we create a function we need to give it a name. Then we need to determine what information will be needed for the function to do its job. We can pass this information to the function as a list of parameters. Python Programming, 2/e7

Creating a Function A function declaration looks like the following: def celsius2farh(c): This function is named celsius2farh and takes a variable which will be called c inside the function The variable could be named something else when the function is called. Python Programming, 2/e8

Creating a Function We then indent the commands that will make up the function. – This is called the body of the function. def celsius2fahr(c): f = 9/5 * c + 32 return f To report the result of the function we use a return statement. Python Programming, 2/e9

Calling a Function To call our function we need to supply a value for c. This can be anything that has an appropriate value. – celsius2fahr(5) has the value – If x = 100 then celsius2fahr(x) has the value You do not need to know what the value is called inside the function in order to use it! Python Programming, 2/e10

Example Program: Temperature Converter #Convert Celsius to Farhenheit def celsius2fahr(c): f = 9/5 * c + 32 return f celsius = float(input("What is the Celsius temperature? ")) fahrenheit = celsius2fahr(celsius) print("The temperature is", fahrenheit, "degrees Fahrenheit.") Python Programming, 2/e11

Example Program: Temperature Converter When we write a program, we should test it! What is the Celsius temperature? 0 The temperature is 32.0 degrees Fahrenheit. What is the Celsius temperature? 100 The temperature is degrees Fahrenheit. What is the Celsius temperature? -40 The temperature is degrees Fahrenheit. Python Programming, 2/e12

Passing Parameters Not all functions need parameters. def instructions(): print(“The purpose of this program… Even if a function doesn’t have parameters it still needs parentheses. In Python a function may “just do something” and so not need a return value. Python Programming, 2/e13

Passing Parameters A function may have several parameters. def coneVol(h, r): return (math.pi * r**2 * h / 3) If there are multiple parameters their values are determined by order. For example if we call this using v = coneVol(5, 9) In the function h=5 and r=9. Python Programming, 2/e14

Objects Some functions “belong” to a special variable called an object. These are called methods of the object. A Large Motor would be an example of an object. m = LargeMotor() m.timed_run() We use a. to show the object to which the function belongs. Python Programming, 2/e15

Functions that “Do Things” To have our robot drive forward there are several commands we need to issue. We might want to combine these into a single function. We could then call the function whenever we need to drive forward. Python Programming, 2/e16

Functions that “Do Things” We will need to tell the function some information. – Which motors are involved (left and right). – How long to drive. – What speed to use. def speed_drive(ml, mr, t, v): This function doesn’t need a return value. Python Programming, 2/e17

Functions that “Do Things” def speed_drive(ml, mr, t, v): ml.speed_regulation_enabled = 'on' mr.speed_regulation_enabled = 'on' ml.speed_sp = v mr.speed_sp = v ml.time_sp = t mr.time_sp = t ml.stop_command = 'brake' mr.stop_command = 'brake' ml.run_timed() mr.run_timed() Python Programming, 2/e18

Activities Write functions to do the following: Turn Right 90 degrees Turn Left 90 degrees Repeatedly call these along with the speed_drive function to navigate a maze. Python Programming, 2/e19

Sensors Using sensors is similar to using motors. You first need to name the sensor. t = TouchSensor(‘in1’) We can then check the value the sensor is currently seeing. t.value() If the button on the touch sensor is pressed this will return 1 otherwise it will return 0. Python Programming, 2/e20

Temperature Sensor The Temperature Sensor did not come with the EV3 set and the software that comes with ev3dev does not support this sensor. We have written a binding for this sensor for us to use, but right now it reports a number whose meaning is not clear. We also want to be able to convert to different measures of temperature. Python Programming, 2/e21

Temperature Sensor Here is how it should work. from ev3 import * temp = TemperatureSensor(‘in2’) temp.mode = ‘TEMP-F’ temp.value() This should report the temperature in degrees Fahrenheit. The conversion function is broken! We need to fix it. Python Programming, 2/e22

Temperature Sensor Modes The temperature sensor is supposed to have the following modes. – ‘TEMP-F’ – degrees Fahrenheit – ‘TEMP-C’ – degrees Celsius – ‘TEMP-K’ – degrees Kelvin Note, with the other sensors, if there is only one, we can skip naming the input port. With the Temperature Sensor it is always needed. Python Programming, 2/e23

Conversion Functions In our next robot challenge we will write the code to implement these conversions functions. We will base these on experimental data that we will gather. We can convert to one temperature measure and then use known conversion formulas to convert to the others. This uses composition of functions. Python Programming, 2/e24