Repetition in Songs: A Python Tutorial | by Okoh Anita | Towards Data Science

Everyone has heard a song or knows what a song sounds like. I can carelessly say everyone can define a song ...in their own words. Just for the benefit of the doubt, a song (according to Wikipedia) is a single work of music that is typically intended to be sung by the human voice with distinct and fixed pitches and patterns using sound and silence and a variety of forms that often include the repetition of sections.
In his journal article called “The complexity of Songs”, computer scientist Donald Knuth capitalized on the tendency of popular songs to devolve from long and content-rich ballads to highly repetitive texts. As some may waste no time agreeing with his notion, it does raise some questions like: Does repetitiveness really help songs become a hit? Is music really becoming more repetitive over time?
In an attempt to teach some basic python code in the form of a case study, I am going to test this hypothesis (Are popular songs really repetitive?)with one of my favorite songs. One way to test this hypothesis is to figure out the unique words and calculate the fraction of those words to the total number of words in a song.
In this tutorial, we’ll cover:
Variables and data types
Lists and Dictionaries
Basic Arithmetic operations
Built-in Functions and Loops
To get the most out of this tutorial, you can follow along by running the codes yourself.
The music we will be using is entitled ‘Perfect’ by Ed Sheeran. You can copy the lyrics here. However, the lyrics I am using in this analysis was cleaned out to get a conclusive result. For example, I changed words like “we’ll” to “we will” etc. You can get my version of the lyrics hereThe editor used was Jupiter NoteBook. Here is a quick tutorial on how to install and use it.
The music we will be using is entitled ‘Perfect’ by Ed Sheeran. You can copy the lyrics here. However, the lyrics I am using in this analysis was cleaned out to get a conclusive result. For example, I changed words like “we’ll” to “we will” etc. You can get my version of the lyrics here
The editor used was Jupiter NoteBook. Here is a quick tutorial on how to install and use it.
For the purpose of this case study, we will streamline our hypothesis by asking two major questions:
How many unique words were used compared to the whole lyrics of our case study song — Perfect by Ed Sheeran?
What are the most repetitive words used and how many times were they used throughout the song?
Let's get started analyzing already
1.A String is a list of characters. A character is anything you can type on the keyboard in one keystroke, like a letter, a number, or a backslash. However, Python recognizes strings as anything that is delimited by quotation marks either a double quote (“ “) or a single quote (‘ ‘) at the beginning and end of a character or text. For example: ‘Hello world’
For this case study, a string is our lyrics as seen below
2.Variables are typically descriptive names, words or symbols used to assign or store values. In other words, they are storage placeholders for any datatype. It is quite handy in order to refer to a value at any time. A variable is always assigned with an equal sign, followed by the value of thevariable. (A way to view your code output is to use a print function. As you may already know with Jupyter notebook, an output can be viewed without a print function)
To store the lyrics, we will assign it a variable named perfect_lyrics .
3.Lists can be created simply by putting different comma-separated values between square brackets. It can have any number of items and they may be of different types (integer, float, string etc.). It can even have another list as an item. For example:
list1 = [1,'mouse', 3.5, [2,'is',5.0]] #3.5 is a float
Now that we have gotten a sense of what a list looks like. Let go back to our data.
Since one of our aims is to figure out the number of unique words used, it means we will need to do a bit of counting i.e to count each word. In order to achieve these, we will not only have to put our string into a list but will have to separate each word using a .split()method. Therefore our dataset will look like this
Input
Output
From the above output, you will notice that each word has been separated into independent strings. And the whole lyrics make up the list called split_lyrics(which is also a variable)
We will also need to separate the unique words of the rest of the words and the count. To do this, we would need to use python functions.
A function is a block of organized, reusable code that is used to perform a single, related action. Python has many built-in functions like print( )-to show your output, list( )-to create a list, len( )- to count the number of characters in a list or string, etc. Python also allows you to create your own functions. But we will not be creating our own function in this case study.
We will be using a few functions in the case study but we will start with the set( ) function. To separate the unique words from the whole lyrics, we need a set( ) and a print ( ) function i.e
Input
Output
To count the number of words in the whole lyrics, we would need a len ( ) function
Input & Output
Doing the same for the unique words extracted
Our above analysis has helped answer our first question: How many unique words were used as compared to the whole lyrics of the song?Simply put, 129 unique words were used in over 290 words in total.
Our next goal is to figure out the second part of the question What are the most repetitive words used and how many times were they used?
In order to answer this question, we will need to learn more data structures.
4. Dictionaries in Python are unordered collections of items. While data structures have only values as an element, A dictionary has a key:value pair. Each key-value pair maps the key to its associated value. Dictionaries are optimized to retrieve values when the key is known. You can define a dictionary by enclosing a comma-separated list of key-value pairs in curly braces ({}). A colon (:) separates each key from its associated value. An example is a simple German-English dictionary:
colours = {"red" : "rot", "green" : "grün", "blue" : "blau", "yellow":"gelb"}print en_de["red"]#output will be the value of 'red'( the key) which is 'rot'empty_dict = {} #a dictionary can also be empty waiting to be filled up with information 
5. Loopsare great when trying to run the same block of code over and over again. In python, there are two types of loops: for and while. For this analysis, we will focus more on the for loop. The for loop is used to iterate over elements of a sequence. It is often used when you have a piece of code which you want to repeat ‘n’ number of times.
It works like this: “ for all elements in a list or dictionary, do this “
For example
list_number = [2,3,4]#inputfor item in list_number:    multiple = 2 * item    print(multiple) 
Going back to our dataset
To know the most repeated word(s), we need to know the number of times each word appeared in the lyrics. To do that, we will need to use both a dictionary(to store each word with their corresponding count) and a for loop (to iterate the counting process as each word appears)
First, we need to store the unique words in a dictionary
Input & Output
Then we need to use the for loop again to count the number of times each unique word appears in the whole lyrics.
Input & Output
Now we have found out the number of times each word appears, let's sort them out to view them from highest to lowest using the sorted ( ) function
There seem to be too many words that only occurred once. Because our aim is to find the most popular word or words, we will narrow our list to the 10 words by slicing it. You can learn more about slicing here
Input & Output
Then changing back to a dictionary
An additional question will be: What are the 10 most popular words in the song called perfect by Ed Sheeran? We can easily extract this information by using the key ( )method under the dictionary data structure and then creating a list of those words.
Input & Output
From the above output code, we can confidently say that the most popular word used in the song called Perfect by Ed Sheeran is ‘I’ which appeared 24 times
Let’s improve our analysis a little further
6. Visualizing data with python.Various techniques have been developed for presenting data visually but in this analysis, we will focus data visualization strictly on a library in Python, namely Matplotlib.
Lets quickly visualize the analysis on the top 10 most popular words in our case study
What are some key insights we can draw from this case study? I can spot out three
Based on the song of choice, we see that unique words are only 44% of the total words used
Out of the 129 unique words used, about 70 words appeared once which is about 24% of the total words used.
The word ‘I’ was used 24 times in the entire songs i.e 8 % of the time, an ‘I’ was used.
This case study does help to see that a greater percentage of a song is made up of repetitive words. However, we can not conclude with just one song that Donald Knuth theory is true. We will need to analyze a lot more songs to conclude that hit songs are as a result of the repetitiveness of words in a song. Although it is good to point out that Perfect by Ed Sheeran was actually a Hit song... (and still is in my world).
If you want to improve your Python skills, these are some articles that may help
24 Best Python Courses, Books, and Online Tutorials 2018
Want to Learn Python? — Functions, Explained, by Ben B
Python Excel Tutorial: The Definitive Guide
P.S Like me, anyone can learn to be a data analyst and if you want to be notified on my next project or updates on my learning, feel free to sign up to my newsletter