…and how to do stuff with them Copyright © The University of Southampton 2011 This work is licensed under the Creative Commons Attribution License See.

Slides:



Advertisements
Similar presentations
This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
Advertisements

Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Q and A for Chapter 3.4 – 3.14 CS 104 Victor Norman.
Introduction to Python. Python is a high-level programming language Open source and community driven “Batteries Included” – a standard distribution includes.
CS 106 Introduction to Computer Science I 02 / 27 / 2008 Instructor: Michael Eckmann.
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.
Main task -write me a program
VB – Core III Functions Sub-routines Parameter passing Modules Scope Lifetime.
James Tam Introduction To Files In Python In this section of notes you will learn how to read from and write to files in your programs.
James Tam Introduction To Files In Python In this section of notes you will learn how to read from and write to files in your programs.
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.
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
Python quick start guide
Python.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Introduction to Python John Reiser May 5 th, 2010.
Functions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Python Modules An Introduction. Introduction A module is a file containing Python definitions and statements. The file name is the module name with the.
Fall Week 3 CSCI-141 Scott C. Johnson.  Say we want to draw the following figure ◦ How would we go about doing this?
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
If statements while loop for loop
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Invasion Percolation: Assembly Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Intro to Python Adriane Huber Debbie Bartlett Python Lab #1Python Lab #1 1.
Functions Chapter 4 Python for Informatics: Exploring Information
Libraries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
+ Ruby and other programming Languages Ronald L. Ramos.
Introduction Copyright © Software Carpentry This work is licensed under the Creative Commons Attribution License See
First-Class Functions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Chapter 15. Modules Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
Functions Chapter 4 Python for Informatics: Exploring Information Slightly modified by Recep Kaya Göktaş on March 2015.
OOP Basics Classes & Methods (c) IDMS/SQL News
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
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.
First Steps in PHP Creating Very Simple PHP Scripts SoftUni Team Technical Trainers Software University
Computer Organization and Design Pointers, Arrays and Strings in C
Sets and Dictionaries Examples Copyright © Software Carpentry 2010
Writing Functions( ) (Part 5)
Call Stacks, Arguments and Returns
Introduction to Python
Passing Parameters by value
What is x? x = 12 %
More Basics of Python Common types of data we will work with
 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.
COMPUTING.
Presentation transcript:

…and how to do stuff with them Copyright © The University of Southampton 2011 This work is licensed under the Creative Commons Attribution License See for more information. Functions in Python

PythonPython Functions Language Design Philosophy Languages have inbuilt functions e.g. for strings Programming language shouldn’t try to include everything - It’s impossible - Would be impossible to learn Perl! Comparison of Perl, Quorum and Randomo* - Quorum: based on language usability research - Randomo: designed by random no. generator - How did Perl compare? *Andreas Stefik et al, “An Empirical Comparison of the Accuracy Rates of Novices using the Quorum, Perl, and Randomo Programming Languages”, PLATEAU 2011

PythonPython Functions Language Design Philosophy Instead: allow people to create what they need to solve specific problems Languages allow you to extend them by defining functions for higher-level operations Programming can be regarded as the act of creating a mini-language specific to a problem

PythonPython Functions …

PythonPython Functions What’s Happening with Greet? def greet(name): answer = ‘Hello, ‘ + name return answer temp = ‘Name’ ‘Name’ temp stackValue

PythonPython Functions What’s Happening with Greet? def greet(name): answer = ‘Hello, ‘ + name return answer temp = ‘Name’ result = greet(temp) name ‘Name’ temp stackvalue

PythonPython Functions What’s Happening with Greet? def greet(name): answer = ‘Hello, ‘ + name return answer temp = ‘Name’ result = greet(temp) name answer ‘Name’ temp ‘Hello, Name’ stackvalue

PythonPython Functions What’s Happening with Greet? def greet(name): answer = ‘Hello, ‘ + name return answer temp = ‘Name’ result = greet(temp) ‘Name’ temp result ‘Hello, Name’ stackvalue

PythonPython Functions What’s Happening with Greet? name answer ‘Name’ temp ‘Hello, Name’ stackvalue These stack entries are known as stack frames Determines scope of variables Only see variables in current, global frames - At any time - Current beats global Each frame is discarded when function returns global greet

PythonPython Functions …

PythonPython Functions Exercise 1 def greet(name): answer = ‘Hello, ‘ + name return answer In pairs Create a new function named exclaim() that takes one argument and is called by greet() exclaim(str) adds an exclamation mark to a string and returns it

PythonPython Functions Exercise 2 def sign(num): if num > 0: return 1 elif num == 0: return 0 else: return -1 In pairs rewrite sign() so that it only has one use of return

PythonPython Functions Exercise 3 – pair programming import sys if (len(sys.argv) < 2): sys.exit("Missing file name") filename = sys.argv[1] print "File name", filename source = open(filename, 'r') count = 0 expected = None # Count number of data records. for line in source: if line.startswith('#’): count = countLines(filename) pass elif line.startswith('D'): pass else: count += 1 source.close() print "Actual number of data records:", count With the intro.py program Mike showed you yesterday, take highlighted blue code above and move it into a new function that takes a filename parameter and returns the number of lines. It should display the line count at the end as before.

PythonPython Functions Exercise 4a Assume we have a separate Python library functions.py that contains our greet, sign and double functions Write a new Python program e.g. prog.py that Greets you by name Loops through the numbers 3, 0 and -9 and runs sign and double on them, printing each result Do not use, rewrite or run the greet, sign or double functions until you have finished prog.py!

PythonPython Functions Exercise 4b Take greet, sign and double functions and put them in a separate functions.py file, and import them Change prog.py to use them Now you can run your prog.py! Can also use ‘from functions use fun_a, fun_b, fun_c’ instead of import – don’t need to use ‘functions’ prefix. Change prog.py to use this method of importing functions

PythonPython Functions Exercise 5 Add in another mathematical function of your choice to functions.py and use it in the same fashion on the array arr