Draw Indian Flag using Python

by anupmaurya
0 comment
Draw Indian Flag using Python

This Independence day I tried to do something creative and made an Indian Flag with Turtle using Python. In this article, we will learn how to Draw “The Great Indian Flag” using Python Turtle Graphics.

What is Turtle?

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.

Here, we will be using many turtle functions like begin_fill(), end_fill() to fill color inside the Flag, penup(), pendown(), goto() etc to reaching the target.

Functions of turtle graphics:

  • forward(x): moves the pen in forward direction by x units.
  • backward(x): moves the pen in the backward direction by x units.
  • right(x): rotate the pen in the clockwise direction by an angle x.
  • left(x): rotate the pen in the anticlockwise direction by an angle x.
  • penup(): stop drawing of the turtle pen.
  • pendown(): start drawing of the turtle pen.
  • begin_fill(): starts filling the color inside the shape.
  • fillcolor(“color_name”): sets the color to be filled.
  • end_fill(): stops filling the color.

Let’s Understand , How we do it

  • In order to access the Python library, you need to import it into your Python environment, use the following command to import turtle it in your python script.
import turtle
  • Get a screen to draw on.
screen = turtle.Screen()
  • Define an instance for turtle(here “t”).
  • For making an Indian Flag let’s divide the process into 4 steps:
  1. The rectangle with orange color.
  2. Then the middle rectangle.
  3. Then the last Green Rectangle.
  4. Then Ashoka Chakra inside the middle rectangle.
  • Here dimensions of all three Rectangles are (800 units x 167 units), which makes up dimensions of the flag as (800 units x 501 units).
  • The turtle starts from coordinates (-400, 250).
  • Then from that position it makes the First rectangle of orange color.
  • Then from the ending point of the first rectangle, Turtle makes the Second rectangle of no color.
  • Then the Third green color rectangle is made. Now for Ashoka Chakra, we need to perform set of operations
  1. A Big Blue circle and a white circle just smaller than blue.
  2. Set of small blue circles on the inner lining of a blue and white circle.
  3. And finally spokes inside the two blue and white circles starting from the Centre towards the outer direction.
  • Finally, The pride of one’s Nation is ready. 

Source Code with comments: Indian Flag using Python Turtle Graphics

import turtle from turtle import* #screen for output screen = turtle.Screen() # Defining a turtle Instance t = turtle.Turtle() speed(0) # initially penup() t.penup() t.goto(-400, 250) t.pendown() # Orange Rectangle #white rectangle t.color("orange") t.begin_fill() t.forward(800) t.right(90) t.forward(167) t.right(90) t.forward(800) t.end_fill() t.left(90) t.forward(167) # Green Rectangle t.color("green") t.begin_fill() t.forward(167) t.left(90) t.forward(800) t.left(90) t.forward(167) t.end_fill() # Big Blue Circle t.penup() t.goto(70, 0) t.pendown() t.color("navy") t.begin_fill() t.circle(70) t.end_fill() # Big White Circle t.penup() t.goto(60, 0) t.pendown() t.color("white") t.begin_fill() t.circle(60) t.end_fill() # Mini Blue Circles t.penup() t.goto(-57, -8) t.pendown() t.color("navy") for i in range(24): t.begin_fill() t.circle(3) t.end_fill() t.penup() t.forward(15) t.right(15) t.pendown() # Small Blue Circle t.penup() t.goto(20, 0) t.pendown() t.begin_fill() t.circle(20) t.end_fill() # Spokes t.penup() t.goto(0, 0) t.pendown() t.pensize(2) for i in range(24): t.forward(60) t.backward(60) t.left(15) #to hold the #output window turtle.done()
Code language: Python (python)


Draw Indian Flag using Python

So, Here is our Draw Indian Flag using Python. Simple isn’t it?? This is how we have successfully done with the ‘ Draw Indian Flag using Python ’. I hope the turtle library is now more clear to you and don’t forget to try this code once!!

You can play around with the library, explore more features and even customize it further.

ThankYou Pythoner’s!!!

Check out more python projects

Indian Flag using Python Indian Flag using Python Indian Flag using Python

You may also like