turtleintro

Runestone Interactive
20 Dec 201712:24

Summary

TLDRThis screencast introduces Python's Turtle Graphics, a feature that uses simple commands to draw shapes and patterns on the screen. It's based on a metaphor of a turtle moving with an inked tail, creating lines as it goes. The tutorial covers creating a graphics window, a turtle object, and using methods like 'forward' and 'left' to control the turtle. It emphasizes the connection between Python code and visual output, helping to develop a mental model of programming. The video also explains object-oriented concepts like methods and attributes, and demonstrates creating multiple turtles with different properties, each performing distinct drawing tasks.

Takeaways

  • 🐢 **Turtle Graphics Overview**: The script introduces Python's Turtle Graphics, a feature that allows users to draw shapes and patterns on the screen using simple commands.
  • 🛠️ **Basic Metaphor**: Turtle Graphics is based on the metaphor of a turtle that can move and draw on a surface, leaving a trail when its tail is dipped in ink.
  • 💻 **Python Integration**: The script emphasizes how Turtle Graphics can help beginners understand programming concepts by visually connecting Python code to on-screen actions.
  • 📝 **Simple Commands**: The script explains that commands like 'forward', 'left', and 'right' can be used to control the turtle's movements, creating various drawings.
  • 🔍 **Visual Mental Model**: It's important for learners to develop a mental model of programming by visualizing the flow of control through the turtle's actions on the screen.
  • 📑 **Code Walkthrough**: The script provides a line-by-line explanation of a simple Python program that uses Turtle Graphics to draw an 'L' shape.
  • 🎨 **Object-Oriented Concepts**: The script introduces object-oriented programming concepts by discussing how the 'screen' and 'turtle' are Python objects with methods and attributes.
  • 🖼️ **Customization**: The script demonstrates how to customize the turtle's attributes, such as color and pen size, to create different visual effects.
  • 🔄 **Multiple Turtles**: It's possible to have multiple turtles in a single program, each with its own set of instructions and attributes, allowing for complex drawings.
  • 📈 **Learning Aid**: The script concludes by highlighting the educational value of Turtle Graphics in learning programming, especially for understanding object-oriented principles.

Q & A

  • What is the main focus of the screencast?

    -The main focus of the screencast is to introduce the concept of turtle graphics in Python, which allows users to draw pictures and shapes on the computer screen using simple commands.

  • How is turtle graphics in Python described in the script?

    -Turtle graphics in Python is described as an interesting feature based on a simple metaphor where a turtle with an ink-dipped tail moves and leaves lines on the floor as it follows commands, creating drawings and patterns.

  • What is the purpose of using turtle graphics in learning Python according to the script?

    -Using turtle graphics helps in visualizing the Python program's actions, aiding in the development of a mental model of programming and the flow of control within a program.

  • What is the first step in creating a turtle graphics program as described in the script?

    -The first step in creating a turtle graphics program is to import the turtle module, which adds the necessary capabilities to Python for drawing.

  • How does the script explain the creation of a graphics window in turtle graphics?

    -The script explains that creating a graphics window involves assigning a screen object to a variable, which associates the variable with the canvas displayed on the web page.

  • What is the significance of the turtle object 'Alex' mentioned in the script?

    -The turtle object 'Alex' is significant as it represents the turtle that executes commands such as moving forward, turning, and drawing on the screen, demonstrating the basic functionality of turtle graphics.

  • What does the script imply about the relationship between the code written and the visual output in turtle graphics?

    -The script implies that there is a direct relationship between the code written in Python and the visual output seen on the screen, which is beneficial for understanding the connection between programming concepts and their visual results.

  • How does the script describe the attributes and methods of a turtle object?

    -The script describes attributes as characteristics like color, position, and pen width, while methods are actions the turtle can perform, such as moving forward or turning.

  • What is the role of the 'forward' method in the turtle graphics program discussed in the script?

    -The 'forward' method is used to command the turtle to move forward a specified number of units, which is essential for drawing lines and shapes on the screen.

  • How does the script demonstrate the concept of multiple turtles in a single program?

    -The script demonstrates the concept by creating two turtles, 'Tess' and 'Alex', each with their own set of attributes and methods, allowing them to draw different shapes independently.

  • What is the educational value of using turtle graphics with multiple turtles as highlighted in the script?

    -Using turtle graphics with multiple turtles helps in understanding object-oriented programming concepts such as instances and how each instance can have unique attributes and behaviors.

Outlines

00:00

🐢 Introduction to Turtle Graphics in Python

This paragraph introduces the concept of Turtle Graphics in Python, a feature that allows users to draw shapes and patterns on the computer screen using simple commands. The metaphor of a turtle with an ink-dipped tail that leaves a trail as it moves is used to explain how commands like 'forward', 'backward', 'left', and 'right' can control the turtle's movements. The paragraph emphasizes the educational value of Turtle Graphics for beginners to grasp programming concepts by visually connecting Python code with the turtle's actions on the screen. A simple six-line Python program is provided to demonstrate how the turtle can draw an 'L' shape, and the script explains each line of the code, highlighting the creation of a graphics window, the instantiation of a turtle named Alex, and the sequence of commands that result in the drawing.

05:01

📘 Understanding Python Objects and Attributes in Turtle Graphics

This paragraph delves into the attributes and methods of Python objects, using the turtle graphics module as an example. It explains that objects have methods, such as 'forward' and 'right', and attributes like color, position, and pen width. The paragraph illustrates how these attributes can be manipulated, such as changing the background color of the screen or the turtle's color. A demonstration is provided where two turtles, Tess and Alex, are created with different colors and pen sizes, and they draw different shapes. The paragraph concludes by encouraging the viewer to visualize the program's flow by tracking the position and state of each turtle in separate boxes, emphasizing the ability of Python to manage multiple instances with their own unique attributes.

10:03

🔄 Multiple Turtle Instances and Object-Oriented Programming

The final paragraph explores the concept of multiple instances in object-oriented programming using the turtle graphics module. It demonstrates how to create two separate turtles, Tess and Alex, each with their own set of instructions and attributes. The paragraph shows how Tess can be programmed to draw a triangle while Alex draws a square, highlighting that their positions, colors, and other attributes are kept separate in their respective objects. The ability to have multiple turtles drawing simultaneously is attributed to the distinct object instances for each turtle, allowing for parallel actions. The paragraph concludes by summarizing the key topics covered, including the use of modules, understanding objects with their methods and attributes, and the concept of multiple instances with unique names.

Mindmap

Keywords

💡Turtle Graphics

Turtle Graphics is a feature of Python that allows users to create drawings and patterns by controlling a cursor, metaphorically referred to as a 'turtle'. It's used to demonstrate basic programming concepts through visual output. In the video, the presenter uses Turtle Graphics to draw shapes on the screen by giving commands to the turtle, such as 'forward', 'back', 'left', and 'right', to illustrate how simple commands can create complex drawings.

💡Python

Python is a high-level programming language known for its readability and ease of use. The video uses Python to demonstrate the Turtle Graphics module, highlighting how Python's simple syntax can be used to control the turtle and create visual output. The script shows how importing the 'turtle' module and using its methods allows for interactive and visual programming.

💡Module

A module in Python is a file containing Python definitions and statements. The term is used to describe a block of code that adds extra capabilities to Python. In the context of the video, the 'turtle' module is imported to enable the use of Turtle Graphics. This allows the script to utilize the module's methods and functions to draw on the screen.

💡Object

In Python, an object is an instance of a class, which contains data in the form of fields (often known as attributes or properties) and code, in the form of procedures (often known as methods). The video explains that both the screen and the turtle are objects with methods like 'forward' and attributes like 'color' and 'position'. The script uses these objects to demonstrate how methods and attributes control the behavior and appearance of the turtle.

💡Method

A method is a function that is associated with an object and operates on that object's data. In the video, methods like 'forward()', 'left()', and 'right()' are used to control the turtle's movements. These methods are part of the turtle object and demonstrate how actions can be performed on an object in Python.

💡Attribute

An attribute is a property or characteristic of an object. In the context of the video, attributes of the turtle object such as 'color', 'position', and 'pen size' are manipulated to control the appearance and behavior of the turtle. The script shows how setting the background color ('BG color') and the turtle's color can change the visual output of the drawing.

💡Control Flow

Control flow refers to the order in which individual statements, instructions, or function calls of an imperative program are executed. The video uses the metaphor of the turtle to explain control flow in programming, showing how commands are executed in sequence to create a drawing. The script demonstrates this with the turtle moving forward and turning, which are actions that occur in a specific order.

💡Mental Model

A mental model is a concept or representation in a person's mind of how something works in the real world. The video emphasizes the importance of developing a mental model of programming by visually connecting the Python code with the turtle's actions on the screen. This helps in understanding the flow and logic of the program.

💡Instance

An instance in object-oriented programming is an individual object of a class. The video script uses the concept of instances to explain how multiple turtles can be created, each with its own set of attributes and methods. This is demonstrated when two turtles, Tess and Alex, are created and programmed to draw different shapes, showing that each instance can have unique properties and behaviors.

💡Canvas

In the context of the video, the canvas refers to the drawing area where the turtle performs its actions. The 'turtle' module's 'Screen()' method is used to create a window that serves as the canvas for the turtle to draw on. The video script uses the canvas to show how the turtle's movements are translated into visual drawings.

Highlights

Introduction to turtle graphics, a feature of Python that allows drawing pictures and patterns on the screen.

Turtle graphics is based on a metaphor of a turtle that can be controlled with simple English commands.

The turtle's tail dipped in ink leaves a line on the floor as it moves, creating drawings.

Turtle graphics is used to start learning Python and to visualize the flow of control in programming.

A six-line Python program is introduced to demonstrate drawing an 'L' shape using turtle graphics.

Explanation of the Python code that imports the turtle module and creates a graphics window and a turtle named Alex.

The turtle's forward method is used to move the turtle forward by a specified number of units.

The left method is used to turn the turtle by a specified number of degrees.

The importance of connecting what is seen in the Python program with the turtle's actions on the screen.

Python objects, such as the screen and the turtle, have methods and attributes that define their behavior and state.

Attributes of a turtle include its color, position, pen width, and heading direction.

Demonstration of setting the background color of the screen and the color of the turtle.

Explanation of how to create multiple turtles with different names and attributes in a single program.

Example of two turtles, Tess and Alex, performing different drawing tasks simultaneously.

The concept that each turtle instance has its own separate set of attributes and methods.

Encouragement for viewers to draw diagrams to understand how multiple turtle instances are managed in a program.

Summary of the key topics covered, including modules, objects, methods, attributes, and multiple instances.

Transcripts

play00:00

hi and welcome to this next screencast

play00:03

today we're going to talk about turtle

play00:05

graphics turtle graphics is an

play00:08

interesting feature of python that

play00:10

allows us to draw pictures and

play00:12

interesting shapes and patterns right on

play00:15

our computer screen turtle graphics is

play00:18

based on a very simple metaphor imagine

play00:21

that you have a turtle on the floor in

play00:23

front of you that you can control using

play00:26

English language and you can tell your

play00:29

turtle to do simple things such as turn

play00:31

to the right turn to the left go forward

play00:34

back up and so on now the interesting

play00:37

thing about these this turtle is that

play00:40

its tail has been dipped in ink and so

play00:44

if the tail is down and the turtle is

play00:47

moving the turtle leaves a line on the

play00:49

floor as it moves if you tell your

play00:52

turtle to pick up its tail then as the

play00:55

turtle moves around it doesn't leave a

play00:56

line it's a pretty simple metaphor but

play01:00

it's also pretty amazing some of the

play01:03

complex drawings that we can do with

play01:06

this simple set of commands and although

play01:08

turtles are fun the real purpose of this

play01:12

chapter is just to kind of started in

play01:15

using a little bit of Python now one of

play01:18

the nice things about using turtles is

play01:20

that it's easy to connect what you see

play01:25

in terms of the Python program visually

play01:28

with what you see the turtle doing so as

play01:31

you begin to develop a mental model of

play01:33

how programming works and how the flow

play01:37

of control of a program goes being able

play01:40

to connect the code that you write with

play01:43

what you see happening on the screen is

play01:45

a good way to cement that in your in

play01:48

your brain okay so let's start out with

play01:52

a few lines of Python code so we have

play01:56

that right here in this first example

play01:58

you can see it's a six line program and

play02:03

you might want to just look at that for

play02:05

a second and see if you can figure out

play02:09

what it's going to do by looking at the

play02:11

last three lines you might imagine that

play02:13

it's gonna draw something sort of shaped

play02:16

like an L all right so let's click on

play02:19

the Run button and just see exactly what

play02:21

happens and so there we see we did we

play02:25

did create this sort of l-shaped drawing

play02:28

on the screen

play02:29

let's just go line by line though

play02:33

through this little program so that we

play02:36

can get a better idea so this first line

play02:39

here where we say import turtle in this

play02:44

line we are importing the turtle module

play02:46

all right so the module is the is the

play02:49

fancy name for one of these libraries or

play02:55

modules of code that adds extra

play02:58

capabilities to Python in line two

play03:02

you'll see we have an assignment

play03:05

statement where we're assigning

play03:06

something turtle dot screen to the

play03:09

variable w n so turtle dot screen

play03:14

creates a graphics window in this web of

play03:18

fied version of python turtle that

play03:21

screen really just associates the

play03:24

variable with the canvas that we see

play03:26

already on the already on the web page

play03:29

the next line turtle turtle simply

play03:36

creates simply creates a turtle and

play03:39

assigns that turtle to the name Alex all

play03:43

right so we've now got a screen we've

play03:45

got a turtle and then in lines four five

play03:48

and six that's where the interesting

play03:50

things happen so in line four we invoke

play03:56

the method called forward and we pass it

play03:59

the parameter 150 what this really means

play04:03

is that we've just told our turtle to go

play04:05

forward by a hundred and fifty units in

play04:07

the next line we invoke the method named

play04:11

left passing it a parameter of 90 and

play04:14

this tells our turtle Alex to turn left

play04:17

by 90 degrees finally on line six we

play04:23

invoke the forward method again but this

play04:25

time we only tell

play04:27

to go forward by 70 so again if we run

play04:31

this you'll see that series of steps

play04:34

happening first Alex goes forward to the

play04:37

left hundred and fifty and then turns

play04:39

left and then goes half that distance 75

play04:42

to the top okay so a couple of very

play04:49

important things that we've kind of

play04:51

glossed over in that first example so

play04:55

we've created a screen and we've created

play04:57

a turtle now both of those are Python

play05:00

objects and Python objects have two very

play05:04

important aspects to them they have

play05:06

methods which as I've said are things

play05:09

like forward and right and objects also

play05:14

have attributes and attributes are

play05:17

things like the color of the turtle the

play05:20

position of the turtle and the canvas

play05:22

the width of the pen that the turtle is

play05:25

using to draw or the width of the

play05:28

turtles tail I guess would be a wetter a

play05:30

better way to say that and the heading

play05:32

of the turtle so let's just sort of back

play05:37

up for a second and rethink those first

play05:40

couple of lines so when we create the

play05:46

screen we can think of that screen

play05:49

creation as being something like this so

play05:57

we have the name W N and we're going to

play06:02

assign an object over here in this case

play06:07

it's the screen object we're gonna

play06:10

assign this screen object to the

play06:18

variable W n so we might draw that

play06:21

interactively like this all right next

play06:24

thing we did was we created a turtle in

play06:27

the example you can see on the screen

play06:31

here we called that turtle Alex

play06:40

and we'll have the turtle Alex over here

play06:47

all right so you might think about these

play06:50

objects then as containing both the

play06:52

methods so somewhere in this turtle

play06:55

object over here is a method to go

play06:57

forward or back also we would have

play07:01

attributes for things like position

play07:07

right so at any given time there'd be

play07:10

some value when the turtle is first

play07:12

created its position is at 0 0 its color

play07:21

is black and typically when a turtle is

play07:29

first created its width is 2 units 2

play07:35

pixels on your screen wide all right

play07:38

in this example down here we'll see that

play07:44

when we create a window we can set an

play07:47

attribute on that window called its BG

play07:51

color so here we might set BG color and

play07:59

in this case we're gonna change that

play08:01

background color to light green

play08:06

okay in this case we hop back over here

play08:17

or setting the turtles color to blue

play08:20

notice that in this example we've given

play08:23

the turtle a new name Tess okay so let's

play08:29

just run this and see what happens okay

play08:35

so a little bit different we create the

play08:38

screen just as we did before and in this

play08:40

case we set the background color to

play08:42

light green we create a turtle in this

play08:45

case we call it tests we set that turtle

play08:48

color to blue we set the pen size to 3

play08:52

you can see now that the pen that the

play08:54

picture we draw has a little bit thicker

play08:56

line and in this case we have the turtle

play08:59

go forward 50 goes left by 120 and then

play09:03

goes forward by 50 again so we're kind

play09:05

of drawing a part of a triangle in this

play09:08

example okay the final thing that I want

play09:15

to highlight here in this little section

play09:17

is that just like in a program that we

play09:23

saw in in the previous chapter where we

play09:25

had multiple numbers with different

play09:27

names in in in a turtle program we can

play09:33

have multiple turtles and each turtle

play09:35

can have their own can have their own

play09:37

name so if we look at this program we

play09:41

are creating a screen again setting the

play09:43

background to light green then we're

play09:46

creating a turtle called tests so we

play09:48

might draw that over here to show that

play09:50

we have two separate turtles we might

play09:52

actually draw the turtle or the name

play09:55

tests and we'll have another turtle

play09:59

object over here and tests refers to

play10:03

that turtle object all right but in this

play10:07

case you can see that we're setting

play10:09

Tess's color attribute to hot pink

play10:19

and then we create another turtle called

play10:21

Alex and in this case we're not changing

play10:25

anything about Alex's color we're gonna

play10:27

leave

play10:27

Alex's color at black alright now we can

play10:33

give the two turtles a different set of

play10:36

instructions we're gonna have Alex draw

play10:38

a square and we're gonna have tests draw

play10:41

a triangle so if we run this you can see

play10:48

that's exactly what happens in fact but

play10:52

now you can the reason that that can

play10:54

happen that the two different turtles

play10:56

can draw two different shapes and it

play10:58

almost looks like they're doing things

play11:00

simultaneously here is because the

play11:05

position and color and width and heading

play11:10

and so on are all different our spa are

play11:14

kept in the two separate objects one for

play11:18

Alex and one for tests so what you might

play11:21

want to do is is sort of draw this

play11:24

diagram out for yourself and then as the

play11:27

as you kind of trace through the program

play11:31

following the instructions line by line

play11:33

you might think about what is the

play11:36

position of tests and write that in

play11:38

Tessa's box what is the position of Alex

play11:41

and write that in Alex's box so you can

play11:45

see how the program is able to keep

play11:47

those two things separate from one

play11:51

another okay so we've covered some

play11:56

pretty important topics in this little

play12:00

screencast we've covered the idea of a

play12:02

simple module we've talked a little bit

play12:05

about objects and how objects have names

play12:08

as well as methods and attributes and

play12:12

finally we've talked about having

play12:14

multiple instances of something each

play12:19

instance with its own name so thanks for

play12:23

listening

Rate This

5.0 / 5 (0 votes)

Related Tags
PythonTurtle GraphicsCodingEducationProgrammingVisual LearningObject-OrientedCode ModuleDrawingInteractive Learning