Wanna draw a pattern using Python?

Varchasa Aggarwal
Nerd For Tech
Published in
2 min readJun 10, 2021

--

Photo by Neven Krcmarek on Unsplash

Hello Coders,

Ever think of drawing a pattern or a shape using python?

Drawing a pattern based on its speed, colors, shape, angles can be quite interesting.

Let’s have a look at it.

  1. I’ll be using turtle library for this and recommend you to use the same. First, you need to install the turtle library.
pip install turtle

Now let’s have a look at the code.

For full code — view at my GitHub repository.

I’ll be taking —

  • “n” is the number of stars,
  • “i” is the iterator
  • “x” is an exterior angle
  • “angle” is the angle of rotation.

Steps of program

  • Importing the library and setting the speed of the pentagon.
from turtle import *
import random

speed(speed ='fastest')
  • Now choosing random integers to generate RGB values.
a = random.randint(0, 255)
b = random.randint(0, 255)
c = random.randint(0, 255)
  • Setting the outline and fill color.
pencolor(a, b, c)
fillcolor(a, b, c)
  • Begins filling the pentagon.
begin_fill()
  • loop for each pentagon.
for i in range(5):
forward(5* n-5 *i)
right(x)
  • Color filling complete.
end_fill()
  • Rotating for the next pentagon.
right(angle)
  • Now setting the parameters.
n = 30  
x = 72
angle = 18

For full code and for more patterns — view at my GitHub repository.

After running the full program, you will get this pattern as output.

Find my article useful, support me and give a clap 👏.

Thanks,

--

--