Function Abstraction Black Box Container for a sequence of actions Call, execute, invoke the function by name Good for abstraction of key operations Good.

Slides:



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

CS1315: Introduction to Media Computation Making sense of functions.
Computer Programming w/ Eng. Applications
Function in Notepad def drawSquare(myTurtle): myTurtle.forward(100) myTurtle.right(90) # side 1 myTurtle.forward(100) myTurtle.right(90) # side 2 myTurtle.forward(100)
Speed CS A109. Big speed differences Many of the techniques we’ve learned take no time at all in other applications Select a figure in Word. –It’s automatically.
By Now, 1. Your Student ID should open the classroom door 2. You should have C:\100 folder Desktop shortcuts to Command Prompt and Notepad 3. You should.
CS 100: Roadmap to Computing Fall 2014 Lecture 0.
Jay Summet CS 1 with Robots IPRE Python Review 1.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,
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.
Python Programming Fundamentals
CS 102 Computers In Context (Multimedia)‏ 01 / 28 / 2009 Instructor: Michael Eckmann.
Munster Programming Training
PYTHON CONDITIONALS AND RECURSION : CHAPTER 5 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
Testing CSE 140 University of Washington 1. Testing Programming to analyze data is powerful It’s useless if the results are not correct Correctness is.
Sep 12, 2007Sprenkle - CS1111 Objectives Review  Linux  Why programming languages? Compiled vs. Interpreted Languages Programming in Python  Data types.
Manipulating Pixels by Range and More on Functions.
General Computer Science for Engineers CISC 106 Lecture 04 Dr. John Cavazos Computer and Information Sciences 09/10/2010.
You SHOUD HAVE.. C:\100 folder A desktop shortcut to “IDLE (Python34)”
Chapter 1. Objectives To provide examples of computer science in the real world To provide an overview of common problem- solving strategies To introduce.
Java is an object oriented programming language In this model of programming all entities are objects that have methods and fields Methods perform tasks.
Variables and Expressions CMSC 201 Chang (rev )
CS1315: Introduction to Media Computation Making sense of functions.
Introduction to Python and programming Ruth Anderson UW CSE 140 Winter
Introduction to Computer Programming
ECS 15 Variables. Outline  Using IDLE  Building blocks of programs: Text Numbers Variables!  Writing a program  Running the program.
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏ Sec 9-7 Web Design.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
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.
Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and.
Variables 1. What is a variable? Something whose value can change over time This value can change more than once over the time period (no limit!) Example:
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
Variables, Types, Expressions Intro2CS – week 1b 1.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Values, Types, and Variables. Values Data Information Numbers Text Pretty much anything.
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
ECS 15 Strings, input. Outline  Strings, string operation  Converting numbers to strings and strings to numbers  Getting input  Running programs by.
Lists vs Strings CMSC 201. Overview Python strings and lists are similar but different. Similar: syntax, access mechanisms, operators Different: Strings.
Data Manipulation Variables, Data Types & Math. Aug Variables A variable is a name (identifier) that points to a value. They are useful to store.
Python programming Using the JES picture functions and defining new functions.
CS1315: Introduction to Media Computation Making sense of functions.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
For vs. While loop count=0 while count< 5: for count in range(5):
Characters and Strings
CS-104 Final Exam Review Victor Norman.
IDLE Hints To re-load a module after making changes:
CS 100: Roadmap to Computing
Types, Truth, and Expressions (Part 2)
To Start Create C:\100 folder
Variables, Data Types & Math
Python Primer 1: Types and Operators
Variables, Data Types & Math
What Color is it?.
CSIS110 - Introduction to Computer Science
Thinking about programming
Variables, Data Types & Math
Thinking about programming
COMPUTER PROGRAMMING SKILLS
Types, Truth, and Expressions
Characters and Strings
Nate Brunelle Today: Strings, Type Casting
CS 100: Roadmap to Computing
CSIS110 - Introduction to Computer Science
Using Modules.
Defining Functions.
Presentation transcript:

Function Abstraction Black Box Container for a sequence of actions Call, execute, invoke the function by name Good for abstraction of key operations Good for useful functions, such as summation, etc.

Olympic Rings import turtle myT = turtle.Turtle() myT.color("blue”) myT.goto(-110,-25) myT.circle(50) a = turtle.Turtle() a.color ("black”) a.goto(0,-25) a.circle(50) b = turtle.Turtle() b.color ("red”) b.goto(110,-25) b.circle(50) ….. import turtle aaa=turtle.Turtle() aaa.width(5) aaa.color("blue”) aaa.goto(0,0) aaa.circle(50) aaa.penup() aaa.color("yellow”) aaa.goto(45,-45) aaa.pendown() aaa.circle(50) aaa.penup() ……

Avg computation myList = [20,30,40,5,55] # get the sum of the list total = 0 numStu = len(myList) for n in range(numStu): total = total + myList[n) avg = total/numStu print(avg)

Avg computation def sum( ): myList = [20,30,40,5,55] avg = sum(myList)/numStu print(avg) myList = [20,30,40,5,55] # get the sum of the list total = 0 numStu = len(myList) for n in range(numStu): total = total + myList[n) avg = total/numStu print(avg)

An imaginary wedding computer def marry(husband, wife): sayVows(husband) sayVows(wife) pronounce(husband, wife) kiss(husband, wife) def sayVows(speaker): print “I, “ + speaker + “ blah blah” def pronounce(man, woman): print “I now pronounce you…” def kiss(p1, p2): if p1 == p2: print “narcissism!” if p1 <> p2: print p1 + “ kisses “ + p2 So, how do we marry Ben and Jo ?

An imaginary wedding computer def marry(husband, wife): sayVows(husband) sayVows(wife) pronounce(husband, wife) kiss(husband, wife) def sayVows(speaker): print “I, “ + speaker + “ blah blah” def pronounce(man, woman): print “I now pronounce you…” def kiss(p1, p2): if p1 == p2: print “narcissism!” if p1 <> p2: print p1 + “ kisses “ + p2

An imaginary wedding computer def marry(husband, wife): sayVows(husband) sayVows(wife) pronounce(husband, wife) kiss(husband, wife) def sayVows(speaker): print “I, “ + speaker + “ blah blah” def pronounce(man, woman): print “I now pronounce you…” def kiss(p1, p2): if p1 == p2: print “narcissism!” if p1 <> p2: print p1 + “ kisses “ + p2

Values A number or a string Python has a set of recognized value types  Integer (int)  Real (float)  String (str) Try in command window  type(‘c’)  type(‘3.2’) Operators  Int, float : arithmetic operations  +, -, *, /, **, %  String: concatenation (+)

Name a value and re-use it >>> x = 2*8 >>> x/2 >>> x Whenever a variable name appears, it is substituted by its value A variable name can be reused, redefined Variables

MUST start with a letter followed by alphanumeric aVar, cs100, … CASE matters ‘Print’ is not the same as ‘print’ ‘makePicture’ is not the same as ‘makepicture’, nor as ‘Makepicture’ Multi-word name First letter is capitalized Convention, not absolute rule Use sensible, meaningful name Naming Convention for both Variables and Functions

Variable vs. Function Variables have fixed values until reassigned Functions can have different values each time it is called Function has () for parameters

ViralSpiral.py (p.72) import turtle t=turtle.Pen() colors = ["red", "green", "purple", "blue"] sides = 4 for m in range(50): t.forward(4*m) position=t.position() direction = t.heading() for n in range(int(m/2)): t.pendown() t.pencolor(colors[n%sides]) t.forward(2*n) t.right(360/sides - 2) t.penup() t.setx(position[0]) t.sety(position[1]) t.setheading(direction) t.left(360/sides + 2)