2 views

Uploaded by Yusuf Imran

PYTHON3

- TriLED
- layer-3_Q&A.docx
- TOSHIBA SATELLITE C650 INWERTEC MANAUS MAS10M PREMP BUILD REV A02.pdf
- 89c51 raining cou > week2
- Code
- Cv Mazilu Catalin
- 130 kmh
- TicTacToe
- SM-G950F_Tshoo_7.pdf
- New Text Document
- The Ring programming language version 1.5 book - Part8 of 180
- UG WBS Fare CalculateMileage 08.2
- unit3 demo7.pdf
- Tp Contact
- Vaibhav Trivedi Resume-1
- Xi Std First Mid Term Syllabus for the Year 2018
- Tuxedo ATMI driver program
- Using Atrenta Spyglass in GUI Mode
- 2. 100 Debug Qbasic Programs
- vi-tips-1-toc

You are on page 1of 24

Today’s lecture

The Turtle graphics package

Brief history

Basic commands

Drawing shapes on screen

Logo and Turtle graphics

In 1967, Seymour Papert and Wally Feurzeig created an

interpretive programming language called Logo.

Papert added commands to Logo so that he could control a

turtle robot, which drew shaped on paper, from his computer

Turtle graphics is now part of Python

Using the Turtle involves instructing the turtle to move on the

screen and draw lines to create the desired shape

The Turtle package

Some functions are part of Python’s core libraries, in

other words they are ‘built-in’

print()

input()

float()

Other functions need to be imported into your

Python program

The turtle module needs to be imported at the

start of any Python program that uses it:

import turtle

Basic Turtle commands

There are four basic turtle commands

turtle.forward(x)

Moves turtle forward in direction it is facing by x steps

turtle.back(x)

Moves turtle backward from its facing direction by x

steps

turtle.left(x)

Turns the turtle x degrees counterclockwise

turtle.right(x)

Turns the turtle x degrees clockwise

Turtle example

Using the Python interpreter in IDLE to demonstrate how to

use Turtle graphics

First, import the turtle package

>>>

Turtle example

We are going to draw a right-angled triangle

90°

Important information:

The turtle appears as an icon

x-axis (0,0)

Initial position: (0, 0)

180° 0°

Initial direction: East (0°)

Colour: black

Line width: 1 pixel

y-axis

Pen: down (ready to draw) 270°

Algorithm

draw a line

Turn 90 degrees left (anti-clockwise)

draw a line

Turn 135 degrees left (anti-clockwise)

draw a line

Turtle example

Step 1: Draw a line

>>>

>>> turtle.forward(200)

>>>

1. Draw a line

90degree

Turtle example

Initial direction: 0

Note how the turtle is now facing upward after being turned

90 degrees left

>>>

>>> turtle.forward(200)

>>> turtle.left(90)

>>>

Turtle example

Step 3: draw a line

>>>

>>> turtle.forward(200)

>>> turtle.left(90)

>>> turtle.forward(200)

>>>

current direction

>>>

>>> turtle.forward(200)

>>> turtle.left(90)

>>> turtle.forward(200)

>>> turtle.left(135)

>>>

Turtle example

Working out the length of the longest side using the

Pythagoras’ formula

>>>

>>> turtle.forward(200)

>>> turtle.left(90)

>>> turtle.forward(200)

>>> turtle.left(135)

>>> c = ((200**2)+(200**2))**0.5 #around 283 steps

Turtle example

Step 6: draw a line

The finished image

>>>

>>> turtle.forward(200)

>>> turtle.left(90)

>>> turtle.forward(200)

>>> turtle.left(135)

>>> c = ((200**2)+(200**2))**0.5)

>>> turtle.forward(c)

Turtle example

We can use loops when drawing shapes using

Turtle graphics

Write a program that will draw a square using a

loop

Draw a Turn 90

line degree left X 4 times

Turtle example

We can use loops when drawing shapes using

Turtle graphics

Write a program that will draw a square using a

loop

import turtle

count = 0

while count < 4:

turtle.forward(200)

turtle.left(90)

count = count + 1

TRY IT OUT!

Exercise 1

Write a Python program that draws a rectangle. The long

sides must be 300 steps long and the short sides must be 150

steps long

Draw a Turn 90

Draw a Turn 90 short line degree left

long line degree left

Draw a Turn 90

Draw a Turn 90 short line degree left

long line degree left

Turtle example

Write a program that will draw a circle

Steps:

Draw a short line (2 pixels)

Turn 1 degree

Repeat the above steps 360 times

Turtle example

Write a program that will draw a circle

import turtle

count = 0

while(count < 360):

turtle.forward(2)

turtle.left(1)

count = count + 1

print("Finished!")

Question

Consider the following program:

import turtle

count = 0

length = 100

while count < 4:

turtle.forward(length)

turtle.left(90)

count = count + 1

length = length - 10

generated by the program above?

Go to:

pwlive.pw

Exercise 2

How to draw a star?

How many steps do you need?

What is the size/length for each step?

What is the turning angle for each step?

TRY IT OUT!

Exercise 3

Draw the shape that is produced by the following Python program:

import turtle

count = 0

while(count < 180):

turtle.forward(2)

turtle.right(1)

count = count + 1

turtle.right(45)

turtle.forward(300)

turtle.left(90)

turtle.back(150)

turtle.right(45)

turtle.back(250)

TRY IT OUT!

Exercise 4

Draw the shape that is produced by the following Python

program:

import turtle

big_line = 100

little_line = 50

angle = 90

turtle.left(angle)

turtle.forward(big_line)

count = 0

while count < 4:

turtle.right(angle//2)

if count != 3:

turtle.forward(little_line)

else:

turtle.forward(big_line)

count = count + 1

turtle.right(90)

turtle.forward(130)

Summary

The Turtle package must be imported into every Python

program that uses it

The Turtle has four basic commands; forward, back, left and

right

- TriLEDUploaded byTạ Hoàng Nguyên
- layer-3_Q&A.docxUploaded byRaj Ranjan
- TOSHIBA SATELLITE C650 INWERTEC MANAUS MAS10M PREMP BUILD REV A02.pdfUploaded byThomas Delacruz
- 89c51 raining cou > week2Uploaded byapi-3761333
- CodeUploaded byBilalHassan
- Cv Mazilu CatalinUploaded byカタリン マジル
- 130 kmhUploaded byHendry Nur Apriyanto
- TicTacToeUploaded byAbhishek Sharma
- SM-G950F_Tshoo_7.pdfUploaded byBrandon Cifuentes
- New Text DocumentUploaded bysonuverma1986
- The Ring programming language version 1.5 book - Part8 of 180Uploaded byMahmoud Samir Fayed
- UG WBS Fare CalculateMileage 08.2Uploaded byAndrei Gogu
- unit3 demo7.pdfUploaded byRamana Salapu
- Tp ContactUploaded bySalah-eddine Nour
- Vaibhav Trivedi Resume-1Uploaded byVishal Sharma
- Xi Std First Mid Term Syllabus for the Year 2018Uploaded bySenthil Kumaran Matric Hr. Sec. School Tiruchendur
- Tuxedo ATMI driver programUploaded byLuis Yañez
- Using Atrenta Spyglass in GUI ModeUploaded byvenu.lancer8205
- 2. 100 Debug Qbasic ProgramsUploaded byJoseph Israel
- vi-tips-1-tocUploaded bydevGuide.net ltd
- A195259101_20264_1_2019_CV ModernUploaded byKeshav Kumar Jha
- Picking TrolleyUploaded bySA1234567
- Wincatt ManualUploaded bySuperiorPerformance
- MS-16GC Disassembly GuideUploaded byAriyawira Binui
- E02 F50BF-1Uploaded bynmanikandan
- Tic Tac Toe CodeUploaded byErik Orellana
- Summ,er intrenship on sound recordingUploaded byAngshusmita Baruah Segra
- Process RR v3Uploaded bynicgildenhuys
- ArfanioTugas Praktikum 5.1Uploaded byDwi Bagus Ainiyah
- Chapter 1 Must ReadUploaded byNaveen Madiraju

- en_quran_the_linguistic_miracle_linguisticmiracle.pdfUploaded byAltaf Adil
- 9_omar faruq_2.pdfUploaded byYusuf Imran
- Borges La Busca de AverroesUploaded byElena Zam Ceb
- 02 Math Essentials fileUploaded byHemaNath
- 2-3-59-544 (1)Uploaded byYusuf Imran
- 1. Introduction.pdfUploaded byYusuf Imran
- 5 Miss Kausar Parveen0001Uploaded byYusuf Imran
- entries.pdfUploaded byDemid Odkhuu
- 0511.pdfUploaded byYusuf Imran
- DocumentUploaded byYusuf Imran
- Git-bnUploaded byFaysal
- Abdul GaffarUploaded byYusuf Imran
- turtles(1)Uploaded byYusuf Imran
- Jquery TutorialUploaded byphani1978
- AICHandout2Uploaded byYusuf Imran
- TurtlesUploaded byYusuf Imran
- 123Uploaded byYusuf Imran
- Linear AlgebraUploaded byvishalwins
- Bangla All Sections.phpUploaded byYusuf Imran

- MTH 252 LabUploaded bysnaxtothemax
- Straight LineUploaded byAditya Bansal
- Enforcement and ComplianceUploaded byMiniala
- HP-DSA Aplication Note 243-1Uploaded byAndres Felipe Loaiza Patiño
- Sem in ServqualUploaded byImrul Jobaid
- LAB 3 ENERGY LOSS IN PIPE AND FITTINGS_23Sept2016.docUploaded byfaezahjalal
- Oracle FunctionUploaded bySammy Jaryal
- RM ThesisUploaded bym_rashed
- Delta Ia-plc Ispsoft Um en 20160518Uploaded bybagopercy
- Class No OpacUploaded byrubenramm
- gcalUploaded byLucas
- D5045-14_Standard_Test_Methods_for_Plane-Strain_Fracture_Toughness_and_Strain_Energy_Release_Rate_of_Plastic_Materials.pdfUploaded byAhmed Abbas Ghubin
- Chemistry - ANSWERS - Bylikin, Horner, Murphy and Tarcy - Oxford 2014Uploaded byRobert Hsu
- Four Bar LinkageUploaded byMBHladilek
- “Geotechnical behaviour of an instrumented urban tunnel built under difficult soft soil conditions”,Uploaded byjesusmorj
- Inflow Performance Relationship 01Uploaded byChristian Karekonde Opuba
- COMP2613 Fall 2018 Assignment #1Uploaded byAnonymous iiMVHF9f
- 320089443Uploaded byLiviu Solcovenco
- Contrast StretchUploaded byMart Muiro
- MCAMX5 Quick Ref CardUploaded bytobich
- Report Format ProjectUploaded byAshwani Kumar
- Programming Methodology In cUploaded byRanjith M Kumar
- XRSIM Lessons_Student PacketUploaded byWilliam Pinto Hernández
- section_8.2_book_and_exercises.docxUploaded byDenise Valencia
- Algebra Question Paper 2Uploaded byAMIN BUHARI ABDUL KHADER
- Excell StatisticsUploaded byCART11
- Advanced Marketing ResearchUploaded bySina Israel
- Joseph Newman the Energy MachineUploaded bykishbud
- IR Matrix Spectroscopy of Pentachlorocyclopentadienyl Cation C5Cl5+. Effect of Chlorine as a SubstituentUploaded byAditya Budi Fauzi
- BinomialUploaded byVishal Kumar