How Can We Help?
Turtle is an in-built library of python. Turtle is a modern python library for creating animations and graphics. The squares are significant figures, and often, the programmer.s needs to deal with squared figures.
This article will explain how to draw multiple squares in Python turtle.
- Prerequisite
- Multiple Squares Along The Same Horizontal Line
- How To Create Multiple Squares Side By Side
- How To Create Squares In Turtles On Top Of Each Other
- How To Create Multiple Squares On Top Of Each Other
- How to make multiple squares with common corners
- How To plot Multiple Nested Squares
- How To Create Grid Layout Using turtle
- Conclusion
Prerequisite
Before we proceed to code, we strongly recommend that you know the basics of the python turtle library. Additionally, we would also suggest you check if you have the following libraries installed in your system:
turtle, numpy
If you have not installed them yet, then follow the following commands:
If you are using the Windows operating system, then open the PowerShell and run the following commands:
pip install turtle numpy
If you are in Linux or macOS, then open the bash terminal and run the following commands:
pip install turtle numpy
Multiple Squares Along The Same Horizontal Line
We can use several techniques in the python turtle to create similar animations because we can apply different logic for the same output. Similarly, we can use multiple methods to create multiple squares along the same horizontal line.
The first method which we can adopt can be explained as follows:
- For creating the first square, define an iterative logic. The logic should be that the pointer moves forward, takes either a left or right turn by 90 degrees, and makes a forward motion again.
- Make sure that all the moves are the same length so they can form a square.
- Next, we can rotate the pointer by 180 degrees.
- Next, we can define the logic for the second square. Since we have already rotated the square by 180 degrees this time, we need to move the pointer in the right direction in each iteration.
Example (1)
# Import the turtle library in the code
import turtle
# Define the screen
s = turtle.Screen()
# Create a turtle object
t = turtle.Turtle()
# Create a user-defined function, namely drawSquares
def drawSquares(size):
# Iteeration for the first square
for _ in range(4):
turtle.forward(size)
turtle.left(90)
# Taking the opposite direction after completion of the first square
turtle.right(180)
# Iteration logic for the second square
for _ in range(4):
turtle.forward(size)
turtle.right(90)
turtle.done()
# Defining the main function
def main():
# Enter the length of the sides of the square
side = int(input("Please enter the sides of the squares: "))
# Calling the drawSquares function
drawSquares(side)
# Calling the main function
if __name__ == '__main__':
main()
Output:
Explanation:
- First, we imported the turtle library in our code using the python import statement.
- Next, we have created two global objects, namely s and t. We used the screen and the turtle functions to make the objects.
- After that, we created a user-defined function, namely drawSquares. This function takes only one argument, namely size, which is the length of the square we will draw.
- Under the function, we used iteration to make all square sides. Under each iteration, we have used two functions – forward and left. The forward function draws a line in the forward direction, and the left function changes the direction to the left of the current direction of the pointer. After one such iteration, we changed the direction of the pointer toward the right and created another iteration for the second square. We used a similar method to create the squares.
- After the drawSquares function, we created the main function. The main function is the driving code of our program. Under this function, we have taken the input from the user. The input is the length of the square we need to draw. Next, we called the drawSquares function to draw the figure.
We can also adopt slightly different techniques to achieve the same result
Example (2)
# Import the turtle library in the code
import turtle
# Define the screen
s = turtle.Screen()
# Create a turtle object
t = turtle.Turtle()
# Create a user-defined function, namely drawSquares
def drawSquares(size):
# Iteeration for the first square
for _ in range(4):
turtle.forward(size)
turtle.left(90)
# Taking the opposite direction after completion of the first square
turtle.backward(size)
# Iteration logic for the second square
for _ in range(4):
turtle.forward(size)
turtle.left(90)
turtle.done()
# Defining the main function
def main():
# Enter the length of the sides of the square
side = int(input("Please enter the sides of the squares: "))
# Calling the drawSquares function
drawSquares(side)
# Calling the main function
if __name__ == '__main__':
main()
Output:
How To Create Multiple Squares Side By Side
Creating multiple squares is tricky. Still, we can achieve it in multiple ways. Here we are giving one logic to draw multiple squares side by side, but you should also think of a few other ways to achieve the same.
Example (3)
# Import the turtle library in the code
import turtle
# Define the screen
s = turtle.Screen()
# Create a turtle object
t = turtle.Turtle()
# Create a user-defined function, namely drawSquares
def drawSquares(size,n):
# Iterating logic for the squares
for j in range(n):
updated_size=size+j*size
for i in range(1,5):
if(i%2!=0):
turtle.forward(updated_size)
turtle.left(90)
else:
turtle.forward(size)
turtle.left(90)
turtle.done()
# Defining the main function
def main():
# Enter the length of the sides of the square
side = int(input("Please enter the sides of the squares: "))
n=int(input("Enter the number of squares: "))
# Calling the drawSquares function
drawSquares(side,n)
# Calling the main function
if __name__ == '__main__':
main()
Output:
For the above output, we have used the length of the side to be 100 and the number of squares to be 5.
Explanation:
- First, we imported the turtle library using the import statement in our code.
- Next, we created two objects, namely s and t, using the screen and the Turtle functions. We declared them as global variables.
- Next, we created a user-defined function, namely drawSquares. The function takes two arguments, namely size and n. Under this function, we first iterated for n steps. Under each step, we created a variable named updated_size, which stores the updated size of the squares. Further, we have another iteration under this iteration. If the iterating step is odd, we have used the updated size of the square; else, we have used the parent size of the square.
- After the drawSquares function, we created the main function. The main function is the driving function of our code. Under this function, we used the input function to get user input for the length of the sides of the square and the number of squares. We called the drawSquares function to draw the figure.
- At last, we used the following lines of code to call the main function: if __name__ == ‘main‘: main()
How To Create Squares In Turtles On Top Of Each Other
Again we can adopt several methods to draw squares on each other. We can adopt the following method as one example:
First, create a logic to create the first square using any step, preferably iterating step.
Next, use the turtle module’s correct function to change the pointer’s direction to the right. Next, use another iteration to create the second square. During each second iteration step, move forward by the desired length and change the direction to the right by 90 degrees.
Example (4)
# Import the turtle library in the code
import turtle
# Define the screen
s = turtle.Screen()
# Create a turtle object
t = turtle.Turtle()
# Create a user-defined function, namely drawSquares
def drawSquares(size):
# Iteeration for the first square
for _ in range(4):
turtle.forward(size)
turtle.left(90)
# Taking the opposite direction after completion of the first square
turtle.right(90)
# Iteration logic for the second square
for _ in range(4):
turtle.forward(size)
turtle.left(90)
turtle.done()
# Defining the main function
def main():
# Enter the length of the sides of the square
side = int(input("Please enter the sides of the squares: "))
# Calling the drawSquares function
drawSquares(side)
# Calling the main function
if __name__ == '__main__':
main()
Output:
How To Create Multiple Squares On Top Of Each Other
We can create multiple squares on top of each other with the help of the similar logic we implemented to create squares side by side. Only we need to tweak a few places. Under each iteration, during each odd step, we need to move forward by the side of the square, and under the even step, we need to move forward by the length of the current length of the rectangle formed incremented by the side of the square.
Example (5)
# Import the turtle library in the code
import turtle
# Define the screen
s = turtle.Screen()
# Create a turtle object
t = turtle.Turtle()
# Create a user-defined function, namely drawSquares
def drawSquares(size,n):
# Iterating logic for the squares
for j in range(n):
updated_size=size+j*size
for i in range(1,5):
if(i%2!=0):
turtle.forward(size)
turtle.left(90)
else:
turtle.forward(updated_size)
turtle.left(90)
turtle.done()
# Defining the main function
def main():
# Enter the length of the sides of the square
side = int(input("Please enter the sides of the squares: "))
n=int(input("Enter the number of squares: "))
# Calling the drawSquares function
drawSquares(side,n)
# Calling the main function
if __name__ == '__main__':
main()
Output:
How to make multiple squares with common corners
Creating multiple squares with a common corner is an easy task. We can adopt the following method to achieve the same:
Make the first square using the iteration or any other method. Next, use the right or the left function to change the direction and again use the iteration method to create another square.
Example (6)
# Import the turtle library in the code
import turtle
# Define the screen
s = turtle.Screen()
# Create a turtle object
t = turtle.Turtle()
# Create a user-defined function, namely drawSquares
def drawSquares(size):
# Iteeration for the first square
for _ in range(4):
turtle.forward(size)
turtle.left(90)
# Taking the opposite direction after completion of the first square
turtle.right(90)
# Iteration logic for the second square
for _ in range(4):
turtle.forward(size)
turtle.right(90)
turtle.done()
# Defining the main function
def main():
# Enter the length of the sides of the square
side = int(input("Please enter the sides of the squares: "))
# Calling the drawSquares function
drawSquares(side)
# Calling the main function
if __name__ == '__main__':
main()
Output:
How To plot Multiple Nested Squares
We can create multiple nested squares, i.e., one square inside another. We can adopt the following method to implement the same:
- Iterate for n number of times. Here “n” represents the number of squares we must draw in the figure.
- Next, under each iteration, make a variable say updated_size, which will keep increasing the length of the sides of the square in each step.
Example (7)
# Import the turtle library in the code
import turtle
# Define the screen
s = turtle.Screen()
# Create a turtle object
t = turtle.Turtle()
# Create a user-defined function, namely drawSquares
def drawSquares(size,n):
# Iterating logic for the squares
for j in range(n):
updated_size=size+j*size
for _ in range(1,5):
turtle.forward(updated_size)
turtle.left(90)
turtle.done()
# Defining the main function
def main():
# Enter the length of the sides of the square
side = int(input("Please enter the sides of the squares: "))
n=int(input("Enter the number of squares: "))
# Calling the drawSquares function
drawSquares(side,n)
# Calling the main function
if __name__ == '__main__':
main()
Output:
How To Create Grid Layout Using turtle
We can use simple iterations to make a grid layout using the python turtle library. The code method will be as follows:
- First, make an iteration of four steps.
- Next, under this iteration, make another iteration. Under this child, iteration uses two functions, namely forward and left. The forward function will move the pointer in the forward direction, and the left function will change the direction of the pointer. After one iteration of the parent, iteration uses the correct function to move the cursor in the right direction.
Conclusion
In this article, we have understood how to plot multiple squares using the turtle library of python. We understood the logic which we could employ to get the desired result.
We strongly recommend that the readers try the concepts independently to understand the topic more. We recommend that readers look for the turtle’s official documentation to explore more functions and the associated libraries.