Module Template

From Linewaves
Jump to navigation Jump to search

This is a template for creating your own module. Please use this format for consistency across the site. Review existing modules for clarification. Delete notes to author (in italics) when complete.

Module Template
Music Topic(s): Key Signatures
Music Category: Fundamentals
Language/Libraries: Python
Programming Difficulty: Easy
Latest Revision By: Admin
Last Updated: 06/16/2021

Edit module information in infobox at right.

This module explores the relationship between keys and key signatures. One-sentence description of music theory topic of module.

In musical notation, different musical keys are represented by patterns of sharps and flats on the staff called key signatures. The relationship between keys and key signatures is illustrated through a diagram called the circle of fifths. Brief paragraph introducing the topic.

In the circle of fifths at right, major keys are given in red, minor keys are given in green, and corresponding key signatures (along with the number of sharps or flats) are given in black. Note that the number of sharps in each key signature increases as we travel clockwise from the top, and the number of flats increases in the reverse direction. Continue explanation, using visual aids as practical.

This module will focus on the following question: Identify the central questions of the module.

  1. How do we determine the key, given the key signature?

Let’s begin by focusing on the major keys on the right half of the circle of fifths. Let’s think of each of these key signatures as defined by the number of sharps it contains: C major contains 0, G major contains 1, D major contains 2, etc.

Generally, design module so that you use short blocks of code first and gradually build up to larger blocks. Use "computer code" for single lines (<code></code> tags in source or font dropdown menu in visual editor) and "preformatted" for multi-line blocks (prefix each line with a space or use <pre></pre> tags in source). Always separate input and output into separate blocks, and annotate for clarity, as below. (If your code requires special libraries or packages, include the installation instructions and import statements.)

We can create a list of key names (as strings), where the index number gives the number of sharps (we start with C since index numbers count from zero):

sharp_keys = ['C', 'G', 'D', 'A', 'E', 'B', 'F#', 'C#']

If we know the key signature, we can count the number of sharps and use that as the index number to determine the key:




We can do the same thing with flats, counting in the opposite direction on the circle:

flat_keys = ['C', 'F', 'Bb', 'Eb', 'Ab', 'Db', 'Gb', 'Cb']





While each of these methods works on its own, it probably makes more sense to integrate them into a single tool for finding keys. To bring them together, we need two pieces of information for each calculation: (1) whether the key signature has sharps or flats, and (2) how many there are. We can organize them as elements in a list:

my_key = ['flats', 3]

We’ll use a conditional if statement to separate flats from sharps (and the print function to test our block of code): Include in-text links to existing tutorials and other modules as practical.

if my_key[0] == 'flats':
elif my_key[0] == 'sharps':



Then we can modify the print function so that it actually displays the key with a statement to access the element in the appropriate list:

if my_key[0] == 'flats':
 elif my_key[0] == 'sharps':



Try this block of code with different key signatures:

my_key = ['sharps', 4]
my_key = ['flats', 6]
my_key = ['sharps', 1]

You can embed videos using the <embedvideo></embedvideo> tags (see the source code).

Finally, we can integrate all of this into a function to make things tidier:

def key_finder(s_or_f, num):
  flat_keys = ['C', 'F', 'Bb', 'Eb', 'Ab', 'Db', 'Gb', 'Cb']
  sharp_keys = ['C', 'G', 'D', 'A', 'E', 'B', 'F#', 'C#']
  if s_or_f == 'flats':
  elif s_or_f == 'sharps':

Call the function:

key_finder('sharps', 3)



This function allows us to identify the name of a major key, given the number of sharps or flats in the key signature. End with a concluding sentence or (brief) paragraph.

At the very bottom, include some questions for further investigation under the heading Extensions, and links to online sources under Further Reading.


  1. How can we integrate major and minor keys into a single key-finding function?
  2. Can you build a function to output relative keys?
  3. What are alternative structures for determining which list contains the given key?
  4. How might we use dictionaries to store and access key and key signature information?

Further Reading

For more on the music theory-related aspects of keys and key signatures, check out the Open Music Theory entry.

See source code for how to assign your module to various categories. Please try to use existing categories whenever possible.