2. 2/32
N
Functions
D
A
Y
4
MAGIC Presenter : Fatemeh Jamshidi. June 17-22 2
● fitMedia(), setTempo(), init(), and rang() are all
examples of functions.
● Functions allow a user to execute some
preexisting section of code without having to write it
over again.
● Functions also make code flow in a clearer way.
They reduce duplication of lines and small details
that get in the way of a clear understanding of a
script’s goals.
3. 3/32
D
A
Y
4
MAGIC Presenter : Fatemeh Jamshidi. June 17-22 3
Let’s take a look at how a function is defined:
Functions
● When we define a function, we use to let python know that there is a
new function being defined.
● Then we name the function, and define what kinds of parameters it expects.
● These parameters, defined through variables, allow the function to accept
arguments.
● When is called, the files are inserted into the first two
tracks to be played simultaneously, just like the original code. It also takes a
measure, meaning that you can pass it a number to make it play at particular
times.
5. 1/5/32
Data Structure
D
A
Y
4
MAGIC Presenter : Fatemeh Jamshidi. June 17-22 5
● In order to make programming easier, computer scientists have
developed data structures.
● Data structures are used to hold information of various kinds and
make manipulating that data easier. Without data structures,
storing and manipulating data can quickly become complicated
and chaotic.
For example, imagine trying to organize books without a
bookshelf.
6. 6/32
Lists
D
A
Y
4
MAGIC Presenter : Fatemeh Jamshidi. June 17-22 6
● One very useful data structure that we’ll frequently use is called a
list.
● Every item in the list is numbered as an index.
● A list is named just like a variable is, but needs brackets around
the items that are being placed in it. These items are separated by a
comma as follows:
beats = [HIP_HOP_GUITAR1_2M, HIP_HOP_GUITAR2_4M,
HIP_HOP_GUITAR3_4M, HIP_HOP_GUITAR4_4M]
7. 7/32
Lists
D
A
Y
4
MAGIC Presenter : Fatemeh Jamshidi. June 17-22 7
Once the above code is run, our bookshelf list would look
something like this:
8. 8/32
Lists and Randomness
D
A
Y
4
MAGIC Presenter : Fatemeh Jamshidi. June 17-22 8
Another way to use lists is with randomness. Python’s
function can be used to select from a list at a random index.
Here’s the 12 measure fill example from before with random
drums:
9. 9/32
Assignment
D
A
Y
4
MAGIC Presenter : Fatemeh Jamshidi. June 17-22 9
Make a list called myMusic that contains at
least 4 samples that you think will work well
together. Use for loops and functions (as
needed) use the list to create a 12 measure
composition.