COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University 2010-2011.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
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.
Fundamentals of Python: From First Programs Through Data Structures Chapter 2 Software Development, Data Types, and Expressions.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Intro to Robots Computer Brains and Python Functions.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Functions. A set of statements (lines of code) that can be run repeatedly Goals: Learning Python by Lutz and Ascher –Code reuse –Procedural decomposition.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Introduction to Python
Python Programming Chapter 3: Functions Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Introduction to C Programming
Guide to Programming with Python Chapter Two Basic data types, Variables, and Simple I/O: The Useless Trivia Program.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 3 Variables, Calculations, and Colors Starting Out with Games.
A First Book of ANSI C Fourth Edition
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
CS 100: Roadmap to Computing Fall 2014 Lecture 01.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Introduction to Python
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Fundamentals of Python: First Programs
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to Computational Linguistics Programming I.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
PhD, Senior Lecturer, Baimuratov Olimzhon A LGORITHMS & P ROGRAMMING (P YTHON ) Lecture 3 From SDU:
Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Input, Output, and Processing
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
Functions Chapter 4 Python for Informatics: Exploring Information
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Python Functions : chapter 3
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Values, Types, and Variables. Values Data Information Numbers Text Pretty much anything.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Topics Designing a Program Input, Processing, and Output
Introduction to Python
CS 100: Roadmap to Computing
Variables, Expressions, and IO
Chapter 3 Introduction to Classes, Objects Methods and Strings
Topics Introduction to File Input and Output
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Variables, Data Types & Math
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Terminal-Based Programs
COMPUTER PROGRAMMING SKILLS
Topics Introduction to File Input and Output
def-ining a function A function as an execution control structure
Presentation transcript:

COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University

Functions Function A named sequence of statements that performs some useful operation Functions may or may not take arguments and may or may not produce a result, named as return value. It is common to say that a function “takes” an argument and “returns” a result. Function Calls A statement that executes a function. It consists of the name of the function followed by a list of arguments enclosed in parentheses. You have already seen one example of a function call: >>> type("32") the name of the function is type it has as argument with value “32” it produces a result as the type of value and displays the result

Functions(cont.) Examples >>> id(3) >>> betty = 3 >>> id(betty) Every value has an id, which is a unique number related to where it is stored in the memory of the computer

Functions(cont.) Examples (cont.) Type conversion functions: int, float, str The int function takes any value and converts it to an integer, if possible, or complains otherwise: >>> int("32") 32 >>> int("Hello") ValueError: invalid literal for int(): Hello int can also convert floating-point values to integers, but remember that it truncates the fractional part: >>> int( ) 3 >>> int(-2.3) -2

Functions(cont.) Math Functions sin, cos, log, pi etc. Python has a math module A module is a file that contains a collection of related functions grouped together Before we can use the functions from a module, we have to import them >>> import math

Functions(cont.) Math Functions(cont.) To call one of the functions, we have to specify the name of the module and the name of the function, separated by a dot, also known as a period. This format is called dot notation. >>> decibel = math.log10 (17.0) >>> angle = 1.5 >>> height = math.sin(angle)

Functions(cont.) New Function specifying its name, parameters, and the statements. Use any name (cannot use keywords) include any number of statements inside The statements do not get executed until the function is called Syntax def NAME( LIST OF PARAMETERS ): STATEMENTS Example def newLine(): print () This function is named newLine. The empty parentheses indicate that it has no parameters. It contains only a single statement, which outputs a newline character.

Functions(cont.) New Function(cont.) Syntax for Calling New Function print "First Line." newLine() print "Second Line." Output: First line. Second line.

Functions(cont.) New Function(cont.) Exercise: Write a function that prints three lines, and name it threeLines. You can call the same procedure Repeatedly or You can have one function call another function; in this case threeLines calls newLine.

Functions(cont.) Parameters and Arguments arguments, the values that control how the function does its job. if you want to find the sine of a number, you have to indicate what the number is as an argument Inside the function, the values that are passed get assigned to variables called parameters. Here is an example of a user-defined function that has a parameter: def printTwice(bruce): print (bruce, bruce) Variables and parameters are local When you create a local variable inside a function, it only exists inside the function, and you cannot use it outside

Exercise Write a function called printParity. Example use is as following: printParity(17) Output: 17 is odd y = 17 printParity(y+1) Output: 18 is even

Exercise Solution

Return Values Some functions produce results, or return values import math def area(radius): temp = math.pi * radius**2 return temp This statement means: “Return immediately from this function and use the following expression as a return value.”

Exercise Write a compare function that returns 1 if x > y, 0 if x == y, and -1 if x < y.

Exercise As an example, suppose you want to find the distance between two points, given by the coordinates (x1, y1) and (x2, y2). By the Pythagorean theorem, the distance is:

Exercise Solution