1. Trang chủ
  2. » Công Nghệ Thông Tin

Algorithms Programming with Python Python Level 2

34 2 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Modules in Python
Chuyên ngành Algorithms & Programming with Python Python Level 2
Thể loại Lesson
Định dạng
Số trang 34
Dung lượng 1,03 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 1

Algorithms & Programming with Python

Python Level 2 – Lesson 5-6

Nguyễn Chí Thức

gthuc.nguyen@gmail.com

0986636879

Trang 2

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

Trang 3

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())

Trang 4

Some popular built-in modules in Python

Trang 5

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

Trang 6

Coordinate system

Trang 7

Coordinate system - heading

Trang 8

Turtle modules – show turtle

Trang 9

Turtle initial settings

Attribute Initial value

Trang 11

Movement 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 12

Draw a square of length 100

Trang 13

Draw a square of length 100

for i in range(1, 5):

t.forward(100) t.left(90)

t.getscreen().exitonclick()

Trang 15

Draw a square of length 100

• Using only goto()?

Trang 16

Drawing an equilateral triangle

Trang 17

Turtle – built-in turtle shapes

Trang 18

Turtle – speed

Trang 19

Undoing, 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 20

Working 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 21

Working with colors

Trang 22

Working with colors (variant)

Trang 23

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()

Trang 25

Functions related to colors

Trang 26

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

Trang 27

t.write ("hello", font=("Arial", 20) )

t.write ("hello", font=("Arial", 20, " bold " ) )

Trang 28

https://docs.python.org/3/library/turtle.html

Trang 29

Move and draw

Trang 30

Settings and state

Trang 31

Pen control

Trang 32

Turtle state

Trang 33

Screen methods - t.getscreen.*

Trang 34

Events and input – ( t.getscreen.*)

Ngày đăng: 27/10/2022, 10:54