Chapter 15. Modules Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.

Slides:



Advertisements
Similar presentations
An Introduction to Programming By :- Vishal Hirani B.Tech II year (CSE)
Advertisements

Chapter Modules CSC1310 Fall Modules Modules Modules are the highest level program organization unit, usually correspond to source files and.
This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
Chapter 19, 20 Object Oriented Programming (OOP) Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
CIT 590 Intro to Programming Lecture 2. Agenda ints, floats, strings, booleans Conditionals Loops Functions Testing The concept of scope.
Python Programming Chapter 1: The way of the program Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
An Introduction to Python – Part IV Dr. Nancy Warter-Perez.
Operator. Assignation (=). The assignation operator serves to assign a value to a variable. a = 5; It is necessary to emphasize that the assignation operation.
Programming Introduction November 9 Unit 7. What is Programming? Besides being a huge industry? Programming is the process used to write computer programs.
Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
CS 0008 Day 2 1. Today Hardware and Software How computers store data How a program works Operators, types, input Print function Running the debugger.
Chapter2 Digital Components Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.
INTRODUCTION TO PYTHON PART 2 INPUT AND OUTPUT CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
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.
Structure of program You must start with a module import# You must then encapsulate any while loop in a main function at the start of the program Then.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Homework 4 Example Bernard Chen How do we generate Random number in Python Random is a very important method in math and statistics, how could we.
Chapter 9 IF Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Guide to Programming with Python Chapter Nine Working with/Creating Modules.
Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 2 Fundamentals of Programming Languages 4/5/09 Python Mini-Course: Day.
Introduction to Python Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012
Ch 1. Introduction Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
An Introduction to Textual Programming
1 Workshop 4 (B): Visual Basic Test Project Mahidol University June 13, 2008 Paul Evenson University of Delaware Bartol Research Institute.
Python 101 Dr. Bernard Chen University of Central Arkansas IT Academic.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
The Python interpreter CSE 140 University of Washington Michael Ernst.
Introduction to Python 2 Dr. Bernard Chen University of Central Arkansas PyArkansas 2011.
Introduction to Computational Linguistics Programming I.
Fixtures Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Python Modules An Introduction. Introduction A module is a file containing Python definitions and statements. The file name is the module name with the.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
Python – May 11 Briefing Course overview Introduction to the language Lab.
Intro to Python Adriane Huber Debbie Bartlett Python Lab #1Python Lab #1 1.
Advanced File Processing NOVEMBER 21 ST, Objectives Write text output to a file. Ensure that a file can be read before reading it in your program.
Ch. 10 For Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #001 (January 17, 2015)
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 4: Writing programs.
Final Review Dr. Bernard Chen University of Central Arkansas Spring 2012.
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
Python Functions : chapter 3
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
CPSC 871 John D. McGregor Module 8 Session 3 Assignment.
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.
Debugging and Printing George Mason University. Today’s topics Review of Chapter 3: Printing and Debugging Go over examples and questions debugging in.
XYZCS Designing Binary Adders with decoders C(X,Y,Z) =  m(3,5,6,7) S(X,Y,Z) = S m(1,2,4,7);
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 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)
CIT 590 Intro to Programming Lecture 2. Agenda ‘import’ Abstraction Functions Testing The concept of scope.
Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012
Chapter 5- Assembling , Linking, and Executing Programs
Introduction to Programming
Ch 1. A Python Q&A Session Bernard Chen 2007.
Linux.
Introduction to Programming
CS 1111 Introduction to Programming Fall 2018
Lesson 06: Functions Class Chat: Attendance: Participation
Python Lesson’S 1 & 2 Mr. Kalmes.
Input and Output Python3 Beginner #3.
The Python interpreter
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.
Presentation transcript:

Chapter 15. Modules Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012

Modules Nodules are the highest level program organization unit, which packages program codes and data for reuse Actually, each “file” is a module (Look into Lib in Python)

Why Use Modules? Modules provide an easy way to organize components into a system, by serving as packages of names Modules have at least three roles: 1. Code Reuse 2. Implementing shared services or data 3. Make everything “lives” in a Module

Module Creation To define a module, use your text editor to type Python code into a text file You may create some functions or variables in that file You can call modules anything, but module filenames should end in.py suffix

Modules Usage Clients can use the module file we just wrote by running “import” statement >>> import math >>> import random >>> import module1 # assume this is the file name we saved

Modules examples We will create two modules: module1 and module2 module2 will import data and functions from module1

module1.py print “First line of module1” def print_out(aa): print aa*3 x=1 y=2

module2.py print “First line of module2” import module1 module1.print_out(“Hello World!! ”) # Use module1’s function print module1.x, module1.y # Reference module1’s variable x=10 y=20 print x, y

module2 output The result of execute this program is: Hello World!! Hello World!! Hello World!!

module3.py You may import as many modules as you like, for example: import module1 import module2 print module1.x print module2.x The result of execute this program is: 1 10

Important Notice Import Happen Only ONCE If you try to import a module for the second time, Python will NOT execute it

Class Practice Create two modules, each module has two functions and two variables Create a third program which calls all functions and prints all variables in module1 and module2 you created