Elements of a Python Program

Slides:



Advertisements
Similar presentations
Adapted from John Zelle’s Book Slides
Advertisements

Chapter 2 Writing Simple Programs
CS190/295 Programming in Python for Life Sciences: Lecture 1 Instructor: Xiaohui Xie University of California, Irvine.
Introduction to Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 6 Defining Functions.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Introduction to FunctionsCIS 1057 Fall Introduction to Functions CIS 1057 Computer Programming in C Fall 2013 (Acknowledgement: Many slides based.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that.
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 1 Help: To get help, type in the following in the interpreter: Welcome.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Chapter 2 Writing Simple Programs
Topic: Functions – Part 1
Introduction to Programming
Python: Experiencing IDLE, writing simple programs
Introduction to Python
Topics Introduction to Repetition Structures
Python: Control Structures
Lesson 5 Functions I A function is a small program which accomplishes a specific task. For example, we invoke (call) the function, sqrt(x), in the library.
Think What will be the output?
JavaScript: Functions.
Topics Introduction to Repetition Structures
CS190/295 Programming in Python for Life Sciences: Lecture 1
Chapter 5 - Functions Outline 5.1 Introduction
Functions Declarations CSCI 230
CHAPTER FOUR Functions.
Python Primer 2: Functions and Control Flow
Functions, Procedures, and Abstraction
First Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
CS190/295 Programming in Python for Life Sciences: Lecture 6
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Loops CIS 40 – Introduction to Programming in Python
Decision Structures and Indefinite Loops
Introduction to Dictionaries
Miscellaneous C++ Topics
Introduction to Programming
Homework #5 — Monte Carlo Simulation
5. Functions Rocky K. C. Chang 30 October 2018
More About Functions Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
Variables, Lists, and Objects
Debuggers and Debugging
Simple Graphics Package
Objects (again) Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
More elements of Python programs
Notes on pyplot Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
Notes about Homework #4 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:
CSV files Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
Note on Program Design Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:
Notes on Homework #6 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
G. Pullaiah College of Engineering and Technology
Strings, Lists, and Files
CISC101 Reminders All assignments are now posted.
15-110: Principles of Computing
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Introduction to Computer Science
Loops and Simple Functions
COMPUTER PROGRAMMING SKILLS
Introduction to Computer Science
Introduction to Computer Science
Introduction to Programming
CISC101 Reminders Assignment 3 due today.
Numpy, pylab, matplotlib (follow-up)
Class code for pythonroom.com cchsp2cs
Functions Taken from notes by Dr. Neil Moore & Dr. Debby Keen
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Week 7 - Friday CS 113.
Presentation transcript:

Elements of a Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction to Computer Science, 2nd edition, by John Zelle and copyright notes by Prof. George Heineman of Worcester Polytechnic Institute) CS-1004, A-Term 2014 Elements of a Python Program

Elements of a Python Program Python Language Every input line (plus its continuations, if any) defines some action by Python interpreter I.e., line causes interpreter to DO something I.e., effect some change in state Examples already encountered:– Expression:– evaluate the expression and print result on output Assignment associate a value or object with a name Three more this week:– Function definitions Function calls Definite loops CS-1004, A-Term 2014 Elements of a Python Program

Elements of a Python Program Function A sequence of one or more lines of Python code that can be invoked as a group under a single name A way of encapsulating a subset of a program that is used repeatedly … … so that the user (or using program) doesn‘t have to write it multiple times doesn‘t have to think about it works again doesn’t have to even know how it works at all It is not humanly possible for an ordinary person to hold an entire program in his/her head at one time! Therefore, we partition it into functions — many small functions! CS-1004, A-Term 2014 Elements of a Python Program

Elements of a Python Program Defining functions Colon denotes next line is continuation def functionName( ): python statement python statement ... python statement return <optional expression> Note: continuation lines may, themselves, have continuations Indented two units of continuation In general, continuation of n-level indentation is (n+1) units of indentation Each continuation line is indented one “unit” End of function definition denoted by reversion to previous indentation level CS-1004, A-Term 2014 Elements of a Python Program

Elements of a Python Program “Calling” a function a.k.a., “invoking” a function Function_name followed by parentheses Containing zero or more “arguments” Meaning:– “DO the function” I.e., execute its body as if you had typed it into a Python “shell” Or in an expression Definition: BODY (of a function) The statements following the “:” Ending with the last indented line Examples:– Area of disk Greet function (p. 11 of textbook) CS-1004, A-Term 2014 Elements of a Python Program

So what’s with the parentheses? Any function may be designed to take arguments I.e., a means of conveying information into a function by caller Examples: sin(radians) # Sine of that angle sqrt(nonNegativeNumber) # square root atan(number) # arctangent CS-1004, A-Term 2014 Elements of a Python Program

So what’s with the parentheses? (continued) Every function definition must include zero or more parameters between the parentheses I.e., identifiers separated by commas Place to receive the information provided by caller Example Area of disk Most functions should have one or more return statements Means of communicating result back to caller Needed if function call is part of expression More about parameters and returns later in course CS-1004, A-Term 2014 Elements of a Python Program

So what’s with the parentheses? (continued) Parentheses on function definition:– Place to put the parameters Error to leave off parameters Parentheses on function call:– Place to put arguments Each argument is expression Separated by commas Tells Python:– Evaluate arguments in order Assign to corresponding parameter Suspend whatever it was doing Interpret body of function (using argument values) Deliver (optional) result back to caller Resume whatever it was previously doing Example CS-1004, A-Term 2014 Elements of a Python Program

Elements of a Python Program Questions? CS-1004, A-Term 2014 Elements of a Python Program

Elements of a Python Program Digression Homework #2 (and all subsequent assignments) are to be completed by two-person teams Please choose a partner Send partner information to cs1004-staff@cs.wpi.edu If you do not have a partner by the time HW2 is assigned (on Thursday), … … a partner will be assigned to you! CS-1004, A-Term 2014 Elements of a Python Program

Elements of a Python Program Module A separate file with .py extension in name Contains function definitions And possibly other stuff Packaged for convenient organization And sanity on part of programmer And programmer’s boss or advisor! Lauer’s advice to rising programmers:– Any program that is more than a couple of pages in length is too long to wrap your head around! Divide it into smaller “modules” organized around related groups of functions CS-1004, A-Term 2014 Elements of a Python Program

Elements of a Python Program Reading Assignments Study §1.7 Especially range, for-loop Note specification of a module Chapter 2, §2.1–2.5 Need to complete Chapter 2 by end of week! CS-1004, A-Term 2014 Elements of a Python Program

Elements of a Python Program Questions? CS-1004, A-Term 2014 Elements of a Python Program

Laboratory assignment tomorrow Define and use functions Including with parameters Will also use Definite loops I.e., a for-loop with a fixed number of iterations Read §1.7 in textbook CS-1004, A-Term 2014 Elements of a Python Program