Unformatted text preview:

Introduction to Computers Programming Program Files in Python Modules and Scripts Adam Meyers New York University Intro to Computers Programming Program Files in Python Modules and Scripts V22 0002 001 Outline Two types of py files Programs scripts Library Files modules Loading programs To run Into Idle for editing Loading and using modules Writing a python script Writing a python module Intro to Computers Programming Program Files in Python Modules and Scripts V22 0002 001 What is a Script A script is a file that contains a single program Functions defined in other files modules can be used if loaded using the keyword import Some example scripts are found in the python folder Extras These can be run in several ways Double clicking if the python launcher is the default program for files of type py By using open with and choosing the python launcher Typing python filename py on a command line in a shell These can also be loaded edited and run in IDLE The script Apple PYTHON DIRECTORY Extras Demo turtle tdemo colormixer Windows PYTHON DIRECTORY Demo turtle tdemo colormixer We can run it Or we can see the variables and functions loaded in it Intro to Computers Programming Program Files in Python Modules and Scripts V22 0002 001 The Example tdemo colormixer When we run it There are 3 sliders corresponding to red green and blue primary colors for light magenta yellow and cyan are primary colors for pigment Moving the sliders show the result of mixing these colors differently When we look at the code This program imports parts of the turtle module It creates some of its own object types we will not discuss this in detail here The real action is in the main function Intro to Computers Programming Program Files in Python Modules and Scripts V22 0002 001 The Example tdemo colormixer 2 By convention many programmers name their principle function that calls all others main The program first defines some variables and creates objects of two types screen and colorturtle the programmer s modified version of a type called turtle And writes the message Drag to label the window The function shift part of the definition of colorturtle maps the y position of a turtle to a numerical value The function setbgcolor sets the red green and blue components for the background color of the screen at the end of each call to the function shift These component values are based on the y positions of the red green and blue turtles Intro to Computers Programming Program Files in Python Modules and Scripts V22 0002 001 What is the turtle module A file called turtle py import turtle loads this in python and gives the path for the turtle py file help turtle lists the various functions variables and objects that are part of the turtle module History Turtle Graphics was originally implemented as part of the LOGO language To this day there are implementations for teaching young children about programming e g Microworlds The turtle module is a python implementation of this environment Intro to Computers Programming Program Files in Python Modules and Scripts V22 0002 001 The Basic Idea behind Turtle Graphics Do graphics by creating turtles A turtle is an object on a Cartesian Plane The turtle can look like a turtle but need not A Cartesian Plane is a grid as in High School Geometry Vertical lines are represented as X 1 X 0 X 1 etc Horizontal lines are represented as Y 1 Y 0 Y 1 etc Points are X Y pairs where X indicates how far to the left or right and Y indicates how far up or down e g 1 1 is located diagonally up from the middle 0 0 Turtles have pens which write when the pen is down but don t when the pen is up The ink color of the pen can be changed by setting their R G B values Intro to Computers Programming Program Files in Python Modules and Scripts V22 0002 001 Basic Components of Turtle Graphics in Python and elsewhere Object types Turtle and Screen In effect this adds to our list of data types integer string float Turtle Screen These are initialized using functions with no arguments turtle Turtle and turtle Screen turtle prefix for commands from the turtle module Simple commands that are connected to the Turtle object using dot notation fd NUM moves forward NUM units i e moves forward from the turtles point of view left DEG and right DEG pivot left right DEG degrees pd and pu put pen down to draw and up to stop Intro to Computers Programming Program Files in Python Modules and Scripts V22 0002 001 A Simple Turtle Graphics Example Loading module creating a screen and a turtle import turtle my screen turtle Screen my turtle turtle Turtle Putting the pen down and drawing a square my turtle pd my turtle fd 100 my turtle left 90 my turtle fd 100 my turtle left 90 my turtle fd 100 my turtle left 90 my turtle fd 100 Intro to Computers Programming Program Files in Python Modules and Scripts V22 0002 001 nd st Drawing a 2 Square Under the 1 One my turtle pu my turtle fd 100 my turtle pd my turtle fd 100 my turtle left 90 my turtle fd 100 my turtle left 90 my turtle fd 100 my turtle left 90 my turtle fd 100 Intro to Computers Programming Program Files in Python Modules and Scripts V22 0002 001 Modules aka Library Files Modules are files of functions and variables Designed to be incorporated in other programs Typically on a single theme math graphics astronomy Some modules are built in i e installed with Python You can download or write others yourself To load a module import module name You can use functions global variables and objects Use dot notation e g module name function from module import functionX or objectX Use functionX without dot notation You may overwrite function definitions if they have the same name from module import same as above except import everything Intro to Computers Programming Program Files in Python Modules and Scripts V22 0002 001 Modules Example the math module import math help math math ceil 5 1 help math ceil The help function Lists variables functions methods etc for a module Also gives function definitions Use dot notation for module variables functions Alternatively from module name import Let s you drop the dot notation Can cause problems name conflicts Intro to Computers Programming Program Files in Python Modules and Scripts V22 0002 001 four squares py Script Uses 2 modules turtle and time Encapsulates square drawing as a single function which we call 4 times The square drawing function puts down the pen moves forward and turns left three times each and then puts down the pen The


View Full Document

NYU CSCI-UA 0002 - Modules and Scripts

Download Modules and Scripts
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Modules and Scripts and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Modules and Scripts 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?