0 Up votes0 Down votes

2 views24 pagesPYTHON3

Mar 27, 2018

© © All Rights Reserved

PDF, TXT or read online from Scribd

PYTHON3

© All Rights Reserved

2 views

PYTHON3

© All Rights Reserved

- Define Structure of Data Screens for Each Screen Sequence
- Computer Programming (for)
- Software
- st
- The Tao of Programming
- iec103master-interoperability
- ptj
- Course List
- Geeta Unit-10valve-13th Oct Wdtl
- TRIOZ RegSys
- Display Order
- PL
- lab 7
- Sys
- Validation
- Lowongan Kerja +Application Letter
- COESD1290_DefineFormulasForcopycontrol
- Exit
- Custom 1
- ListAgg Function

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

- Define Structure of Data Screens for Each Screen SequenceUploaded byaalex_aalex
- Computer Programming (for)Uploaded byBank
- SoftwareUploaded byabdyn
- stUploaded byNurfitria Puspa Indah
- The Tao of ProgrammingUploaded byScott Foster
- iec103master-interoperabilityUploaded byLEViet
- ptjUploaded byArpita Bhattacharjee
- Course ListUploaded bySazzad Hossain
- Geeta Unit-10valve-13th Oct WdtlUploaded byjayanta37
- TRIOZ RegSysUploaded byMdhr Sagar
- Display OrderUploaded bybharath_sajja
- PLUploaded bydilipmatolkar
- lab 7Uploaded byMonica Baxter
- SysUploaded byDubravszky József
- ValidationUploaded byBijoy Biju
- Lowongan Kerja +Application LetterUploaded byDheWiie Whieavfy Whiee
- COESD1290_DefineFormulasForcopycontrolUploaded byAdauri Serra
- ExitUploaded byMalik Muhammad
- Custom 1Uploaded bynagarajuvcc123
- ListAgg FunctionUploaded byRavi Kumar Pasupula
- Annotated BibliographyUploaded bykunalshah81
- Ucpb New Pm FormUploaded byDhexter Villa
- fUploaded byToàn Minh Lê
- DOE INDUSUploaded byLarenze Dimaandal
- Manual de tecnologíasUploaded byJonathan Screenit
- WEEKLY AUGUST.xlsxUploaded byerkomal
- Assessment 4Uploaded byksmohana
- dfs sis 457.docxUploaded byAldo Lopez
- FhUploaded byrahulrg
- CURSOSUploaded byalan

- Quran - the Linguistic Miracle (LinguisticMiracle.com)Uploaded byspeed2kx
- 1. Introduction.pdfUploaded byYusuf Imran
- 02 Math Essentials fileUploaded byHemaNath
- 0511.pdfUploaded byYusuf Imran
- entries.pdfUploaded byDemid Odkhuu
- DocumentUploaded byYusuf Imran
- 2-3-59-544 (1)Uploaded byYusuf Imran
- Abdul GaffarUploaded byYusuf Imran
- Linear AlgebraUploaded byvishalwins
- TurtlesUploaded byYusuf Imran
- turtles(1)Uploaded byYusuf Imran
- Jquery TutorialUploaded byphani1978
- AICHandout2Uploaded byYusuf Imran

- LAT10T5Uploaded byoadipphone7031
- Torn ResultUploaded byYevgenia Troian
- Windowns ShortcutUploaded bynia yuniarani
- FDI in Cluj Napoca_projectUploaded byTeodora Chetan
- WTDSOFT-2014 PhD-StudyDesign-Ajhol AGTheory v0.15 ToPublishUploaded byforlevi
- GENSET PORTABLE pdf.pdfUploaded byazry_alqadry
- DevicesUploaded byjoe_lexs
- Hexarmor Rig Lizard 2021Uploaded byCovalciuc Bogdan
- 29th AbsencesUploaded bySebastian Carter
- Achyut Kanvinde Ppt HoaUploaded byrajesh350
- Sample ATP BTS 3900 AxisUploaded byMaulana Arif Rahman Hakim
- Application for senior faculty recruitment.docxUploaded byDrBollapu Sudarshan
- Paper Deagregasi EarthquakeUploaded bytwiggylicious
- R_S_ALR_87013181 Material Ledger Data Over Several PeriodsUploaded bypedrochapin
- Revue Technique Bmw Serie 3 325i E30Uploaded bymaccaron
- co2-system-Cylinder-Valve-Assembly (1).pdfUploaded byFahmi Bazher
- 04-PFD and P&IDUploaded byhelerahu
- CW2Uploaded byDebbie Lim
- ZKTimeEnterprise User ManualUploaded byfokom talom gaetan
- brushing teeth lessonUploaded byapi-248976520
- Indigenous Knowledge as a Socio Cultural Context of Science to Promote Transformative Education for Sustainable Development a Case Study on the Baduy Community Indonesia(1)Uploaded byLaila Fitriah Before Reborn
- CCNA 3 Exploration Chapter-1Uploaded byanwarul islam
- Program Director Project Manager in Nashville TN Resume Dean JohnsonUploaded byDeanJohnson
- HDMISpecification14a.pdfUploaded byCristian Brito Rivas
- Vat Taxpayer GuideUploaded bya_chrj
- Evolution of Regional Planning in ColombiaUploaded byJulian Ren Renfo
- GuidingStar Just One Thing Microlearning a Practitioners GuideUploaded byJStone
- Writing Part 4 - Online Task 1 (Responses)Uploaded byMaria Victoria Mermoz Paci
- 1-s2.0-S0043135408000821-main.pdfUploaded byAditya Rahmat
- Water DistributionUploaded byChristian Consigna

## Much more than documents.

Discover everything Scribd has to offer, including books and audiobooks from major publishers.

Cancel anytime.