Compsci 101.2, Fall 2015 3.1 Plan For the Week l Solve problems by programming in Python  Like to do "real-world" problems, but we're very new to the.

Slides:



Advertisements
Similar presentations
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Advertisements

Python Basics: Statements Expressions Loops Strings Functions.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
JAVA BASICS SYNTAX, ERRORS, AND DEBUGGING. OBJECTIVES FOR THIS UNIT Upon completion of this unit, you should be able to: Explain the Java virtual machine.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Chapter 25 GRASP: More Objects with Responsibilities 1CS6359 Fall 2011 John Cole.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!
Aalborg Media Lab 21-Jun-15 Software Design Lecture 1 “ Introduction to Java and OOP”
Programming Introduction November 9 Unit 7. What is Programming? Besides being a huge industry? Programming is the process used to write computer programs.
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.
Lecture Note 3: ASP Syntax.  ASP Syntax  ASP Syntax ASP Code is Browser-Independent. You cannot view the ASP source code by selecting "View source"
Abstraction IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 17, 2013 Carolyn Seaman University of Maryland, Baltimore County.
CompSci 101 Introduction to Computer Science January 20, 2015 Prof. Rodger compsci 101, spring
Cs3102: Theory of Computation Class 18: Proving Undecidability Spring 2010 University of Virginia David Evans.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
ICAPRG301A Week 4Buggy Programming ICAPRG301A Apply introductory programming techniques Program Bugs US Navy Admiral Grace Hopper is often credited with.
General Programming Introduction to Computing Science and Programming I.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
Compsci 101, Fall Plan for the week: Week 2, Sept 1-5 l Understanding program structure  Defining, testing, calling functions  How to run a.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
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.
Compsci 06/101, Fall What will we do today? l Practice solving problems  Solving problems without a computer  Is this different than solving.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Compsci 101.2, Fall Plan For the Day l Discuss Algorithms and Programming at a high level, examples with cooperative/group work  Connect to reading.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
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.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Compsci 6/101, Spring More on Python, Tools, Compsci 101 l APTs, Assignments, Tools  APT: Algorithmic Problem-solving and Testing  How to get.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
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.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
CS105 Computer Programming PYTHON (based on CS 11 Python track: lecture 1, CALTECH)
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
Compsci 101, Fall Plan For The Day (PFTD) l Practice solving problems  Some solved with a computer, some with Python  Differences in solving.
Higher Computing Science 2016 Prelim Revision. Topics to revise Computational Constructs parameter passing (value and reference, formal and actual) sub-programs/routines,
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
CompSci 101 Introduction to Computer Science January 15, 2015 Prof. Rodger 1.
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.
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.
PROBLEM SOLVING WARM-UP Fill in the spaces using any operation to solve the following (!, (), -/+,÷,×): = 6.
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.
Topic 2: Hardware and Software
Introduction to Computing Science and Programming I
Exam #1 You will have exactly 30 Mins to complete the exam.
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
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.
Tonga Institute of Higher Education IT 141: Information Systems
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Tonga Institute of Higher Education IT 141: Information Systems
CISC101 Reminders Assignment 3 due today.
Functions, Procedures, and Abstraction
Presentation transcript:

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 language  Learn the syntax and semantics of simple Python programs l Compsci 101 Way-of-life  Assignments, APTs, READMEs, oh-my l Vocabulary and Concepts  Talk, think, speak like a Compsci 101 student  Talk, think, speak like a computer scientist?

Compsci 101.2, Fall PFTDay (introducing the week) l Review Python program we didn't get to  helloaroundtheworld.py  Forming hypotheses about programs and Python, testing hypotheses by running code l Functions in Python and Compsci101  What is a function, organizing programs  How do we gain understanding and practice with Python functions in Compsci101

Compsci 101.2, Fall Understanding terminology: code l Move from "Hello World" to "Hello Around the World"  Look at Python, code, libraries  Learning (reviewing) terminology about Python print "hello world" f = open("hello_unicode.txt") for line in f: print line)

Compsci 101.2, Fall Running and Understanding Code l Need Python compiler/interpreter  We're using Canopy, includes libraries l Need an editor development environment  We use Eclipse and PyDev, open source and widely used, Ambient is Duke Plugin l You need experience thinking and coding and debugging ideas and code:  Installing the suite of tools can be cumbersome Persist, Perservere, Get Help, start over 

Compsci 101.2, Fall Code Dissection l Every line thought about, occasionally understood at different levels  Use your understanding of natural language and experience, apply to Python  Run program and apply knowledge to each of the other lines f = open("hello_unicode.txt") for line in f: print line f = open("hello_unicode.txt")

Compsci 101.2, Fall Questions about Python Code l Every line thought about, occasionally understood  What about when you make changes to the program? Answer these questions about possible errors introduced when making changes

Compsci 101.2, Fall Barbara Liskov l (one of) first women to earn PhD from compsci dept  Stanford 1968 l Turing award in 2008  Programming Languages “It's much better to go for the thing that's exciting. But the question of how you know what's worth working on and what's not separates someone who's going to be really good at research and someone who's not. There's no prescription. It comes from your own intuition and judgment.”

Compsci 101.2, Fall Hello Around the World in Python l We open a file, and we open a URL  Syntax slightly different, concept is similar  Real-world differences between files and URLs? l Must adhere to syntactic rules of Python  Naming, whitespace, : or. or ( or ) or [ or ] l Must adhere to semantic rules of Python  Can't loop over anything, more rules to follow f = open("hello_unicode.txt") f = urllib2.urlopen("

Compsci 101.2, Fall Libraries, Modules, APIs l We need libraries of useful code  Can't write it all from scratch  Connect to web  Payment systems  Mapping systems  Geolocation … l Application Programming Interface  How to "connect" to software  Syntax and semantics of use

Compsci 101.2, Fall Python programming l You write a Python program  Starting from scratch, snarfing, finding online  You execute it using a Python interpreter  Code executes top-to-bottom, see Hello World l Constructing a Python program  Like constructing sentences in a new language  Learning and using vocabulary  Constructing increasingly complex programs, but starting from something simple

Compsci 101.2, Fall Examples of functions

Compsci 101.2, 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.2, Fall Python Functions l Answer these questions based on thinking, don't run any code  l Why do we need functions?  Manage complexity of large programs  Test and develop code independently  Reuse code in new contexts: create APIs!

Compsci 101.2, Fall Functions return values l Most functions return values  Sometimes used to make things simpler, but returning values is a good idea def inch2centi(inches): return 2.54*inches xh = inch2centi(72) def pluralize(word): return word + "es" pf = pluralize("fish")

Compsci 101.2, 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 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.2, 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 1 l Step 1 is key, without it you won’t get anywhere

Compsci 101.2, Fall Functions: BMI (Body Mass Index) l What is formula? How to use it?  Functions allow us to re-use the formula 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.2, 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.2, Fall Toward creating functions l New meets old  Old MacDonald had a farm, Ee-igh, Ee-igh, oh! And on his farm he had a pig, Ee-igh, Ee-igh, oh! With a oink oink here And a oink oink there Here a oink there a oink everywhere a oink oink Old MacDonald had a farm, Ee-igh, Ee-igh, oh!

Compsci 101.2, Fall Creating Parameterized Function What differs? Variable or Parameter Old MacDonald had a farm, Ee-igh, Ee-igh, oh! And on his farm he had a horse, Ee-igh, Ee-igh, oh! With a neigh neigh here And a neigh neigh there Here a neigh there a neigh everywhere a neigh neigh Old MacDonald had a farm, Ee-igh, Ee-igh, oh! And on his farm he had a pig, Ee-igh, Ee-igh, oh! With a oink oink here And a oink oink there Here a oink there a oink everywhere a oink oink Old MacDonald had a farm, Ee-igh, Ee-igh, oh!

Compsci 101.2, 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.2, 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.2, 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.2, 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.2, 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.2, Fall Nathan Myhrvold l Who is he?  We invent for fun. Invention is a lot of fun to do. And we also invent for profit. The two are related because the profit actually takes long enough that, if it isn't fun you wouldn't have the time to do it.