Genome Sciences 373 Genome Informatics Quiz Section 5 April 28, 2015.

Slides:



Advertisements
Similar presentations
Solve problems with Java code Algorithms with Java.
Advertisements

Python Programming Chapter 5: Fruitful Functions Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Exercise 4 1. Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Genome Sciences 373 Genome Informatics Quiz Section 5 April 28, 2015.
Recursion Introduction to Computing Science and Programming I.
CS150 Introduction to Computer Science 1
1 Functions Samuel Marateck © A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the.
General Computer Science for Engineers CISC 106 Lecture 25 Dr. John Cavazos Computer and Information Sciences 04/20/2009.
Genome Sciences 373 Genome Informatics Quiz Section #1 March 31, 2015.
Genome Sciences 373 Genome Informatics Quiz Section 2 April 7, 2015.
1 11/8/06CS150 Introduction to Computer Science 1 More Functions! page 343 November 8, 2006.
Recursion. Sum a list of numbers Iterative def sum(L): total = 0 for i in L: total += i return total Recursive def sum(L): if len(L) == 0: return 0 else:
CHAPTER 6 Functions. Function Overview We’ve used built-in functions:  Examples:  print(“ABC”, x+10, sep=“:”)  round(x * 5, 2)  pygame.draw.circle(screen,
Genome Sciences 373 Genome Informatics Quiz Section 4 April 21, 2015.
Functions.
 Experiment 07 Function Dr. rer.nat. Jing LU
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Functions CMSC 201. Motivation Using the tools we have so far, we can easily write code to find the largest number in a list, right?
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
Chapter 8 More On Functions. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. First cut, scope.
Ch. 10 For Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
Functions CMSC 201. Motivation Using the tools we have so far, we can easily write code to find the largest number in a list, right?
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.
Lecture 15 – more on lists and for…in loops, the split() function COMPSCI 1 1 Principles of Programming.
Exercise 3 Example> Write a C function that implements /* input : integer value n output : */ int f ( int n ) { int i, sum=0; for (i=1;i
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
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 Files and Lists. Files  Chapter 9 actually introduces you to opening up files for reading  Chapter 14 has more on file I/O  Python can read.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
Introduction to Computing Science and Programming I
Fundamentals of Programming I Managing the Namespace
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
CMSC201 Computer Science I for Majors Lecture 16 – Recursion
Arrays & Functions Lesson xx
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Python Lessons 9 & 10 Mr. Kalmes.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Passing Parameters by value
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Iteration: Beyond the Basic PERFORM
Introduction to Python: Day Three
Writing Functions( ) (Part 4)
Python Functions Part I Presented by : Sharmin Abdullah
Suppose I want to add all the even integers from 1 to 100 (inclusive)
G. Pullaiah College of Engineering and Technology
National Central University, Taiwan
COMPUTER PROGRAMMING SKILLS
CSE 231 Lab 4.
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Python Functions.
Errors.
Functions So far we've been using functions from the builtin module, which is automatically loaded in most cases. Occasionally we've accessed functions.
IST256 : Applications Programming for Information Systems
REPETITION Why Repetition?
Functions John R. Woodward.
Recursion Examples.
Lecture 6 - Recursion.
Presentation transcript:

Genome Sciences 373 Genome Informatics Quiz Section 5 April 28, 2015

Functions in Python: a brief overview Functions are: reusable pieces of code, that take zero or more arguments, perform some actions, and return one or more values

Functions in Python: a brief overview Functions are: reusable pieces of code, that take zero or more arguments, perform some actions, and return one or more values function “sum” takes arguments a, b adds a and b returns sum conceptually

Functions in Python: a brief overview Functions are: reusable pieces of code, that take zero or more arguments, perform some actions, and return one or more values function “sum” takes arguments a, b adds a and b returns sum conceptually def sum(a, b): total = a + b return total # later in the program my_sum = sum(2, 5) # my_sum is now 7 in python…

Functions in Python: a brief overview Functions are: reusable pieces of code, that take zero or more arguments, perform some actions, and return one or more values def sum(a, b): total = a + b return total # later in the program my_sum = sum(2, 5) print total # this won’t work! in python… stuff that happens in here is invisible outside of the function

function to find Jukes-Cantor distance

Functions in Python: returning stuff Functions can return more than one value: def sum_and_product(myList): num_sum = 0 num_product = 1 for num in myList: num_sum += num num_product *= num return [num_sum, num_product] >>> x = sum_and_product([1, 2, 4]) >>> print x [7, 8]

In-class example: Write a function to calculate the factorial of an integer

Inputs: integer >= 0 Outputs: integer > 0

The intuitive approach def factorial(x): my_total = 1 while x > 1: my_total = my_total * x x -= 1 return my_total print factorial(10) # prints

def fact(x): if x == 2: return x else: return x * fact(x - 1) print fact(10) # (will print ) The recursion approach

def fact(x): if x == 2: return x else: return x * fact(x - 1) print fact(10) step 1: 10 * fact (10 – 1)

def fact(x): if x == 2: return x else: return x * fact(x - 1) print fact(10) step 1: 10 * fact (10 – 1) step 2: 10 * 9 * fact(9 – 1)

def fact(x): if x == 2: return x else: return x * fact(x - 1) print fact(10) step 1: 10 * fact (10 – 1) step 2: 10 * 9 * fact(9 – 1) step 3: 10 * 9 * 8 * fact(8 – 1)

def fact(x): if x == 2: return x else: return x * fact(x - 1) print fact(10) step 1: 10 * fact (10 – 1) step 2: 10 * 9 * fact(9 – 1) step 3: 10 * 9 * 8 * fact(8 – 1) step 4: 10 * 9 * 8 * 7 * fact(7 – 1) […] step 8: 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * fact(3-1)

def fact(x): if x == 2: return x else: return x * fact(x - 1) print fact(10) step 1: 10 * fact (10 – 1) step 2: 10 * 9 * fact(9 – 1) step 3: 10 * 9 * 8 * fact(8 – 1) step 4: 10 * 9 * 8 * 7 * fact(7 – 1) […] step 8: 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * fact(3-1)

def fact(x): if x == 2: return x else: return x * fact(x - 1) print fact(10) step 1: 10 * fact (10 – 1) step 2: 10 * 9 * fact(9 – 1) step 3: 10 * 9 * 8 * fact(8 – 1) step 4: 10 * 9 * 8 * 7 * fact(7 – 1) […] step 8: 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * fact(3-1) step 9: 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 # done!

def fact(x): if x < 0: # write error and exit sys.stderr.write(…) if x == 0 or x == 1: return 1 if x == 2: return x else: return x * fact(x - 1) A more complete version…

def less_than(myList, num): new_list = [] for x in myList: if x < num: new_list.append(x) return new_list >>> my_list = [1, 2, 3] >>> m = less_than(my_list) >>> print m TypeError: less_than() takes exactly 2 arguments (1 given) Functions in Python: required arguments

def less_than(myList, num): new_list = [] for x in myList: if x < num: new_list.append(x) return new_list >>> my_list = [1, 2, 3] >>> m = less_than(my_list, 3) >>> print m [1, 2] Functions in Python: required arguments

def less_than(myList, num=4): new_list = [] for x in myList: if x < num: new_list.append(x) return new_list >>> my_list = [1, 2, 3] >>> m = less_than(my_list) >>> print m [1, 2, 3] Functions in Python: default arguments

def less_than(myList, num=4): new_list = [] for x in myList: if x < num: new_list.append(x) return new_list >>> my_list = [1, 2, 3] >>> m = less_than(my_list, 2) >>> print m [1] Functions in Python: default arguments You can override default arguments