Presentation is loading. Please wait.

Presentation is loading. Please wait.

Let’s Learn 3. Modules Saenthong School, January – February 2016

Similar presentations


Presentation on theme: "Let’s Learn 3. Modules Saenthong School, January – February 2016"— Presentation transcript:

1 Let’s Learn 3. Modules Saenthong School, January – February 2016
Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus 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

2 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

3 Python's Design A small … plus lots language … of modules turtle math
winsound

4 2. Using the random Module

5 List Modules Already in Python

6 Help on any Modules using the word "random"

7 Module Documentation click on "modules"

8

9 Information on random Look under 'r' in the Python Module Index

10 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 pi import math or from math import *

11

12 Information on math Look under 'm' in the Python Module Index

13 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

14 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° 270° y-axis

15 Create a Turtle Tell Python to add the Turtle command to its
built-in commands >>> from turtle import Turtle >>> sleepy = Turtle()

16 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.

17 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.

18 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

19 Tell the turtle to draw a square
Square.py

20 Draw a House House.py

21 A Blue Equilateral Triangle
Triangle.py

22 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 * == degrees How would you get the turtle to draw a five-sided shape (a pentagon)?

23 A Faster Turtle Square.py

24

25 Filling the Shape with Color
Square.py

26

27 Drawing (Bits of) Circles
Circles.py

28 Writing Text HelloWorld.py

29 goto(x,y) The usual turtle window has -200 ≤ x ≤ 200 and
Also used in setpos(x,y)

30 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

31 Information on turtle Look under 't' in the Python Module Index

32 6. Windows Sound: winsound

33 Have you seen Star Wars? Run the programs: Python Gangnam Style:
March.py playSound.py Python Gangnam Style: Gangnam.py

34 7. Getting More Modules https://pypi.python.org/pypi
I'll search for "GUI"

35 lots more off the bottom of this picture

36 the "easygui" looks good; click on it for
lots more details

37 Info on easygui The easygui webpage: An easygui tutorial:
you can also download easygui from there then use pip on the downloaded zip file An easygui tutorial:

38 8. Installing a PyPI Module
Use the pip program that comes as part of the Python download. Use it from the Command window.

39 Use pip to Search PyPI

40 Use pip to Install easygui

41 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

42 9. Use easygui in Python

43 Use easygui with Less Typing
Tell Python to add all the easygui commands to Python's built-in commands

44 Getting the "OK" Input

45 Dialog Box with Buttons
IceButtons.py

46 (Ugly) Choice Box IceChoice.py

47 Text Input IceEnter.py

48 Response


Download ppt "Let’s Learn 3. Modules Saenthong School, January – February 2016"

Similar presentations


Ads by Google