Draw Indian Flag using Python

Search for a command to run...

Have done similar with HTML, CSS and some JS Checkout if someone interested https://codepen.io/imvpn22/pen/yLamLby
Thank you
Thank you so much
Thank you Anurag !
The series content will mostly cover Technical Python tutorials and my recent learnings and experience in Python.
Easily automate Whatsapp | Facebook | Instagram Chat-Messenger with Python using PyAutoGUI
All you need to know about Hacktoberfest 2021

Hello reader! Well, Hacktoberfest is something, you are gonna hear a lot about in the coming month. You can refer to my YouTube video Tutorial to see a working tutorial for better understanding and a step-by-step guide of the same. https://www.yo...

Hello reader! In this Blog post, I will share some useful code snippets and functions in Python. Give it a read. Let's get started! 1. Memory The method getsizeof can be used to retrieve the size of any object. Here's an example of the same. import...

Hello reader! Whenever a client sends a request to the server, the response always contains a status code. You might not see it always but it's returned at every client-server interaction. Well, even if you are not a programmer, you would still know...

Hello reader! Microsoft subsidiary GitHub launched Copilot to power pair programming with AI. In this blog post, I will share all you need to know about it. You can refer to my YouTube video Tutorial to see a working tutorial for better understandin...

Hello world! This Republic day I tried to do something creative and made an Indian Flag with Turtle using Python.
In this Blog article, we will learn how to Draw Indian Flag. We will see the implementation in Python.
Check out the Repository for Ultimate Resource in python. Drop a star if you find it useful! Got anything to add? Open a PR on the same!
You can refer to my YouTube video Tutorial to see a working tutorial for better Understanding and a step by step Guide of the same.
1. Turtle Introduction
2. Creating an Indian Flag with Turtle
Let's get started!
Turtle is a pre-installed Python library. It enables users to create pictures and shapes by providing them with a virtual canvas. The onscreen pen that you use for drawing is called as turtle.
The turtle has three attributes: a location, an orientation (or direction), and a pen.
The turtle can move in four directions:
If you wish to know more about it, you can refer to Turtle Documentation. Use this link to navigate to the documentation.
Now that you are familiar with with our agenda and have acquired basic knowledge of Turtle module, we can move forward to the coding section.
You can find all the code at my GitHub Repository. Drop a star if you find it useful.

In order to access the Python library, you need to import it into your Python environment, use the following command to import turtleit in your python script.
import turtle
Starting out, let's create an instance of turtle.
flag = turtle.Turtle()
Now let's define some properties,
3 using speed method, that means the flag will not just appear on the screen, the drawing will have some animation. bgcolor method, by default it is white. pensize method, it will be slightly bold.turtle.speed(3)
#turtle.bgcolor("black")
turtle.pensize(5)
Now let's define a function to define for movements, I am calling it as draw.
def draw(x, y):
flag.penup()
flag.goto(x, y)
flag.pendown()

So far so good. Now, let's draw the Ashok Chakra, we need to pick the right colour for it.

flag.color("#054187")
Now, let's draw the 24 strokes, so for this, I will run the loop 24 times
If we start from center, we need to cover 360 degrees, and we got 24 strokes, so that becomes 15 degree each. We start out from center, draw a stroke using forward , track back to center using backward and turn 15 degrees. we repeat the same process 24 times.
for i in range(24):
flag.forward(80)
flag.backward(80)
flag.left(15)
draw(0, -80)

Once done, we can now draw a circle at the edges of the strokes. So earlier, we moved forward and backward by 80, so we need to consider same length for the radius of circle i.e 80.
flag.circle( 80, 360)

Now, if you observe, our turtle head is pointing at the bottom of a stoke, so
Let's specify the color here as green
flag.color("green")
Next, let's make a green rectangle's outline.
flag.begin_fill()
flag.forward(350)
flag.backward(700)
flag.right(90)
flag.forward(200)
flag.left(90)
flag.forward(700)
flag.left(90)
flag.forward(200)
flag.left(90)
flag.end_fill()
To fill it entirely with green colour, we are using begin_fill and end_fill method here. let's have a look at what we have got so far!

To fill it entirely with green colour, we are using begin_fill and end_fill method here. Now, it's time to draw the last part, the second rectangle. So, right now, we have cursor at A and we need it at B.

To fill it entirely with green colour, we are using begin_fill and end_fill method here.
Now, it's time to draw the last part, the second rectangle.
orange. draw method for the same.flag.color("orange")
draw(-350, 80)

Let's follow same procedure and draw orange reactangle.
flag.begin_fill()
flag.right(180)
flag.forward(700)
flag.left(90)
flag.forward(200)
flag.left(90)
flag.forward(700)
flag.left(90)
flag.forward(200)
flag.end_fill()
It should look something like this.

With these steps, we have successfully Drawn an Indian Flag using Python. That's it!
Simple, isn't it? Hope this tutorial has helped. I would strongly recommend you to Check out the YouTube video of the same and don't forget to subscribe my Channel.
You can play around with the turtle library and even explore more features.
You can find all the code at my GitHub Repository. Drop a star if you find it useful.
Thank you for reading, I would love to connect with you at Twitter | LinkedIn.
Do share your valuable suggestions, I appreciate your honest feedback!
You should definitely check out my other Blogs:
See you in my next Blog article, Take care!!