Setting the PYTHONPATH PYTHONPATH is where Python looks for modules it is told to import List of paths Add new path to the end with: setenv PYTHONPATH.

Slides:



Advertisements
Similar presentations
Arithmetic Calculations
Advertisements

Chapter 4 Parameters and Overloading. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Learning Objectives Parameters Call-by-value Call-by-reference.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Methods Java 5.1 A quick overview of methods
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Lilian Blot PART V: FUNCTIONS Core elements Autumn 2013 TPOP 1.
Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.
15. Python - Modules A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand.
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.
Chapter Modules CSC1310 Fall Modules Modules Modules are the highest level program organization unit, usually correspond to source files and.
Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes.
Perkovic, Chapter 7 Functions revisited Python Namespaces
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Introduction to Computing Using Python Namespaces and Exceptions, revisited  Encapsulation in Functions  Global versus Local Namespaces  Exceptional.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Tcl and Otcl Tutorial Part I Internet Computing KUT Youn-Hee Han.
Concepts when Python retrieves a variable’s value Namespaces – Namespaces store information about identifiers and the values to which they are bound –
Structured programming
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Outline 4.1 Introduction 4.2 Program Components in Python 4.3 Functions 4.4Module.
 2002 Prentice Hall. All rights reserved. 1 Functions Purpose –Building blocks for the program (separate functionality in independant parts) –Avoid code.
BIL101, Introduction to Computers and Information Systems Chapter 12 A Portable Scientific Visualization Program: GnuPlot Prepared by Metin Demiralp Istanbul.
FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language.
1 Python Chapter 2 © Samuel Marateck, After you install the compiler, an icon labeled IDLE (Python GUI) will appear on the screen. If you click.
Introduction to Computing Using Python Dictionary container class + modules  Container Class dict  Modules, revisited.
CSC 110 Numeric data types [Reading: chapter 3] CSC 110 D 1.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language.
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,
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.
Modules and Decomposition UW CSE 190p Summer 2012 download examples from the calendar.
Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
1 Math Expressions and Operators. 2 Some C++ Operators Precedence OperatorDescription Higher ( )Function call +Positive - Negative *Multiplication / Division.
Functions Chapter 4 Python for Informatics: Exploring Information
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 5: Functions Xiang Lian The University of Texas – Pan American Edinburg, TX
Python Functions.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
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?
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
Function and Function call Functions name programs Functions can be defined: def myFunction( ): function body (indented) Functions can be called: myFunction(
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
Functions Chapter 4 Python for Informatics: Exploring Information Slightly modified by Recep Kaya Göktaş on March 2015.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Useful Python CMSC 120: Visualizing Information. Scope def area(diameter): radius = diameter / 2.0 A = PI * radius ** 2 return A def circumference(diameter):
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 6 - Functions modular programming general function format
Python’s Modules Noah Black.
G. Pullaiah College of Engineering and Technology
Introduction to Programming
Introduction to Python
Catapult Python Programming Thursday Session
Functions Chapter 4 Python for Everybody
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
CHAPTER FOUR Functions.
Rocky K. C. Chang 15 November 2018 (Based on Dierbach)
Python for Informatics: Exploring Information
G. Pullaiah College of Engineering and Technology
CHAPTER 3: String And Numeric Data In Python
Terminal-Based Programs
What is a Function? Takes one or more arguments, or perhaps none at all Performs an operation Returns one or more values, or perhaps none at all Similar.
Python Modules.
Presentation transcript:

Setting the PYTHONPATH PYTHONPATH is where Python looks for modules it is told to import List of paths Add new path to the end with: setenv PYTHONPATH ${PYTHONPATH}:/users/chili/Pythonmodules/ export PYTHONPATH=${PYTHONPATH}:/users/chili/Pythonmodules/ (C shell and Bash, respectively)

Permanently adding path to PYTHONPATH Create/load file called ~/.daimi-setup/tcsh/tcshrc.d/path.rc Add the magic line to it To later add more paths: setenv PYTHONPATH ${PYTHONPATH}:/users/chili/Pythonmodules/:/users/chili /Pythonmodules/RealGoodies/

def f(a): q = 7 z = 13 f(z) global namespace local namespace z a q 13 7 f How arguments are transferred in a function call function object

def f(a): q = 7 a = 8 z = 13 f(z) print z global namespace local namespace z aqaq 13 7 How arguments are transferred in a function call 8 f function object

Retrieving a variable’s value built_in global local import math def f(a): q = 7 print y x = 9 z = 13 f(z) raw_input, int, float,.. __name__ = ‘__main__’, math, f, x = 9, z = 13,.. a = 13, q = 7 ? NameError: global name ‘y’ is not defined

built_in global local import math def f(a): x = 7 print x x = 9 z = 13 f(z) print x raw_input, int, float,.. __name__ = ‘__main__’, math, f, x = 9, z = 13,.. a = 13, x = 7 local x global x Value of x..? 9. Two x ’es

built_in global local import math def f(a): global x x = 7 x = 9 z = 13 f(z) print x range, raw_input, int, float,.. __name__ = ‘__main__’, math, f, x =.., z = 13,.. a = 13 global x Value of x..? 7! Keyword global no x here

Scope of a variable The region of the program where the variable is accessible Using a variable outside its scope: def area(r): pi = 3.15 return pi*r*r* a = area(1.0) print pi a = area(1.0) def area(r): pi = 3.15 return pi*r*r*  

Importing only parts of a module The from/import statement – For importing only a specific part of a module – Takes identifiers from a module and inserts them directly into the current program’s name space (avoids e.g. math. prefix) >>> from math import sqrt >>> >>> dir() ['__builtins__', '__doc__', '__name__', 'sqrt'] >>> >>> sqrt( 9.0 ) 3.0 >>> >>> from math import sin, cos, tan >>> >>> dir() ['__builtins__', '__doc__', '__name__', 'cos', 'sin', 'sqrt', 'tan'] Now directly a part of the current namespace

Directly importing all identifiers from a module >>> from math import * >>> >>> dir() ['__builtins__', '__doc__', '__name__', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp','log', 'log10', 'modf', 'pi', 'pow', 'sin', 'sinh', 'sqrt', 'tan', 'tanh'] Danger! If you already have a log() function with a different purpose (perhaps you are a captain and wrote the function to access the log of your spaceship..), it will be shadowed! Either just import math and use math.log(), or..

Binding new names to modules and module functions from import as – imports a function but gives it a different name: from math import log as logarithm You can also give a new name to an entire module: – import math as mathtoolbox

Example: a function for computing the factorial of n 1! = 1 2! = 2*1 7! = 7*6*5*4*3*2*1 n! = n*((n-1)!) I.e. the factorial can be defined in terms of itself Algorithm: If n < 2: fac = 1 Otherwise fac = n*fac(n-1)!

A recursive function This code is only executed if the file is python’ed directly, not if it is imported. factorial.py Documentation string. Printed by help() if called on this function

threonine:~...ProgramExamples% python factorial.py n: n: 4 24 n: -1 threonine:~...ProgramExamples% threonine:~...ExamplePrograms% python Python (#1, Feb , 12:11:53) [GCC (Red Hat fc3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> >>> from factorial import recursive_factorial as f >>> f(5) >>> 120 Executing file directly: Importing file:

Default Arguments A function may sometimes be called repeatedly with the same values – Default arguments can be set in function definition – Must appear to the right of undefaulted parameters: def myfunction( a, b = 2, c = 3 ): – Given values are ‘filled in from the left’: myfunction(6, 3) # set a and b, use default for c myfunction(6) # set a, use default for b and c myfunction(6, 3, 7) # set a, override default values

def boxVolume( length = 3, width = 2, height = 1 ): return length * width * height print "The default box volume is:", boxVolume() print "\nThe volume of a box with length 10," print "width 2 and height 1 is:", boxVolume( 10 ) print "\nThe volume of a box with length 10," print "width 5 and height 1 is:", boxVolume( 10, 5 ) print "\nThe volume of a box with length 10," print "width 5 and height 2 is:", boxVolume( 10, 5, 2 ) The default box volume is: 6 The volume of a box with length 10, width 2 and height 1 is: 20 The volume of a box with length 10, width 5 and height 1 is: 50 The volume of a box with length 10, width 5 and height 2 is: 100 All default values usedThe 10 will replace the 3 and the other default values will be used No default values used Here two values are sent replacing the two left-most default values

Keyword Arguments – You can use parameter names as keywords: – Allows arguments to be passed in any order: def myfunction( a, b ):.. myfunction ( b = 5, a = 9 )

Personal data: George, 53, president (US) Personal data: Tony, 53, prime minister (GB) Personal data: Ronald, 92, president (US) Some of the parameters have default values The parameters are given new values except age which uses the default. Keyword arguments need not be entered in order. Sets first argument, uses the defaults for all the others Uses keywords for two arguments, uses default values for the rest

How not to use keyword arguments >>> >>> def test( name, age = 10, town = “Canberra” ):... pass... >>> test( age = 30, “Alice" ) SyntaxError: non-keyword arg after keyword arg >>> >>> test( age = 30 ) Traceback (most recent call last): File " ", line 1, in ? TypeError: test() takes at least 1 non-keyword argument (0 given) Keyword arguments must appear to the right of any other arguments in a function call. Otherwise ambiguity may arise. Is “Alice” the value for name or town ? No value given for name which doesn’t have a default value Rule of thumb: arguments determined by position must come first

On to the exercises..