Course A201: Introduction to Programming 11/04/2010.

Slides:



Advertisements
Similar presentations
Course A201: Introduction to Programming 10/28/2010.
Advertisements

Python Basics: Statements Expressions Loops Strings Functions.
Do I need a robot today? You only need a robot if your team has not shown me your octagon and/or the octagon in a function.
Course A201: Introduction to Programming 11/11/2010.
Guide to Programming with Python
Functions Jay Summet. Aug Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 2.
CS 1 with Robots Functions Institute for Personal Robots in Education (IPRE)‏
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Introduction to Python
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.
CS61A Lecture 2 Functions and Applicative Model of Computation Tom Magrino and Jon Kotker UC Berkeley EECS June 19, 2012.
CS 100: Roadmap to Computing Fall 2014 Lecture 01.
Course A201: Introduction to Programming 09/09/2010.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
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,
Functions and subroutines – Computer and Programming.
Guide to Programming with Python Chapter Six Functions: The Tic-Tac-Toe Game.
General Computer Science for Engineers CISC 106 Lecture 04 Dr. John Cavazos Computer and Information Sciences 09/10/2010.
Built-in Data Structures in Python An Introduction.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Python Functions.
Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new.
Course A201: Introduction to Programming 09/30/2010.
ProgLan Python Session 4. Functions are a convenient way to divide your code into useful blocks, allowing us to: order our code, make it more readable,
Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! We have agreed so far that it can.
1 Functions, Part 1 of 2 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function Header Comments.
INLS 560 – F UNCTIONS Instructor: Jason Carter.
Python Let’s get started!.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
Lists Victor Norman CS104. Reading Quiz Lists Our second collection data type – elements are in order (like strings) – indexed from 0 to n – 1 (like.
Guide to Programming with Python Chapter Six Functions: The Tic-Tac-Toe Game.
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.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
LECTURE 2 Python Basics. MODULES So, we just put together our first real Python program. Let’s say we store this program in a file called fib.py. We have.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Course A201: Introduction to Programming 09/09/2010.
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.
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.
Introduction to Python
Topic: Functions – Part 1
Course A201: Introduction to Programming
Introduction to Programming
Python Let’s get started!.
Functions Jay Summet.
loops for loops iterate over a given sequence.
Review.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Introduction to Programming
Python Functions Part I Presented by : Sharmin Abdullah
Functions Institute for Personal Robots in Education (IPRE)‏
Functions Jay Summet.
Introduction to Python
Functions Institute for Personal Robots in Education (IPRE)‏
COMPUTER PROGRAMMING SKILLS
Functions Institute for Personal Robots in Education (IPRE)‏
Functions Jay Summet.
Introduction to Computer Science
CMSC201 Computer Science I for Majors Lecture 16 – Tuples
Introduction to Programming
Course A201: Introduction to Programming
def-ining a function A function as an execution control structure
Functions Jay Summet.
Functions John R. Woodward.
Selamat Datang di “Programming Essentials in Python”
Presentation transcript:

Course A201: Introduction to Programming 11/04/2010

Outline for this week Last week’s lab assignment and recap Function – General structure – How to define a function; call a function – Return values – Parameters and Arguments

Last week’s in lab assignment Simple list manipulation

Recap Sequences we’ve learned so far: – String: ‘hello’, ‘goodbye’ – Tuple: (‘hello’, ‘begin with h’), (‘goodbye’, ‘end with e’) – List: [‘hello’, ‘goodbye’, 1, 0.9] – Dictionary: {1:‘hello’, 2:‘goodbye’} Useful methods belongs to each type

Recall Function [functions] I am a function. I will perform a certain job. input1, input2, input3, … output1, output2, output3, … You give me some inputs I give you some outputs back, explicitly or implicitly

Functions Built-in function len(): – Input: a sequence – output: an integer – Job: count how many items are there in this sequence print(): – Input: any type of value – Output: None – Job: print out the inputs

Functions: define and call def circle_area(radius): return ( * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) This is the definition of a function. This block define what job will circle_area perform, input and output. This is how you call the function: name and parentheses

Functions: define and call def instructions(): print(“““Welcome to the greatest intellectual challenge of all time: Tic-Tac-Toe. This will be a showdown between your human brain and my silicon processor.”””) instructions() This is how you call the function: name and parentheses This is the definition.

Functions: Return values So, from the above two examples: – Function circle_area() does return a value, which is the area of a circle – Function instructions() does NOT return a value, and by default the return value will be None The keyword return

Functions: Return values See lecture slides: catching a return value

Parameters and Arguments def circle_area(radius): return ( * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) Parameters Arguments

Parameters and Arguments def add(a, b): return (a+b) add(2, 4) Parameters Arguments

Parameters and Arguments Parameters are essentially variable names inside the parentheses of a function header: – add(a,b), a and b are parameters of the function add – You can have 0, 1 or many parameters The function receives values/arguments through its parameters e.g. add(1,2) – The code in the function add now treats a and b as variables with values 1 and 2

Parameters and Arguments See lecture slides: – positional parameters and arguments – Keyword argument – Default Parameter Values – Parameter Type

Default Parameters Values Once you assign a default value to a parameter in the list, you have to assign default values to all the parameters listed after it. So, this function header is perfectly fine: def monkey_around(bananas = 100, barrel_of = "yes"): But this isn't: def monkey_around(bananas = 100, barrel_of): The above header will generate an ERROR.

In Lab Assignments See Assignment 7

Have a nice evening! See you tomorrow~