Let’s Learn 3. Modules Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus E-mail: ad@fivedots.coe.psu.ac.th 3. Modules Using modules to give Python even more POWER Look at built-in modules: random, math, turtle, winsound Start turtle graphics. Look at installing easygui using pip Using easygui
1. What is a Module? A module is a program that adds extra features to Python e.g. for 3D graphics, for writing games There are lots of modules that come with Python called built-in or standard modules It is easy to add more called third-party modules
Python's Design A small … plus lots language … of modules turtle math winsound
2. Using the random Module
List Modules Already in Python
Help on any Modules using the word "random"
Module Documentation click on "modules"
Information on random Look under 'r' in the Python Module Index
3. The math Module import math or from math import * Function name Description abs(value) absolute value ceil(value) rounds up cos(value) cosine, in radians degrees(value) convert radians to degrees floor(value) rounds down log(value, base) logarithm in any base log10(value) logarithm, base 10 max(value1, value2, ...) larger of two (or more) values min(value1, value2, ...) smaller of two (or more) values radians(value) convert degrees to radians round(value) nearest whole number sin(value) sine, in radians sqrt(value) square root tan(value) tangent Constant Description e 2.7182818... pi 3.1415926... import math or from math import *
Information on math Look under 'm' in the Python Module Index
4. The turtle Module The turtle moves about in a drawing window with a pen for drawing lines. The turtle can be told to turn a number of degrees, move a distance, move to a new position, and change its pen's color and line width If the turtle’s pen is down, it draws a line; otherwise, it moves without drawing
The Turtle Drawing Window 90° The turtle is an icon Initial position: (0, 0) Initial direction: East (0°) Color: black Line width: 1 pixel Pen: down (ready to draw) x-axis (0,0) 180° 0° 270° y-axis
Create a Turtle Tell Python to add the Turtle command to its built-in commands >>> from turtle import Turtle >>> sleepy = Turtle()
Some turtle Commands home() # Return to (0, 0) and 0° (east) down() # Enable drawing up() # Disable drawing forward(distance) # Go distance in current direction goto(x, y) # Go to (x, y) left(degrees) # Add degrees to current direction setheading(degrees) # Make degrees the new direction width(width) # Set width of pen in pixels color(color) # Red, green, blue, etc.
Move a Distance Move 50 pixels in the current direction. >>> from turtle import Turtle >>> sleepy = Turtle() >>> sleepy.forward(50) Move 50 pixels in the current direction. There's also a backward() command.
Turning Left or Right A turtle can turn left or right by a number of degrees e.g. sleepy.left(45) This angle is offset from the turtle's current forward direction. left(45) forward left(45) forward
Tell the turtle to draw a square Square.py
Draw a House House.py
A Blue Equilateral Triangle Triangle.py
Why those Angle? Why does the square need 4 turns of 90*? Why does the triangle need 3 turns of 120*? Look for a pattern... 4 * 90 == 3 * 120 == 360 degrees How would you get the turtle to draw a five-sided shape (a pentagon)?
A Faster Turtle Square.py
Filling the Shape with Color Square.py
Drawing (Bits of) Circles Circles.py
Writing Text HelloWorld.py
goto(x,y) The usual turtle window has -200 ≤ x ≤ 200 and Also used in setpos(x,y)
Print Information about the Turtle >>> from turtle import Turtle >>> sleepy = Turtle() >>> sleepy.goto(0, 50) >>> print(sleepy.position(), sleepy.pencolor(), \ sleepy.heading(), sleepy.isdown()) (0.00,50.00) black 0.0 True
Information on turtle Look under 't' in the Python Module Index
6. Windows Sound: winsound
Have you seen Star Wars? Run the programs: Python Gangnam Style: March.py playSound.py Python Gangnam Style: Gangnam.py
7. Getting More Modules https://pypi.python.org/pypi I'll search for "GUI"
lots more off the bottom of this picture
the "easygui" looks good; click on it for lots more details
Info on easygui The easygui webpage: An easygui tutorial: http://easygui.sourceforge.net/ you can also download easygui from there then use pip on the downloaded zip file An easygui tutorial: http://www.ferg.org/easygui/tutorial.html
8. Installing a PyPI Module Use the pip program that comes as part of the Python download. Use it from the Command window.
Use pip to Search PyPI
Use pip to Install easygui
More pip commands pip help pip list gives help on using pip pip list lists the extra (thitd-party) modules that you have added to Python pip install <downloaded file> installs a module that you've downloaded yourself
9. Use easygui in Python
Use easygui with Less Typing Tell Python to add all the easygui commands to Python's built-in commands
Getting the "OK" Input
Dialog Box with Buttons IceButtons.py
(Ugly) Choice Box IceChoice.py
Text Input IceEnter.py
Response