Compsci 101, Fall 2014 3.1 Plan for the week: Week 2, Sept 1-5 l Understanding program structure  Defining, testing, calling functions  How to run a.

Slides:



Advertisements
Similar presentations
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
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 Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
JavaScript & jQuery the missing manual Chapter 11
Abstraction IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 17, 2013 Carolyn Seaman University of Maryland, Baltimore County.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
ICAPRG301A Week 4Buggy Programming ICAPRG301A Apply introductory programming techniques Program Bugs US Navy Admiral Grace Hopper is often credited with.
Computing Science 1P Lecture 21: Friday 20 th April Simon Gay Department of Computing Science University of Glasgow 2006/07.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
Compsci 06/101, Fall Compsci 6/101: Sept 6-10 l We've looked at the language Python  Names and types: int, float, long, String, file, …  Operations:
Compsci 101.2, Fall PFTT (plan for this Thursday) l What is a Python program?  In the context of what we do in Compsci 101  In a neuroscience.
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
Compsci 06/101, Fall What will we do today? l Practice solving problems  Solving problems without a computer  Is this different than solving.
Compsci 101.2, Fall Plan For the Week l Solve problems by programming in Python  Like to do "real-world" problems, but we're very new to the.
Week 91 Introduction to Programming Ms. Knudtzon C Period Quarter 2 – Lecture 20 Monday, November 1 st.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
A Computer Science Tapestry 3.1 Programs that Respond to Input l Programs in chapters one and two generate the same output each time they are executed.
Compsci 06/101, Fall What will you do in Compsci 6 Today? l Learn about selection and if/else statements  In context of solving problems  Understanding.
CompSci 101 Introduction to Computer Science September 23, 2014 Prof. Rodger.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
CompSci 101 Introduction to Computer Science Sept. 9, 2014 Prof. Rodger President Brodhead speech graduation 2010 CompSci 101 Fall
Compsci 6/101, Spring More on Python, Tools, Compsci 101 l APTs, Assignments, Tools  APT: Algorithmic Problem-solving and Testing  How to get.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Compsci 101.2, Fall PFTWeek l Introduce new Python concepts  Control: if, elif, else, for  Data: Strings, Lists Operators on data: slicing,
Compsci 06/101, Fall Vocabulary l What's the Spanish word for 'boy'? 'eat'?  How do you know? l What about the Turkish equivalents?  How do.
A compiler is a computer program that translate written code (source code) into another computer language Associated with high level languages A well.
Python Functions : chapter 3
Georgia Institute of Technology Speed part 4 Barb Ericson Georgia Institute of Technology May 2006.
Compsci 101, Fall Plan For The Day (PFTD) l Practice solving problems  Some solved with a computer, some with Python  Differences in solving.
CompSci 101 Introduction to Computer Science January 28, 2016 Prof. Rodger compsci101 spring161.
ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that.
Compsci 6/101, Spring PFTW: Functions, Control, Python/Tools l How do functions work and why do we use them?  Functions we call (APIs), Functions.
Compsci 06/101, Spring Compsci 6: PFTW l Problem solving and (Python) programming  What are the steps in solving an APT?  How do you get better.
CompSci 101 Introduction to Computer Science January 26, 2016 Prof. Rodger compsci 101, spring
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
CompSci 101 Introduction to Computer Science January 15, 2015 Prof. Rodger 1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
CompSci 101 Introduction to Computer Science February 16, 2016 Prof. Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
CompSci 101 Introduction to Computer Science Sept 13, 2016 Prof. Rodger compsci101 fall161.
CompSci 4 Java 1 Apr 2, 2009 Prof. Susan Rodger. Announcements Assignment 7 questions? –Beware having two events that kick in at the same time! –Beware.
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Functions CIS 40 – Introduction to Programming in Python
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Compsci 6/101: PFTW Review how APTs and Python work, run
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
CS 1111 Introduction to Programming Spring 2019
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Introduction to Programming
CISC101 Reminders Assignment 3 due today.
Functions, Procedures, and Abstraction
IS 135 Business Programming
Presentation transcript:

Compsci 101, Fall Plan for the week: Week 2, Sept 1-5 l Understanding program structure  Defining, testing, calling functions  How to run a program, how someone runs yours l Understanding more of Python the language  Types: string, int, bool, float  Operations on these, e.g., +, %, [:], and  Control: conditionals and loops (Thursday) l Course structure: APTs, labs, assignments  Tools for enabling structure

Compsci 101, Fall Examples of functions

Compsci 101, Fall Functions explained l In a calculator, sqrt: number in -> number out  What is domain, what is range? l In MSWord, word count: document -> number  Domain is word doc, range is integer l In browser, web: URL -> HTML formatted "page"  Domain is valid URL, range is HTML resources l In Python we see similar structure!

Compsci 101, Fall Abstracting over code: functions l l See snarf for class work as well l These functions do not return values, they print  Illustrates problem decomposition, but …  Normally have each function return a value  Normally use the return value in function call

Compsci 101, Fall Part of (and snarf) def eieio(): print "Ee-igh, Ee-igh, oh!" def refrain(): print "Old MacDonald had a farm,", eieio() def had_a(animal): print "And on his farm he had a",animal,",", eieio() Lots of commas

Compsci 101, Fall Anatomy and Dissection of Print l Print generates output to a console, window, …  Depends on how program invoked  Basically used for: help with debugging and creating output for copy/paste, view l Space inserted between comma-separated items  Can use string concatentation, "hello"+str(x)  If statement ends with comma, no newline  Print anything that has a string representation… print "hello,",x,"what's up",y

Compsci 101, Fall Tracing program execution The def statement defines a function  Creates a name that can be used in program  Name encapsulates program statements, creates its own environment for running code Variables, parameters, local to the function l Function name and statements part of Python execution environment  Can call or invoke the function  If parameters needed, must pass values for each  Visualize program execution: PythonTutor, brain

Compsci 101, Fall Abstraction over barnyards In OldMacPrint we have pig() and fox() …  What's the same in these? What's different?  Capture differences in parameters/variables l Create new function:  def verse(animal, noise) l Look at pig() and fox() create new function  Call: verse("horse", "neigh")  Call: verse("cow", "moo")

Compsci 101, Fall Nancy Leveson: Software Safety l Mathematical and engineering aspects, invented the discipline  Health care software  MS Word, Airbus 360, … “ There will always be another software bug; never trust human life solely on software” huffington post?huffington post? l Therac 25: Radiation machine  rac-25, rac-25http://bit.ly/5qOjoH l Software and steam engines Software and steam engines

Compsci 101, Fall Compsci 101: Running Python l What does it mean to run a program?  What does clicking on app/program do?  How do you run/test APT code, other Python code Where does program start to execute/run? l Control flow in Python  Loops and if statements --- coming on Thursday  Essential for writing real programs But function calls are part of control flow

Compsci 101, Fall Functions that return values l Most functions return values  Example in Old MacDonald is "different"  Some claim: all functions return values def inch2centi(inches): return 2.54*inches xh = inch2centi(72) def pluralize(word): return word + "es" pf = pluralize("fish")

Compsci 101, Fall What is an APT? BMI APTBMI APT l Automated/Algorithmic Problem Testing  Write one function, 2-30 lines, solve a problem  Tested automagically in Eclipse or the browser  Test test test … Quality of code not an issue l Start simple, build toward more complex  What is a function? A function call?  What is a parameter? Argument?  How do you run/execute a program

Compsci 101, Fall How to solve an APT l Two very, very, very important steps 1. How to solve the problem without computer Paper, Pencil, (Calculator) 2. How to translate problem-solving to Python l Both steps can be hard, vocabulary and language are initially a real barrier  More Python experience, easier step 2 becomes  With experience, step 2 can influence step 2 l Step 1 is key, without it you won’t get anywhere

Compsci 101, Fall Anatomy of a Python function def name(params): body l Define a function: name, parameters, body  How do we decide on these?  Do we need parameters?  What does body of function do l Functions provide a named abstraction over code  Huh? math.factorial(5)"hello".upper()

Compsci 101, Fall Functions: BMI (Body Mass Index) l What is formula? How to use it?  For one person can simply print the BMI Make sure units are correct, formula right  What if we want to validate data?  What if we want to notify folks who might need guidance? def bmi(weight, height): return * weight/(height*height) if bmi(170,72) < 18.5: print "underweight" call replaced by return value, why use function?

Compsci 101, Fall What does return statement do? l Programs execute one line at a time  After one statement finishes, the next executes  Calling a function causes its code to execute What happens in the code that calls the function? l The value returned replaces the function call  print math.sqrt(25.0)  if bmi(170,72) < 18.5: print "underweight" l What if nothing returned?  None by default in Python

Compsci 101, Fall Re-use: Counting words in file def word_count(filename): f = open(filename) all = f.read() words = all.split() return len(words) if __name__ == "__main__": name = "/data/romeo.txt" print "# words in",name, print "=",wordCount(filename)

Compsci 101, Fall Running Python Program/Module l Python is an interpreter, platform specific  So is Java, so is Android, … contrast compilers  Python can execute a.py file, need a "launch point" l Convention in Python and other languages  Start with section labeled __main__, that's run if __name__ == "__main__": statements here l Boilerplate, don't memorize, let Eclipse do work!

Compsci 101, Fall Function calls: what is an API?

Compsci 101, Fall Eclipse Particulars l Supports many languages: we care about Python  PyDev perspective: Windows>Open Perspective>Other>…  Also use console: Windows>Show View>Console  Use PyDev console (right click console icon) l Creating projects, Python Module  Illustrated with examples in class l Submitting and check via Ambient  Illustrated with examples in class

Compsci 101, Fall A-Z, Soup to Nuts, APT all the way l Where do we find what APTs are due this week?  Web pages, Sakai v Google v bookmark l Testing code for APTs supplied by 101 staff  Snarf the project that provides testing harnass  Don't call us, ETester.py will call you (your code) l Refresh to see results.html  Repeat until finished l Submit using Ambient, Duke CS Eclipse plugin

Compsci 101, Fall Summary of Today l Functions help in program/problem decomposition  Each function does one thing only  Functions typically return values Song printing functions don't, they print l Names, parameters, arguments, return values  Functions execute, return replaces call point  Calling code picks up and continues after call l We'll see loops and conditionals on Thursday

Compsci 101, Fall Grace Murray Hopper ( ) l “third programmer on world's first large-scale digital computer”  US Navy: Admiral “It's better to show that something can be done and apologize for not asking permission, than to try to persuade the powers that be at the beginning” l ACM Hopper award given for contributions before : Craig Gentry: : Luis von Ahn 2013: Pedro Felzenszwalb

Compsci 101, Fall Duke Compsci: Grace Hopper 2013