Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new.

Slides:



Advertisements
Similar presentations
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Advertisements

Introduction to Computing Using Python Imperative Programming  Python Programs  Interactive Input/Output  One-Way and Two-Way if Statements  for Loops.
Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
CHAPTER 6 FILE PROCESSING. 2 Introduction  The most convenient way to process involving large data sets is to store them into a file for later processing.
Functions and abstraction Michael Ernst UW CSE 190p Summer 2012.
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.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Introduction to Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
CS 100: Roadmap to Computing Fall 2014 Lecture 01.
Introduction to Computing Using Python Python  Python is an interactive language.  Java or C++: compile, run  Also, a main function or method  Python:
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
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
Course A201: Introduction to Programming 11/04/2010.
Introduction to Computing Using Python Straight-line code  In chapter 2, code was “straight-line”; the Python statements in a file (or entered into the.
Introduction to Computing Using Python Imperative Programming  what is a Python program?  print() statement  input() statement  type conversion statements.
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 5 Function Interfaces 4/18/09 Python Mini-Course: Day 2 - Lesson 5 1.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Python Functions.
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.
‘Tirgul’ # 2 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2.
Python Functions : chapter 3
Lecture 4 Functions. 2 CodeCademy 4.a. 3 CodeCademy: comments.  As parameter of a function  Second way to introduce variables:  What do max, min,
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.
EECS 110: Lec 3: Data Aleksandar Kuzmanovic Northwestern University
Debugging and Printing George Mason University. Today’s topics Review of Chapter 3: Printing and Debugging Go over examples and questions debugging in.
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
6. FUNCTIONS Rocky K. C. Chang October 4, 2015 (Adapted from John Zelle’s slides)
PYTHON FUNCTIONS. What you should already know Example: f(x) = 2 * x f(3) = 6 f(3)  using f() 3 is the x input parameter 6 is the output of f(3)
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
Topic: Functions – Part 1
Introduction to Programming
Introduction to Python
COMPSCI 107 Computer Science Fundamentals
CS 100: Roadmap to Computing
Chapter 3 Instructor: Zhuojun Duan
Topic: Functions – Part 2
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
Functions.
Python Data Types Expressions, Variables, and Assignments Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Imperative Programming
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Programming
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
G. Pullaiah College of Engineering and Technology
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
COMPUTER PROGRAMMING SKILLS
Introduction to Computer Science
CS 100: Roadmap to Computing
Introduction to Programming
What is a Function? Takes one or more arguments, or perhaps none at all Performs an operation Returns one or more values, or perhaps none at all Similar.
CISC101 Reminders Assignment 3 due today.
def-ining a function A function as an execution control structure
 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.
Imperative Programming
Functions John R. Woodward.
© Sangeeta M Chauhan, Gwalior
Using Modules.
Presentation transcript:

Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new function  Passing and using function parameters  return- ing a value from a function

Introduction to Computing Using Python Function calls and execution control A function call is another kind of statement that alters the order in which statements in a program are executed A function call creates a temporary ‘excursion,’ causing the sequence of execution to jump to the named function. When the function exits, execution returns to the point where the function call occurred. We've already seen these built-in function calls: len(), type(), min(), max(), str(), bool(),... Today we are going to learn how to write our own functions how to specify what parameters are passed to our functions how to return information from functions

Introduction to Computing Using Python Defining a new function We have seen these calls to built-in functions: abs(), max(), len(), sum(), print() You can define a new function using def def iSquaredPlus10(x): result = x** return result def iSquaredPlus10(x): result = x** return result def : function definition keyword iSquaredPlus10 : name of function x : variable name for input argument return : specifies the value that the function returns (default: None) >>> abs(-9) 9 >>> max(2, 4) 4 >>> lst = [2,3,4,5] >>> len(lst) 4 >>> sum(lst) 14 >>> print() >>> def iSquaredPlus10(x): result = x** return result >>> f(1) 11 >>> f(3) 19 >>> f(0) 10 >>> abs(-9) 9 >>> max(2, 4) 4 >>> lst = [2,3,4,5] >>> len(lst) 4 >>> sum(lst) 14 >>> print() >>> def iSquaredPlus10(x): result = x** return result >>> f(1) 11 >>> f(3) 19 >>> f(0) 10

Introduction to Computing Using Python print() versus return def iSquaredPlus10(x): result = x** return result squarePlusTen = iSquaredPlus10(5) print(squarePlusTen) def iSquaredPlus10(x): result = x** return result squarePlusTen = iSquaredPlus10(5) print(squarePlusTen) def iSquaredPlus10 (x): result = x** print(result) def iSquaredPlus10 (x): result = x** print(result) return returns the sequence of execution to the point of where the function is called and (optionally) sends a value (which can be any type) back to the function call. Function returns value of res which can then be used in an expression Function prints value of res but does not return anything print() sends text to the screen. Execution proceeds to the next line of code.

Introduction to Computing Using Python Defining a new function def ( ): def ( ): a b c >>> hyp(3,4) 5.0 >>> >>> hyp(3,4) 5.0 >>> The general format of a function definition is Let’s develop function hyp() that: Takes two numbers as input (side lengths a and b of above right triangle ) Returns the length of the hypotenuse c import math def hyp(a, b): result = math.sqrt(a**2 + b**2) return result import math def hyp(a, b): result = math.sqrt(a**2 + b**2) return result

Introduction to Computing Using Python Exercise >>> hello('Julie') Welcome, Julie, to the world of Python. >>> >>> hello('Julie') Welcome, Julie, to the world of Python. >>> def hello(name): Write function hello() that: takes a name (i.e., a string) as input prints a personalized welcome message Note that the function does not return anything def hello(name): line = 'Welcome, ' + name + ', to the world of Python.’ def hello(name): line = 'Welcome, ' + name + ', to the world of Python.’ def hello(name): line = 'Welcome, ' + name + ', to the world of Python.' print(line) def hello(name): line = 'Welcome, ' + name + ', to the world of Python.' print(line)

Introduction to Computing Using Python Exercise >>> oddCount([4, 0, 1, -2]) 1 >>> >>> oddCount([4, 0, 1, -2]) 1 >>> def rng(lst): Write function oddCount() that: takes a list of numbers as input returns the number of odd numbers in the list def rng(lst): res = max(lst) - min(lst) def rng(lst): res = max(lst) - min(lst) def oddCount(lst): count = 0 for i in lst: if i%2 == 1: count += 1 # same as count = count+1 return count def oddCount(lst): count = 0 for i in lst: if i%2 == 1: count += 1 # same as count = count+1 return count

Introduction to Computing Using Python Comments and docstrings Python programs should be documented So the program’s ‘mission’ is well defined So the developer who writes/maintains the code understands it So the user knows what the program does Comments def iSquaredPlus10(x): result = x** # compute result return res # and return it def iSquaredPlus10(x): result = x** # compute result return res # and return it def f(x): 'returns x**2 + 10' res = x** # compute result return res # and return it def f(x): 'returns x**2 + 10' res = x** # compute result return res # and return it Docstring >>> help(iSquaredPlus10) Help on function iSquaredPlus10 in module __main__: iSquaredPlus10(x) >>> def iSquaredPlus10 (x): 'returns x**2 + 10' result = x** return result >>> help(iSquaredPlus10) Help on function iSquaredPlus10 in module __main__: iSquaredPlus10(x) returns x** >>> help(iSquaredPlus10) Help on function iSquaredPlus10 in module __main__: iSquaredPlus10(x) >>> def iSquaredPlus10 (x): 'returns x**2 + 10' result = x** return result >>> help(iSquaredPlus10) Help on function iSquaredPlus10 in module __main__: iSquaredPlus10(x) returns x**2 + 10