Python to Draw a Sine wave using Turtle with Full Source Code For Beginners

  • This script draws a sine wave using the built-in Python library Turtle.
  • The image below demonstrates the equation of a sine wave. 

Run the Script:

  1. Open a terminal
  2. Navigate to the “Sine_Wave” directory containing this python file using the command prompt.

Execute:

python sine_wave.pyCode language: CSS (css)

Source Code:

sine_wave.py

from turtle import *
from math import *


A = 50      # Amplitude
B = 100     # WaveLength
C = 0       # Horizontal Shift
D = 0       # Vertical Shift

penup()
# As x increases y increases and decreases as it is evaluated.
for x in range(-200, 200):
    # Sine Wave Equation
    y = A * sin((2 * pi / B) * (x + C)) + D
    goto(x, y)
    pendown()

hideturtle()
mainloop()Code language: PHP (php)

Output:

Leave a Comment