80 likes | 211 Views
Creating and Using Modules. Sec 9-6 Web Design. Objectives. The student will: Know how to create and save a module in Python Know how to include your modules in IDLE. Modules.
E N D
Creating and Using Modules Sec 9-6 Web Design
Objectives The student will: • Know how to create and save a module in Python • Know how to include your modules in IDLE.
Modules • Once you start to create functions for Python you may want to store these functions and use them later in different program. • The files in which you stores functions is called a module • Modules are stored in files with an extension of .py (e.g. testrobots.py)
Creating a Module • Start Python. • From the File menu Select New Window • Type in the functions • It’s always a good idea to comment the code so you can remember what the module and the functions do • Save the file with a .py extension • Save the file in the same folder as your start IDLE program
Including Modules in IDLE Once you save your program you “include” it in your IDLE window: • fromtestrobotsimport * • Now all the functions are available for use… • Note that the lines not included in a function are executed immediately
Using Functions form a Module Once you have imported the functions you can simply call them:
Summary • Putting functions into modules helps keep things organized. • Include the functions in your program by: from module import * • Once imported all functions can be used.
Rest of Today • Complete the program from Monday. Show me the results when complete. • Everyone must do this!