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
Algorithms & Programming with Python Python Level – Lesson 5-6 Nguyễn Chí Thức gthuc.nguyen@gmail.com 0986636879 Modules 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 Using 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()) Some popular built-in modules in Python • • • • • • • math time sys turtle random pickle os Turtle 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 Coordinate system Coordinate system - heading Turtle modules – show turtle import turtle t= turtle.Turtle() t.shape("turtle") t.showturtle() t.getscreen().exitonclick() Turtle initial settings Attribute fillcolor heading pencolor fillcolor pensize position shape speed Initial value “black” degrees (due east) “black” “black” (pixel) (0, 0) “classic” (0 is the fastest; 10 is the slowest) Basic movements import turtle t = turtle.Turtle() t.shape('turtle') t.left(45) t.forward(64) print(t.position()) print(t.heading()) t.getscreen().exitonclick() 10 Working with colors • • • • Most common: RGB system color components of red, green, and blue are mixed to form a unique color value A color component can be any real number from through The value represents the maximum intensity of a given color component, whereas the value represents the total absence of that component In Python, an RGB value is represented as the tuple: (r, g, b) 20 Working with colors Color RGB “black” (0, 0, 0) “red” (1, 0, 0) “green” (0, 1, 0) “blue” (0, 0, 1) “yellow” (1, 1, 0) “gray” (0.5, 0.5, 0.5) “white” (1, 1, 1) 21 Working with colors (variant) Color RGB “black” (0, 0, 0) “red” (255, 0, 0) “green” (0, 255, 0) “blue” (0, 0, 255) “yellow” (255, 255, 0) “gray” (127, 127, 127) “white” (255, 255, 255) 22 Filled 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() 23 Filled shapes t.fillcolor("red") t.begin_fill() for i in range(1, 5): t.forward(100) t.left(90) t.end_fill() 24 Functions related to colors 25 Drawing 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 26 Draw text t.write(str, align="left", font=("Arial", 8, "normal")) Examples: t.write("hello") t.write("hello", font=("Arial", 20)) t.write("hello", font=("Arial", 20, "bold")) 27 References https://docs.python.org/3/library/turtle.html 28 Move and draw 29 Settings and state 30 Pen control 31 Turtle state 32 Screen methods - t.getscreen.* 33 Events and input – ( t.getscreen.*) 34 ... 1) 21 Working with colors (variant) Color RGB “black” (0, 0, 0) “red” (25 5, 0, 0) “green” (0, 25 5, 0) “blue” (0, 0, 25 5) “yellow” (25 5, 25 5, 0) “gray” ( 127 , 127 , 127 ) “white” (25 5, 25 5, 25 5) 22 ... polygon 26 Draw text t.write(str, align="left", font=("Arial", 8, "normal")) Examples: t.write("hello") t.write("hello", font=("Arial", 20 )) t.write("hello", font=("Arial", 20 , "bold")) 27 References... font=("Arial", 20 , "bold")) 27 References https://docs .python. org/3/library/turtle.html 28 Move and draw 29 Settings and state 30 Pen control 31 Turtle state 32 Screen methods - t.getscreen.* 33 Events and