
Lesson Plan
Loopy Hip-Hop Beat
Learn how to use Python loops to code a simple Hip-Hop beat
Level
IntermediateTime
45 minutesHere's a hip-hop groove that combines electronic clap sounds, ticking hi-hats, and scattered kick drums. To add these new sounds, we're going to make use of loops in Python code.
What's a Loop?
A loop in Python is a way of repeating some of our code over and over again.

- The word
for
means that we're starting a loop. - The next part is the name of a variable that Python will use to count with.
- We named our variable
i
, but we could have used other names as well. - The
in range(8)
means that we're going to loop for 8 times in a row. - The last part is the colon character. This tells Python that the next line(s) will get repeated as part of the loop.
- Everything inside the loop needs to be indented by four spaces.
Step 1: Create a new project
- Log into TunePad and create a new project.
- Name your project Hip Hop Beat
Step 2: Sprinkler Hi-Hats
- Add a new Drum cell to your project
- Set the instrument to Drums → Headlines Drums
- Add this code for the hi-hats.
All we're doing here is using a loop (line 1) to play 16 hi-hat sounds in a row (line 2). Notice that we indent line 2 by four spaces. That tells Python that line 2 is the code that gets repeated. Starting on line 6 we do almost the exact same thing except that we play 13 hi-hats at a faster rate.
Step 3: Claps
- Now we're going to add our clap sounds on the even numbered beats (2, 4, 6 ...)
- For simplicity, we're going to keep everything together in the same cell.
- We use the
moveTo(0)
function on line 9 to rewind back to the beginning so that everything plays at the same time. - If we didn't have the
moveTo
, then all of the hi-hats would play before the claps even start.
This code uses another loop to repeat a rest followed by a clap six times in a row.
STEP 4: Kicks
For the last part, we're going to sprinkle in some kick drum sounds. Feel free to experiment and customize this arrangement.