Python Concepts Using Turtle

“Turtle” is a way to draw with Python. Turtle was introduced in 1967 via a language called “Logo”.
turtle
is mainly used to introduce coding to the kids. It’s a straightforward yet versatile way to understand the concepts of Python
Getting started with turtle:
Python Version: Ensure that you have version 3 of Python on your computer. If not, then you can download it from the Python website. We use application IDLE to program with turtle.
- First step is to import turtle Library
import turtle
2. Initialize a variable t which is used to refer the turtle
t = turtle.Turtle()
3. Moving the turtle: There are four functions tells the turtle to move
I Forward: Tells the turtle to move forward by the given distance.
II Backward :Tells the turtle to move backward by the given distance.
III Left: Tells the turtle to
IV Right
t.right(90)
t.forward(100)
t.left(90)
t.backward(100)
Drawing a square:
import turtle
t=turtle.Turtle()
t.forward(90)
t.right(90)
t.forward(90)
t.right(90)
t.forward(90)
t.right(90)
t.forward(90)
Output:

Now lets write the program to draw same square using loop
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
import turtle
t=turtle.Turtle()
for i in range(4):
t.forward(90)
t.right(90)
OR
import turtle
t=turtle.Turtle()
for i in [1,2,3,4]:
t.forward(90)
t.right(90)
OR
import turtle
t=turtle.Turtle()
a=[1,2,3,4]
for i in a:
t.forward(90)
t.right(90)
Output:

Now lets using the function pen color and pen size to change the color and size of pen respectively for the same square.
import turtle
t=turtle.Turtle()
a=["red","green","orange","blue"]
for i in a:
t.pencolor(i)
t.forward(90)
t.right(90)
Output:

Tuple: A tuple is a collection which is ordered and unchangeable. Since tuple is immutable
import turtle
t=turtle.Turtle()
t.pensize(5)
a=("red","green","orange","blue")
for i in a:
t.pencolor(i)
t.forward(90)
t.right(90)
Output:

Dictionary
Dictionaries are used to store data values in key:value pairs.
A dictionary is a collection which is ordered*, changeable and does not allow duplicates.
#syntax
a={1:"red",
2:"green",
3:"blue",
4:"orange"}
Example: Turtle with dictionary
import turtle
t=turtle.Turtle()
t.pensize(5)
a={1:"red",2:"green",3:"blue",4:"orange"}
for i in a:
t.pencolor(a[i])
t.forward(90)
t.right(90)
Output:
