Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”

Slides:



Advertisements
Similar presentations
FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน สามารถใช้ชื่อใดๆ ตามกฎการตั้งชื่อ.
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1.
Introduction to Computing Using Python Exceptions (Oops! When things go wrong)  Errors and Exceptions  Syntax errors  State (execution) errors.
Q and A for Chapter 3.4 – 3.14 CS 104 Victor Norman.
Jay Summet CS 1 with Robots IPRE Python Review 1.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
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.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Introduction to Python
Chapter 2 Writing Simple Programs
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏
Python Programming Chapter 3: Functions Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Functions in Python. The indentation matters… First line with less indentation is considered to be outside of the function definition. Defining Functions.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Programming for Linguists An Introduction to Python 17/11/2011.
CS 100: Roadmap to Computing Fall 2014 Lecture 01.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Programming Training Main Points: - Python Statements - Problems with selections.
Functions and Modules CSIS 1595: Fundamentals of Programming and Problem Solving 1.
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.
Chapter 4 Numbers. Python Program Structure Python programs consist of: Modules Statements Expressions Objects.
Computing with Numbers Zelle - Chapter 3 Charles Severance - Textbook: Python Programming: An Introduction to Computer Science, John.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
H3D API Training  Part 3.1: Python – Quick overview.
PhD, Senior Lecturer, Baimuratov Olimzhon A LGORITHMS & P ROGRAMMING (P YTHON ) Lecture 3 From SDU:
General Computer Science for Engineers CISC 106 Lecture 04 Dr. John Cavazos Computer and Information Sciences 09/10/2010.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
Functions Chapter 4 Python for Informatics: Exploring Information
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Variables, Expressions and Statements
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Python Conditionals chapter 5
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏ Sec 9-7 Web Design.
Python – Part 3 Functions 1. Getting help Start the Python interpreter and type help() to start the online help utility. Or you can type help(print) to.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Python Functions : chapter 3
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
INLS 560 – F UNCTIONS Instructor: Jason Carter.
Chapter 3: User-Defined Functions I
Function and Function call Functions name programs Functions can be defined: def myFunction( ): function body (indented) Functions can be called: myFunction(
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
Introduction to Computing Using Python Exceptions (Oops! When things go wrong)  Errors and Exceptions  Syntax errors  State (execution) errors.
Data Manipulation Variables, Data Types & Math. Aug Variables A variable is a name (identifier) that points to a value. They are useful to store.
Functions Chapter 4 Python for Informatics: Exploring Information Slightly modified by Recep Kaya Göktaş on March 2015.
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.
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.
Python Functions : Chapter 3 Working wit text: Chapter 4.
Chapter 2 Writing Simple Programs
Python Review 1.
Introduction to Python
Module 3 Introduction to Python – variables, expressions and statements, evaluation of expressions, precedence, string operations . Functions, calling.
Functions Chapter 4 Python for Everybody
Presented By S.Yamuna AP/IT
Python - Functions.
Functions CIS 40 – Introduction to Programming in Python
Functions, Procedures, and Abstraction
(Oops! When things go wrong)
Variables, Data Types & Math
Python for Informatics: Exploring Information
Variables, Data Types & Math
Terminal-Based Programs
Variables, Data Types & Math
COMPUTER PROGRAMMING SKILLS
General Computer Science for Engineers CISC 106 Lecture 03
CSE 190p University of Washington Michael Ernst
Functions, Procedures, and Abstraction
Presentation transcript:

Python – Part 3 Functions 1

Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call” function by name >>> type(32) – Function name – type – Argument Prepared by Department of Preparatory year

Function Calls Commonly “takes” an argument “returns” a result Result called the return value. 3 Prepared by Department of Preparatory year

Type conversion functions Built-in functions that convert values from one type to another >>>int (’32’) 32 >>>int (‘Hello’) ValueError: invalid literal for int(): Hello >>>int (3.9999) 3 >>> int (-2.3) -2 4 Prepared by Department of Preparatory year

Type conversion functions >>>float (32) 32.0 >>>float (‘ ’) >>> str (32) ’32’ >>> str ( ) ‘ ’ 5 Prepared by Department of Preparatory year

Math functions Math module – provides most of the familiar mathematical functions Module – a file that contains a collection of related functions 6 Prepared by Department of Preparatory year

Math Functions Import the module before using it >>> import math Creates a module object named math >>> print math Prints some information about it 7 Prepared by Department of Preparatory year

Math functions >>> ratio = signal_power / noise_power >>> decibels = 10 * math.log10(ratio) #computes logarithm base 10 of ratio >>> radians = 0.7 >>> height = math.sin(radians) #computes sine of radians 8 Prepared by Department of Preparatory year

Math functions >>> degrees=45 >>> radians=degrees/360.0*2*math.pi >>>math.sin(radians) >>>math.sqrt(2)/ Prepared by Department of Preparatory year

Composition Can use functions to compose more complex expressions x=math.sin(degrees/360.0*2*math.pi) >>>minutes=hours*60 #right >>>hours*60=minutes #wrong! SyntaxError: can’t assign to operator 10 Prepared by Department of Preparatory year

Adding new functions Function definition – Name of a new function – Sequence of statements that execute when function is called def print_lyrics(): print ("I'm a lumberjack, and I'm okay.“) print ("I sleep all night and I work all day." ) 11 Prepared by Department of Preparatory year

Adding new functions def – keyword for function definition print_lyrics – name of the function () – no arguments Function name –same rules as for variables Avoid using variable and function with same name 12 Prepared by Department of Preparatory year

Adding new functions Header – first line of the function definition – Ends in colon Body – the rest – Has to be indented (always four spaces) Empty line to end the function (not necessary in a script) 13 Prepared by Department of Preparatory year

Adding new functions Defining a function creates a variable with the same name >>> print_lyrics() def repeat_lyrics(): print_lyrics() 14 Prepared by Department of Preparatory year

Parameters and Arguments math.pow(2,3) 8.0 def print_twice(bruce): print bruce Assigns the argument to a parameter named bruce 15 Prepared by Department of Preparatory year

Parameters and Arguments >>>print_twice (‘spam’) spam >>>print_twice(17) Prepared by Department of Preparatory year

Parameters and Arguments >>>print_twice(‘spam’*2) spam Argument evaluated before function is called 17 Prepared by Department of Preparatory year

Parameters and Arguments Can also use a variable as an argument >>>number=17 >>>print_twice(number) Prepared by Department of Preparatory year

Local variables and parameters Variable inside a function is local Exists only inside the function def cat_twice(part1, part2): cat = part1 + part2 print_twice(cat) This function takes two arguments, concatenates them, and prints the result twice. 19 Prepared by Department of Preparatory year

Local variables (cont’d…) >>> line1 = 'Bing‘ >>> line2 = 'bang.' >>> cat_twice(line1, line2) Bing bang. 20 Prepared by Department of Preparatory year

Local variables (cont’d …) When cat_twice terminates, the variable cat is destroyed. If we try to print it, we get an exception: >>> print cat NameError: name 'cat' is not defined 21 Prepared by Department of Preparatory year

Void functions print_twice Perform and action but does not return a value If function returns value, almost always use it as part of an expression: x = math.cos(radians) golden = (math.sqrt(5) + 1) / 2 22 Prepared by Department of Preparatory year

Void functions >>> result = print_twice('Bing') Bing >>> print result None 23 Prepared by Department of Preparatory year

Part 3 End 24