Skip to content
rootpath
Share
Explore
root

icon picker
BeatBox


Melody Structures


create melody


We can create melodies by coding structures to hold pitch data. These structures are like sequence of
boxes, with each box holding some data, or a pitch, inside. Let's code some melodies.
Let us first create a melody using a list of notes.
Create a list and access an element with [ ] ( remember, elements start at 0 ) :
1
melody = [0, 5, 7]
2
run.melody[0]
0
There are no rows in this table

edit melody


Lists are mutable, meaning we can change or mutate them. Let's make some changes.
Try adding some stuff with append( ) and extend( ),
1
melody.add(5)
2
run.melody[4]
5
There are no rows in this table

and then take something off of the list with pop( ):
1
one = melody.del(0)
run.melody[3]
5
There are no rows in this table
2 or more lists can be added together to form a new list:
1
melody2 = [2, 9, 11]

melody3 = melody1+melody2
5, 7, 5, 2, 9, 10
There are no rows in this table

save tuning


If we would like to save our tuning, we can save it as a set of pitches. The set mutable, kind of like lists, except
in a set, every kind of thing can only appear once.
Set example:
1
build set
setOne = {1,2,3,4}
setTwo = {1,5,7,9}
2
union, intersection, difference
unionSet = setOne.union(setTwo)

intersect = setOne.intersection(setTwo)

difference = setOne.difference(setTwo)
{1, 2, 3, 4, 5, 7, 9}

{1}

{2, 3, 4}
There are no rows in this table
Dictionaries are some of the most powerful Python data structures. They hold data in key value pairs.
construct dictionary and get value y by key x
1
dictionaryOne = {1: "b", 2: 19 }
2
print(dictionaryOne.items())
3
dictionaryOne.get(1)
There are no rows in this table

Control Flow


Create loop


Loops are not data structures; rather, they are control structures, or structures for controlling other
structures. Lets loop through a list and run each note
1
for(note in melody):
run.note
5, 7, 5, 2, 9, 10
There are no rows in this table

Functions


Create Functions


Functions are blocks of organized code that run when given a push.
We're going to build different types of functions.
simple function examples:
1
zero input
def Allahuabha():
print("Allahuabha!")

Allahuabha()
Allahuabha!
2
single input
def plus(input):
print(input+1)

plus(0)
1
3
multiple input
def adder(input1, input2):
print(input1+input2)

adder(5,9)
14

4
nested functions
def addem(x,y):
def inner(val):
return val**val
return(inner(x), inner(y))
There are no rows in this table

Lambda functions are anonymous functions.
construct lambda function:
1
echo_word = (lambda word1, echo: word1 * echo)

result = echo_word('hey', 5)

print(result)
There are no rows in this table
construct filter lambda
1
quarks = ['up', 'down', 'strange']

result = filter(lambda type: len(type) > 5, quarks)

result_list = list(result)
There are no rows in this table
filter DataFrame with lambda
1
result =
filter(lambda x: x[0:2] == 'RT', tweets_df['text'])

res_list = list(result)

for tweet in res_list:
print(tweet)
There are no rows in this table
construct map lambda
1
charge = ["+", "-"]

change_charge =
map(lambda item: item+"-", charge)

charge_list = list(change_charge)
There are no rows in this table
construct reduce lambda
1
from functools import reduce
quark = ['up', 'down','strange']

result =
reduce(lambda item1, item2: item1 + item2, stark)
There are no rows in this table

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.