In this Python turtle tutorial, we will learn How to create a nested loop in Python turtle and we will also cover different examples related to turtle nested loop. And, we will cover these topics.
- Python turtle nested loop
- Python turtle nested loop square
- Python turtle nested loop practice
- Python turtle nested loop pattern
- Python turtle nested loop triangle
Python turtle nested loop
In this section, we will learn about how to create a nested loop in python turtle.
A nested loop is defined as a loop inside another loop that created different shapes and patterns and also represents the continuous deployment of our logic.
Code:
In the following code, we will import the turtle module import turtle from which we create a nested loop. The turtle() method is used to make objects.
- ws.speed(0) is used to give the speed to the turtle and 0 is the fastest speed.
- ws.color(“cyan”) is used to give the color to the turtle for drawing a beautiful shape.
- ws.left(144) is used to move the turtle in the left direction.
- ws.forward(200) is used to move the turtle in the forward direction after moving in the left.
import turtle
ws = turtle.Turtle()
ws.speed(0)
ws.color("cyan")
for j in range (1,100):
for i in range (1,6):
ws.left(144)
ws.forward(200)
ws.left(5)
turtle.done()
Output:
After running the above code, we get the following output in which we can see a beautiful shape is created with the help of a nested loop.
Also, check: Python Turtle Race
Python turtle nested loop square
In this section, we will learn about how to create a nested loop square in a python turtle.
As we know the nested loop is a loop inside another loop. Here we can create a square with help of a nested loop we can use one loop inside the one loop we can use another loop.
Code:
In the following code, we will import the turtle module from turtle import *, import turtle from which we created a nested loop square, and a beautiful pattern is drawn. The turtle() method is used to make objects.
- turtle.speed(0) is used to give the fastest speed to the turtle.
- turtle.fillcolor(“black”) is used to give the black color to the turtle for filling the shape.
- turtle.begin_fill() is used to start filling the shape.
- turtle.setpos(a,b) is used to set the position of the turtle.
- turtle.forward(length) is used to move the turtle in the forward direction.
- turtle.left(90) is used to move the turtle in the left direction.
- turtle.end_fill() is used to end filling the shape.
from turtle import *
import turtle
size = 8
length = 50
startx = -200
starty = -200
def nestedloop():
turtle.speed(0)
for row1 in range(size):
for col1 in range(size):
if (row1+col1)%2 == 0:
turtle.fillcolor("black")
else:
turtle.fillcolor("blue")
turtle.begin_fill()
a = startx + col1*length
b = starty + row1*length
turtle.penup()
turtle.setpos(a,b)
turtle.pendown()
for i in range(4):
turtle.forward(length)
turtle.left(90)
turtle.end_fill()
nestedloop()
turtle.done()
Output:
After running the above code, we get the following output in which we can see the building blocks are created with the help of a nested loop square which looks very pretty.
Read: Python Turtle Tracer
Python turtle nested loop practice
In this section, we will practice the nested loop and also learn about how to create a different and more beautiful pattern with the help of a nested loop.
Nested Loop represents to continuous deployment of our logic when repeating or any design is needed to be made.
Code:
In the following code, we will import the turtle module from turtle import *, import turtle. The turtle() method is used to make objects.
- turtle.Screen() is used to create a screen in which we can draw different shapes.
- while counter < 4 is used to repeat the lines 4 times.
- tur.forward(50) is used to move the turtle in the forward direction.
- tur.left(90) is used to move the turtle in the left direction.
- ws.exitonclick() is used after completing the program we want to exit the screen only when we click on the screen.
from turtle import *
import turtle
ws = turtle.Screen()
tur = turtle.Turtle()
counter = 0
while counter < 4:
tur.forward(50)
tur.left(90)
counter = counter + 1
ws.exitonclick()
Output:
After running the above code, we get the following output in which we can see a pattern. This pattern is created with the help of a counter it repeats four times, and a nested loop is generated.
Read: Python Turtle Triangle
Python turtle nested loop pattern
In this section, we will learn about how to create a nested loop pattern in python turtle.
Before moving forward we should have a piece of knowledge about patterns. The pattern is that which is formed with the help of geometrical shapes and draw beautiful designs.
Here we can create different patterns with the help of a nested loop. And we know the nested loop is a loop inside another loop.
Code:
In the following code, we will import the turtle module import turtle import *, import turtle. The turtle() method is used to make objects.
- myturtle.penup() is used to stop the turtle.
- myturtle.forward(stepsize) is used to move the turtle in the forward ditrection.
from turtle import *
import turtle
def drawtriangle(size, col=1):
for shift in range(0, col):
myturtle.goto(myturtle.xcor(), shift * stepsize)
myturtle.dot()
myturtle.forward(stepsize)
if col < size:
drawtriangle(size, col + 1)
myturtle = turtle.Turtle()
myturtle.penup()
stepsize = 5
for trianglesize in range(1, 5):
drawtriangle(trianglesize)
myturtle.forward(stepsize)
turtle.done()
Output:
After running the above code, we get the following output in which we can see the patterns are drawn and a beautiful design is created with the help of a nested loop.
Read: Python Turtle Size
Python turtle nested loop triangle
In this section, we will learn about how to create a nested loop triangle in a python turtle.
Before moving forward we have some piece of knowledge about triangles. A triangle has three vertices and three edges. It is a closed, two-dimensional shape. We can also draw a triangle with the help of a nested loop. A nested loop triangle is defined as a loop inside another loop.
Code:
In the following code, we will import the turtle module from turtle import Screen, Turtle from which we can create a nested loop triangle. The turtle() method is used to make objects.
- turtle.position() is used to set the position of the turtle.
- turtle.dot() is used to draw a circular dot.
- ws.exitonclick() is used after completing the program we want to exit the screen only when we click on the screen.
from turtle import Screen, Turtle
stepsize = 10
def draw_triangles(tur, dot, steps=1):
a, b = turtle.position()
for step in range(steps):
turtle.goto(a, b + step * stepsize)
turtle.dot()
for _ in range(steps - step - 1):
turtle.backward(stepsize)
turtle.dot()
if steps < dot:
turtle.goto(a + stepsize * (steps + 2), b)
draw_triangles(turtle, dot, steps + 1)
screen = Screen()
turtle = Turtle()
turtle.hideturtle()
turtle.penup()
draw_triangles(turtle, 6)
screen.exitonclick()
Output:
After running the above code, we get the following output in which we can see a nested loop triangle is shown on the screen.
You may also like to read the following tutorials on Python turtle.
- Python Turtle Screen Size
- Python Turtle Graphics
- Python Turtle Mouse
- Python Turtle Get Position
- Python Turtle Draw Line
- Fractal Python Turtle + Examples
- Python Clear Turtle with examples
- Python turtle input
So, in this tutorial, we discussed Python turtle nested loop and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.
- Python turtle nested loop
- Python turtle nested loop square
- Python turtle nested loop practice
- Python turtle nested loop pattern
- Python turtle nested loop triangle
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.