Turtle drawingBeginner 20 lines

    Impress Your GF: Draw a Flower with Python Turtle (Full Code)

    Want to send someone a flower made of code? This short turtle program draws a flower of 36 overlapping petals in red, pink, magenta and violet on a black background, puts a gold dot in the middle and writes "For you ❤" underneath.

    The whole flower comes from one small loop: each petal is two arcs drawn with circle(), and turning 10 degrees after each one spins 36 petals all the way around. Draw it right here in your browser, then change the colours, the number of petals and the message to make it yours.

    What you will learn

    • Drawing arcs with circle(radius, extent)
    • Repeating a shape in a loop and turning to make a pattern
    • Cycling through colours with a list and the % operator
    • dot() for a filled circle and write() for text
    • penup() and goto() to move without drawing

    How the code works

    1. 1

      Set up the turtle

      turtle.Turtle() creates the pen, turtle.bgcolor("black") makes the background black so the colours glow, speed(0) draws as fast as possible and width(2) makes the lines a little thicker.

    2. 2

      One petal is two arcs

      circle(150, 60) draws a 60-degree arc of a circle with radius 150. left(120) turns the turtle around, and a second circle(150, 60) curves back to the start: together the two arcs make a pointed petal.

    3. 3

      36 petals make the flower

      The loop runs 36 times. After each petal, left(130) turns 120 degrees to finish the petal plus 10 more, so the next petal points 10 degrees further round. 36 × 10 = 360, a full circle of petals.

    4. 4

      Changing colours

      colors[i % 4] picks the next colour from the list: i % 4 counts 0, 1, 2, 3, 0, 1, … so the four colours repeat around the flower.

    5. 5

      The centre and the message

      dot(30, "gold") draws a gold circle in the middle. penup() and goto(-80, -250) move below the flower without drawing, and write() adds "For you ❤" in size 30 Arial. turtle.done() keeps the window open.

    Full source code

    Copy it, download it, or press Run it to play and change it in your browser.

    main.py20 lines
    import turtle
    
    t = turtle.Turtle()
    turtle.bgcolor("black")
    t.speed(0)
    t.width(2)
    
    colors = ["red", "pink", "magenta", "violet"]
    for i in range(36):
        t.color(colors[i % 4])
        t.circle(150, 60)
        t.left(120)
        t.circle(150, 60)
        t.left(130)
    
    t.dot(30, "gold")
    t.penup()
    t.goto(-80, -250)
    t.write("For you ❤", font=("Arial", 30))
    turtle.done()

    Run it on your computer

    1. Install Python 3 if you do not have it yet.
    2. Save the code as main.py and run python main.py

    More Python projects

    ← All Python projects