PowerPoint Presentation Algorithms Programming with Python Python Level 2 – Lesson 5 6 Nguyễn Chí Thức gthuc nguyePowerPoint Presentation Algorithms Programming with Python Python Level 2 – Lesson 5 6 Nguyễn Chí Thức gthuc nguye
Trang 1Algorithms & Programming with Python
Python Level 2 – Lesson 5-6
Nguyễn Chí Thức
gthuc.nguyen@gmail.com
0986636879
Trang 2Modules in python
• Modules are used to group functions, variables, and other
things together into larger, more powerful programs
• Some modules are built in to Python, and you can
download other modules separately
Trang 3Using modules
import {module-name}
Ex:
import time import math
• We can then call functions that are available in this
module, using the dot symbol
print(time.asctime())
Trang 4Some popular built-in modules in Python
Trang 5Turtle graphics – LOGO programming language in Python
• Allows you to simulate the movements of
a turtle robot by moving an image around on a computer monitor
• The turtle is equipped with a pen that can draw lines and
shapes as it moves about
Trang 6Coordinate system
Trang 7Coordinate system - heading
Trang 8Turtle modules – show turtle
Trang 9Turtle initial settings
Attribute Initial value
Trang 11Movement commands
Function What it does
backward(distance) Moves the turtle the given distance in the opposite of
its current direction forward(distance) Moves the turtle the given distance in its current
direction goto(x, y) Moves the turtle to position (x, y)
home() Moves the turtle to position (0, 0) and sets its
heading to 0 degrees left(degrees) Turns the turtle counterclockwise by the given
degrees right(degrees) Turns the turtle clockwise by the given degrees
setheading(degrees) Sets the turtle’s heading to degrees
penup()
pendown()
Trang 12Draw a square of length 100
Trang 13Draw a square of length 100
for i in range(1, 5):
t.forward(100) t.left(90)
t.getscreen().exitonclick()
Trang 15Draw a square of length 100
• Using only goto()?
Trang 16Drawing an equilateral triangle
Trang 17Turtle – built-in turtle shapes
Trang 18Turtle – speed
Trang 19Undoing, clearing and reseting
• clear() : erases all the drawings but leaves all the
turtle’s current settings alone (position, heading, color, and pen size)
• reset(): erases all the drawings and restores the
turtle’s initial settings
Trang 20Working with colors
• Most common: RGB system
• 3 color components of red, green, and blue are mixed to form a
unique color value
• A color component can be any real number from 0 through 1
The value 1 represents the maximum intensity of a given
color component, whereas the value 0 represents the total
absence of that component
• In Python, an RGB value is represented as the tuple:
(r, g, b)
Trang 21Working with colors
Trang 22Working with colors (variant)
Trang 23Filled shapes
• How to draw not just the outline of a square, but a square
filled with a given color ?
• With Turtle graphics:
• fillcolor(color)
• begin_fill()
• end_fill()
Trang 25Functions related to colors
Trang 26Drawing a circle
• The only one built-in function that draws a shape:
• circle(radius, extent = None, steps = None)
• extent: an angle - determines which part of the circle is drawn
(in degrees)
• steps: number of side of the regular polygon
Trang 27t.write ("hello", font=("Arial", 20) )
t.write ("hello", font=("Arial", 20, " bold " ) )
Trang 28https://docs.python.org/3/library/turtle.html
Trang 29Move and draw
Trang 30Settings and state
Trang 31Pen control
Trang 32Turtle state
Trang 33Screen methods - t.getscreen.*
Trang 34Events and input – ( t.getscreen.*)