In this Python tutorial, we will learn How to create Square in Python Turtle and we will also cover different examples related to Turtle square. And we will cover these topics.
- Python turtle square function
- Python turtle square loop
- Python turtle square spiral
- Python turtle square size
- Python turtle square root
- Python turtle square inside a square
- Python turtle square example
- Python turtle fill square with color
- Python turtle draw multiple squares
- Python turtle repeating square
Python turtle square function
In this section, we will learn about the Turtle Square function in Python turtle.
In this, we use a built-in module in python (turtle). It uses to draw on the screen using a turtle (pen). To move turtle here are some functions that we use to give shapes forward() and backward().
CODE:
In the following code, we imported the turtle module in python, and with help of the forward() and left() functions, we generated a shape of a square.
- ws.forward(100) is forwarding turtle by 5 units
- ws.left(90) turn turtle by 90 degree.
from turtle import *
import turtle
ws = turtle.Turtle()
ws.forward(100)
ws.left(90)
ws.forward(100)
ws.left(90)
ws.forward(100)
ws.left(90)
ws.forward(100)
ws.left(90)
turtle.exitonclick()
Output:
In the following output, we have made a shape of a square with help of forward() and left() functions.
Read: Python Turtle Commands
Python turtle square loop
In this section, we will learn about the Turtle Square loop in python turtle.
Loops are a fun way to simply learn and implement to see what it will draw.
Code:
In the following code, we have some fun using for loop in a program in which we design a shape of the square using the left() and forward() functions.
from turtle import *
ws = Screen()
ws.setup(width=550, height=400)
shape('square')
for s in range(4):
left(90)
forward(150)
done()
Output:
After running the above code, we have the following output which we generate with help of the left() and forward() functions using for loop.
Read: Python Turtle Colors
Python turtle square spiral
In this section, we will learn about the Turtle Square Spiral in python turtle.
In this, we used a built-in module in python (turtle). It uses to draw on the screen using a turtle (pen). To move turtle here are some functions that we use to give shapes forward() and backward() etc.
Code:
In the following code, we have used a turtle module and using forward() and right() functions in which we define a length as len =10 and angle as angl=90 to form a spiral shape.
import turtle as tur
len = 10
angl = 90
tur.showturtle()
tur.shape("turtle")
tur.forward(len+len)
tur.right(angl)
length = len + 10
tur.forward(length+length)
tur.right(angl)
length = length + 10
tur.forward(length+length)
tur.right(angl)
length = length + 10
tur.forward(length+length)
tur.right(angl)
length = length + 10
tur.forward(length+length)
tur.right(angl)
length = length + 10
tur.forward(length+length)
tur.right(angl)
length = length + 10
tur.forward(length+length)
tur.right(angl)
length = length + 10
tur.forward(length+length)
tur.right(angl)
length = length + 10
tur.forward(length+length)
tur.right(angl)
length = length + 10
tur.forward(length+length)
tur.right(angl)
length = length + 10
tur.forward(length+length)
tur.right(angl)
length = length + 10
tur.forward(length+length)
tur.right(angl)
length = length + 10
tur.forward(length+length)
tur.right(angl)
length = length + 10
tur.forward(length+length)
tur.right(angl)
length = length + 10
Output:
After running the above code, we can see how we can make a spiral shape by using forward() and right() functions.
Read: Python Turtle Speed
Python turtle square size
In this section, we will learn about the Turtle Square Size in python turtles.
In this, we use a built-in module in python (turtle). A square is similar to a regular quadrilateral both have equal sides and equal angles of 90 degrees. And the size of the square depends upon the sides of the square.
Code:
In the following code, we create a screen inside the that we set the size of the square giving the width and height.
- size.setup(width=850, height=650) is a function used to set the height and width of the square.
- shape(‘square’) is used for making the shape of a square.
from turtle import *
size = Screen()
size.setup(width=850, height=650)
shape('square')
for si in range(4):
left(90)
forward(150)
done()
Output:
After running the above code we get the following output in which we see a square is made within a given size as mentioned in code where we take the width =850 and height =650 which depends on using size.setup() function.
Read: Python Turtle Circle
Python turtle square root
In this section, we will learn about Turtle Square Root in Python turtle.
Before moving forward we should have a piece of knowledge about square root. The square root is that a number produces another number when it multiplies on its own.
Code:
In the following code, we import the turtle library and also import the math module for using mathematical functions.
- print(math.sqrt(6)) is used for printing the square root of 6.
- print(math.sqrt(16)) is used for printing the square root of 16.
- print(math.sqrt(7.5)) is used for printing the square root of 7.5.
from turtle import *
import turtle
import math
print(math.sqrt(6))
print(math.sqrt(16))
print(math.sqrt(7.5))
Output:
After running the above code we get the following output in which we can see the square root of ‘6’, ’16’, and ‘7.5’.
Read: Python Turtle Art
Python turtle square inside a square
In this section, we will learn about squares inside a square in a python turtle.
As we know square has four equal sides and equal angles and each square has one more square inside it and it also has an equal side and equal angle.
Code:
In the following code, we import the turtle library import turtle import *, import turtle for drawing square inside a square.
tur.forward (length) function is used for moving the turtle in a forward direction with the given length.
from turtle import *
import turtle
tur=turtle.Turtle()
for length in [45,40,30,30,25,20,15,10,5]:
for x in range (4):
tur.forward (length)
tur.left (90)
Output:
After running the above code we get the following output in which we see the squares are drawn inside a square.
Read: Python Turtle Write Function
Python turtle square example
In this section, we will learn about the turtle square example in Python turtle.
In the Square example, we make a square shape with the help of a turtle we use the forward() and left() functions to make the perfect shape of the square.
Code:
In the following code, we import the turtle library for drawing the square with the help of the turtle.
- tur.forward(110) is used for moving the turtle in the forwarding direction by 110 units.
- tur.left(90) is used to turn the turtle in the left direction after moving forward.
from turtle import *
import turtle
tur = turtle.Turtle()
tur.forward(110)
tur.left(90)
tur.forward(110)
tur.left(90)
tur.forward(110)
tur.left(90)
tur.forward(110)
tur.left(90)
Output:
In the following output, we see the square is drawn on the screen with the help of a turtle.
Read: How to attach an image in Turtle Python
Python turtle fill square with color
In this section, we will learn about how to fill squares with color in Python turtle.
As we know we draw a square with the help of a turtle to look at the square we also fill color inside the square.
Code:
In the following code, we draw a square with the turtle and also filled color inside the square which gives the attractive look.
- tur.fillcolor(“cyan”) function is used to set the fill color.
- tur.begin_fill() function is used to start the filling color.
- tur.end_fill() function is used to ending the filling of the color.
from turtle import *
import turtle
tur = turtle.Turtle()
tur.fillcolor("cyan")
tur.begin_fill()
for _ in range(4):
tur.forward(200)
tur.right(90)
tur.end_fill()
turtle.exitonclick()
Output:
After running the above code we get the following output in which we see a square that is filled with “cyan” color.
Read: How to Create a Snake game in Python using Turtle
Python turtle draw multiple squares
In this section, we will learn about how to draw multiple squares in Python turtle.
As we can draw a square with the help of a turtle similarly we also draw multiple squares. Multiple squares create beautiful pictures on the screen.
Code:
In the following code, we create a screen inside the screen to draw multiple squares and these multiple squares give beautiful look to the screen.
from turtle import *
import turtle
scrn = turtle.Screen()
tur = turtle.Turtle()
def drawSquares(tur, siz, num, angl):
for i in range(num):
for x in range(4):
turtle.forward(siz)
turtle.left(90)
turtle.right(angl)
drawSquares(tur, 110, 5, 2)
Output:
After running the above code we get the following output in which we see multiple squares are drawn with the help of the turtle.
Read: Draw colored filled shapes using Python Turtle
Python turtle repeating square
In this section, we will learn about How to draw repeating squares in python turtle.
Repeating square is that as we draw a single square and we want to draw more squares then we apply the loop after applying the loop repetition of the square is shown on the screen.
Code:
In the following code, we import turtle library from turtle import *, import turtle for draw repeating square.
- turtle.right(180) is used to turn the turtle 180 degrees to the right.
- turtle.speed(0) function is used to give speed to the turtle and ‘0 ‘ is the fastest speed.
from turtle import *
import turtle
turtle.right(180)
turtle.speed(0)
leng = 500
for times in range (100):
for endtimes in range (4):
turtle.forward(leng)
turtle.right(90)
leng -= 6
Output:
After running the above code we get the following output in which we see the squares are drawn in repeating mode.
You check the following tutorials.
- Replit Python Turtle
- Python Turtle Size
- Python Turtle Triangle
- Python turtle onclick
- Python Turtle Race
- Python Turtle Screen Size
So, in this tutorial, we discussed Python Turtle Square and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.
- Python turtle square function
- Python turtle square loop
- Python turtle square spiral
- Python turtle square size
- Python turtle square root
- Python turtle square inside a square
- Python turtle square example
- Python turtle fill square with color
- Python turtle draw multiple squares
- Python turtle repeating square
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.