Draw Indian Flag Using Python - I created my country's flag using python.

I Created my Country’s Flag using Python.

Introduction:

Creating India’s flag using Python is a fantastic way to combine your love for coding with your patriotic spirit. This project is beginner-friendly and leverages Python’s turtle module, which is great for drawing shapes and patterns. In this tutorial, you’ll learn how to draw the Indian flag step by step. Let’s get started!

Technical Overview:

Following are the pseudo code and steps that we implement in the python program to draw the flag of India :-

  1. First we need to import the turtle module.
  2. then we initialize the Turtle window and Turtle object.
  3. creating a function to draw a colored rectangle.
  4. Drawing the saffron (orange) rectangle using the function.
  5. Drawing the white rectangle using the function.
  6. Drawing the green band using the function.
  7. After drawing all rectangle, we will came to the center and draw a blue color circle for Ashoka Chakra.
  8. And now we will draw the 24 spokes of the Ashoka Chakra inside that circle using a for-loop.
  9. Now we will hide the turtle and display the flag.

Requirements:

Before we begin, ensure you have Python installed on your computer. Additionally, a code editor like Visual Studio Code or PyCharm will make coding and testing the more convenient.

We’ll use the turtle module for drawing (Turtle’s Documentation). This module is part of Python’s standard library which is pre-installed in python, so no extra installation is required.

  • Python3
  • Turtle-0.0.2 : If required then Install this python’s module using the command, pip install turtle==0.0.2

Python Code:

# https://amanaadi.com   --> Draw India's flag using python.

import turtle


# Initialize the Turtle window and Turtle object
screen = turtle.Screen()
screen.title("Indian Flag")
screen.setup(width=600, height=400)  # Set window size
screen.bgcolor("white")  # Set the background color to white

flag = turtle.Turtle()  # Create a Turtle object
flag.speed(6)  # Set Turtle speed to moderate


# Function to draw a colored rectangle
def draw_rectangle(color, width, height):
    flag.fillcolor(color)
    flag.begin_fill()  # Start filling with the specified color
    for _ in range(2):
        flag.forward(width)  # Draw the width
        flag.right(90)
        flag.forward(height)  # Draw the height
        flag.right(90)
    flag.end_fill()  # Stop filling


# Draw the saffron (orange) band
flag.penup()
flag.goto(-150, 100)  # Position the Turtle
flag.pendown()
draw_rectangle("orange", 300, 67)  # Draw a rectangle (300 width, 67 height)


# Draw the white band
flag.penup()
flag.goto(-150, 33)  # Move to the next band
flag.pendown()
draw_rectangle("white", 300, 67)


# Draw the green band
flag.penup()
flag.goto(-150, -34)  # Move to the last band
flag.pendown()
draw_rectangle("green", 300, 67)


# Draw the Ashoka Chakra (blue circle with 24 spokes)
flag.penup()
flag.goto(0, -33)  # Move to the center of the flag
flag.pendown()
flag.pensize(2)
flag.pencolor("navyblue")  # Set pen color to navy blue
flag.fillcolor("white")  # Set fill color to navy blue
flag.begin_fill()
flag.circle(33)  # Draw a circle with a radius of 33
flag.end_fill()


# Draw the 24 spokes of the Ashoka Chakra
flag.penup()
flag.goto(0, 0)  # Move to the top of the circle
flag.setheading(-90)  # Set the direction downwards
flag.pendown()

for _ in range(24):
    flag.forward(33)  # Draw a spoke
    flag.backward(33)  # Move back to the center
    flag.right(15)  # Rotate by 15 degrees


# Hide the Turtle and display the flag
flag.hideturtle()
screen.mainloop()  # Keeps the Turtle window open

Output:

Save and run the python code, now you will see how python is drawing the flag of India.

Conclusion:

Congratulations! You’ve successfully drawn the flag of India using Python. This project is not only a great way to practice coding but also a wonderful expression of creativity and national pride. Keep experimenting with the turtle module to create more complex designs and enhance your coding skills.

Happy coding!

Discover more from Aman Aadi

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top