Geography 465 Modules and Functions Writing Custom Functions.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter Modules CSC1310 Fall Modules Modules Modules are the highest level program organization unit, usually correspond to source files and.
Intro to Python Welcome to the Wonderful world of GIS programing!
Why python? Automate processes Batch programming Faster Open source Easy recognition of errors Good for data management What is python? Scripting programming.
An Introduction to Python – Part IV Dr. Nancy Warter-Perez May 19, 2005.
An Introduction to Python – Part IV Dr. Nancy Warter-Perez.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
Chapter 11 ASP.NET JavaScript, Third Edition. 2 Objectives Learn about client/server architecture Study server-side scripting Create ASP.NET applications.
Writing Methods. Create the method Methods, like functions, do something They contain the code that performs the job Methods have two parts.
1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
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.
PYTHON: LESSON 1 Catherine and Annie. WHAT IS PYTHON ANYWAY?  Python is a programming language.  But what’s a programming language?  It’s a language.
Guide to Programming with Python Chapter Nine Working with/Creating Modules.
Python Programming Fundamentals
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.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormack 3rd floor 607.
COMPSCI 101 Principles of Programming Lecture 28 – Docstrings & Doctests.
Python Modules An Introduction. Introduction A module is a file containing Python definitions and statements. The file name is the module name with the.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Python – Part 1 Python Programming Language 1. What is Python? High-level language Interpreted – easy to test and use interactively Object-oriented Open-source.
Linux Operations and Administration
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
By James Braunsberg. What are Modules? Modules are files containing Python definitions and statements (ex. name.py) A module’s definitions can be imported.
For loops in programming Assumes you have seen assignment statements and print statements.
Overview Intro to functions What are functions? Why use functions? Defining functions Calling functions Documenting functions Top-down design Variable.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Introduction to Python By Neil Cook Twitter: njcuk Slides/Notes:
Using Simulator With Undertow Suite. Source environment variables For example, envsource has all the environment variables set up. You can change the.
CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2.
Visual Basic CDA College Limassol Campus COM123 Visual Programming 1 Semester B Lecture:Pelekanou Olga Week 5: Useful Functions and Procedures.
Function Basics. Function In this chapter, we will move on to explore a set of additional statements that create functions of our own function (subroutine,
CS2021 Python Programming Week 3 Systems Programming PP-Part II.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
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.
Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
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.
Modules. Modules Modules are the highest level program organization unit, usually correspond to source files and serve as libraries of tools. Each file.
LECTURE 2 Python Basics. MODULES So, we just put together our first real Python program. Let’s say we store this program in a file called fib.py. We have.
Python: File Directories What is a directory? A hierarchical file system that contains folders and files. Directory (root folder) Sub-directory (folder.
Tips. Iteration On a list: o group = ["Paul","Duncan","Jessica"] for person in group: print(group) On a dictionary: o stock = {'eggs':15, 'milk':3, 'sugar':28}
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Juancho Datu. What is a Module? File containing Python definitions and statements with the suffix ‘.py’ in the current directory For example file name:
Python’s Modules Noah Black.
G. Pullaiah College of Engineering and Technology
Lecture 2 Python Basics.
CS 1110 Introduction to Programming Spring 2017
CS 1110 Introduction to Programming Spring 2017
Python’s Modules by E. Esin GOKGOZ.
Procedures Programming Guides.
Passing Parameters by value
Rocky K. C. Chang 15 November 2018 (Based on Dierbach)
Geography 465 Managing Custom Python Script Tools
G. Pullaiah College of Engineering and Technology
Topics Introduction to Functions Defining and Calling a Function
Appending or adding to a file using python
Scripts In Matlab.
1-6 Midterm Review.
Python Modules.
The Python interpreter
 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.
Python Modules.
Learning Python 5th Edition
Week 7 - Friday CS 113.
Presentation transcript:

Geography 465 Modules and Functions Writing Custom Functions

Python Modules Each Python script is a module Modules can access other modules Modules can use variables and/or constants Modules can use functions

Writing a Function What makes a good function –Single, clear objective –Reusable by intent Function syntax –Def defines the function –Return() sends result back to calling module –See example

Syntax Example MyCustomFunction.py Def (arg1, arg2, …argN): # your custom function code return

Code example result of two numbers MyFunctions.py def calculate(num1, num2, mathOp): result = eval(str(num1) + mathOp + str(num2)) return result Interactive Window >>> import MyFunctions >>> MyFunctions.calculate (3, 4, “+”) >>> 7

Using variables in functions Assignment statement –Variables at module level are available to calling script –Variables within a function are local to the function Script1.py import NewYork nyLat = NewYork.lat nyLong = NewYork.long nyCalc = NewYork.calcgrowth ( , 0.03) Print nyCalc NewYork.py lat = # Global long = # Global def calcgrowth(pop, rate): growth = pop * rate return growth Passing values to module function

Importing a custom module First time in Python –Finds the module –Compiles the module –Runs the module Subsequent times (same Python session) –Re-uses imported (compiled) module

Finding the module Where does Python look to find module? The search path hierarchy is: –Home directory of calling module –PYTHONPATH environment variable –Standard Python library directions –Contents of.pth (text) files

Adding a custom search path Modify sys.path Append location of your custom modules Last for duration of script Resets to default when PythonWin closes Example.py import sys sys.path.append(“C:\\MyModules”) Print sys.path

Reloading a module single Python session Module load only once Script1.py NewYork.py import NewYork Access NewYork.py lat = -73 long = 40 To re-import, call reload() Interactive WindowNewYork.py >>> reload(NewYork)Access modifiedlat = NewYork.py long =

Running the module All statements execute from top to bottom –Statements do not exist until Python reaches and runs code (line by line interpret) –Code inside functions do not run until called NewYork.py lat = long = def calcgrowth (pop, rate): growth = pop * rate return growth Follow indent rules