Python Modules.

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

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.
Perkovic, Chapter 7 Functions revisited Python Namespaces
Intro to Python Welcome to the Wonderful world of GIS programing!
Geography 465 Modules and Functions Writing Custom Functions.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
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.
© 2010 IBM Corporation IBM Experience Modeler - Theme Editor Installing Python Image Library Presenter’s Name - Presenter’s Title DD Month Year.
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
Guide to Programming with Python Chapter Nine Working with/Creating Modules.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Python Modules An Introduction. Introduction A module is a file containing Python definitions and statements. The file name is the module name with the.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Linux Operations and Administration
Make a blank window This is a starter activity and should take 5 minutes [ slide 1 ] 1.Log in to your computer 2.Open IDLE 3.In script mode create a file.
By James Braunsberg. What are Modules? Modules are files containing Python definitions and statements (ex. name.py) A module’s definitions can be imported.
Overview Intro to functions What are functions? Why use functions? Defining functions Calling functions Documenting functions Top-down design Variable.
CS2021 Week 2 Off and Running with Python. Two ways to run Python The Python interpreter – You type one expression at a time – The interpreter evaluates.
You Need an Interpreter!. Closing the GAP Thus far, we’ve been struggling to speak to computers in “their” language, maybe its time we spoke to them in.
Introduction to Computing Using Python Namespaces – Local and Global  The Purpose of Functions  Global versus Local Namespaces  The Program Stack 
CSI 3125, Preliminaries, page 1 Compiling the Program.
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.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
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}
Python C API overview References:
Juancho Datu. What is a Module? File containing Python definitions and statements with the suffix ‘.py’ in the current directory For example file name:
Lesson 06: Functions Class Participation: Class Chat:
Matlab Programming for Engineers
Exam #1 You will have exactly 30 Mins to complete the exam.
Python’s Modules Noah Black.
G. Pullaiah College of Engineering and Technology
Customizing custom.
3 Introduction to Classes and Objects.
Topics Introduction Hardware and Software How Computers Store Data
Lecture 2 Python Basics.
Classes and objects You can define many objects of the same class:
Topics Procedural and Object-Oriented Programming Classes
Computer Programming Fundamentals
Matlab Training Session 4: Control, Flow and Functions
IST256 : Applications Programming for Information Systems
Intro to Python Programming – Part II
Python’s Modules by E. Esin GOKGOZ.
JavaScript: Functions
CHAPTER FOUR Functions.
Functions, Procedures, and Abstraction
Topics Introduction Hardware and Software How Computers Store Data
Lesson 06: Functions Class Chat: Attendance: Participation
More About Objects and Methods
Good Testing Practices
Rocky K. C. Chang 15 November 2018 (Based on Dierbach)
Geography 465 Managing Custom Python Script Tools
Python 19 Mr. Husch.
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
How Java Program Executes
Java IDE Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Lab 4: Introduction to Scripting
Input and Output Python3 Beginner #3.
Python 19 Mr. Husch.
EN Software Carpentry Python – A Crash Course Esoteric Sections Compiled Languages.
Functions, Procedures, and Abstraction
Review We've seen that a module is a file that can contain classes as well as its own variables. We've seen that you need to import it to access the code,
 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.
SPL – PS1 Introduction to C++.
Python Modules.
Using Modules.
Presentation transcript:

Python Modules

What are Python modules? Files with previously written code that can be imported to the Python interpreter. A module contains its own public and private variables to prevent interferences with user defined ones. Modules file have .py extension

An example of a module $ vim modex.py def cube(x): //Returns the value of x^3 return x*x*x,

Importing Modules >>> import modex >>> modex.cube(6) 216 >>> y = modex.cube(6) >>>y

Accessing global variables in a module Using dot notation. >>> a = anymodule.globalvar Note that a global variables will not affect another module; i.e. modify module1.gvar will not change the value of module2.gvar.

From/import statements Used to import specific functions from a module >>> from modex import cube >>> cube(3) >>>27

Executing modules as scripts Python modules can be executed in command line whith python modex.py <arguments>

Compiled Python files When importing modex.py, a modex.pyc file is generated. It contains the bytecode from the last time modex.py is imported. If there is no .pyc file exists, it is created For future imports, these .pyc files are used to speed up the loading time, not the execution time

The dir() function This function returns a list of modules and functions that have been imported >>> dir() [‘_ _name_ _’, ‘_ _package_ _’] >>>import modex >>>dir() [‘_ _name_ _’, ‘_ _package_ _’,’_ _modex’]

Standard Modules Python comes with a library of standard modules. These modules contains built-in operations that are not actually part of Python’s core Some of them are only imported based on the OS. For instance: winreg module is only provided on Windows systems. An important Std module is sys. it is used to change the search path for the module >>> sys.path.append(‘/home/pnp29/cs265_spring_2011

Packages They are organized collections of modules. Modules are stored in directories Each directory contains a __init__.py file which tells the Python interpreter that it is a package directory.

Example of a package math/ __init__.py triArea.py sqArea.py statistics/ meanVal.py

Example of using packages >>> import math.sqArea >>> math.sqArea.area(5) 25 >>> from math.sqArea import area >>> area(5)

Questions?

Sources http://docs.python.org/tutorial/modules.html