How to Draw the Indian Flag Using Python Turtle

Python’s Turtle graphics is a great way to learn programming while creating visual projects. In this tutorial, weโ€™ll use Turtle to draw the Indian flag with its saffron, white, and green stripes, along with the Ashoka Chakra at the center. ๐Ÿ‡ฎ๐Ÿ‡ณโœจ


Prerequisites

Before getting started, ensure you have Python installed on your system. Turtle is a built-in module, so no additional installations are required.

Steps to Draw the Indian Flag

  1. Set up the Turtle environment โ€“ Create a drawing screen with a white background.
  2. Draw the three horizontal stripes โ€“ Saffron at the top, white in the middle, and green at the bottom.
  3. Draw the Ashoka Chakra โ€“ A navy blue wheel with 24 equally spaced spokes.
  4. Position the elements correctly โ€“ Use precise coordinates for accurate representation.

Python Code to Draw the Indian Flag

import turtle

def draw_rectangle(color, x, y, width, height):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()
    turtle.color(color)
    turtle.begin_fill()
    for _ in range(2):
        turtle.forward(width)
        turtle.right(90)
        turtle.forward(height)
        turtle.right(90)
    turtle.end_fill()

def draw_chakra():
    turtle.penup()
    turtle.goto(0, -20)
    turtle.pendown()
    turtle.color("navy")
    turtle.width(2)
    turtle.circle(30)
    
    # Draw 24 spokes
    for _ in range(24):
        turtle.penup()
        turtle.goto(0, 13)
        turtle.pendown()
        turtle.forward(30)
        turtle.backward(30)
        turtle.right(15)

def draw_indian_flag():
    screen = turtle.Screen()
    screen.bgcolor("white")
    screen.title("Indian Flag")
    turtle.speed(3)
    
    # Draw Saffron Rectangle
    draw_rectangle("orange", -180, 100, 360, 60)
    
    # Draw White Rectangle
    draw_rectangle("white", -180, 40, 360, 60)
    
    # Draw Green Rectangle
    draw_rectangle("green", -180, -20, 360, 60)
    
    # Draw Ashoka Chakra
    draw_chakra()
    
    # Hide Turtle
    turtle.hideturtle()
    turtle.done()

# Run the function
draw_indian_flag()

Understanding the Code

  • Drawing the Flag Stripes: We use a function to draw three colored rectangles representing saffron, white, and green.
  • Drawing the Ashoka Chakra: The draw_chakra() function uses a circle and loops to create 24 spokes.
  • Using Turtle Functions: penup(), pendown(), goto(), and circle() are used to position and draw elements.

Output

Running this script will generate an Indian flag with the correct proportions and the Ashoka Chakra at its center. ๐ŸŽจ


Conclusion

This Python Turtle project is an excellent way to practice graphics programming while celebrating Indiaโ€™s national pride. Modify and experiment with the colors, size, or add animations to make it even more engaging!

๐Ÿš€ Enjoyed this tutorial? Like, share, and subscribe for more exciting coding projects!


๐Ÿ”— Additional Resources

๐Ÿ“Œ Video Tutorial:

๐Ÿ“Œ Code Repository: GitHub Source Code

#Python #IndianFlag #TurtleGraphics #LearnPython #Programming #TechTutorial ๐Ÿš€๐Ÿ”ฅ

Leave a Reply

Your email address will not be published. Required fields are marked *