Homework

albums = [
    ("Welcome to my Nightmare", "Alice Cooper", 1975,   # First album list
     [
         (1, "Welcome to my Nightmare"),
         (2, "Devil's Food"),
         (3, "The Black Widow"),
         (4, "Some Folks"),
         (5, "Only Women Bleed"),
     ]
     ),
    ("Bad Company", "Bad Company", 1974,   # Second album list
     [
         (1, "Can't Get Enough"),
         (2, "Rock Steady"),
         (3, "Ready for Love"),
         (4, "Don't Let Me Down"),
         (5, "Bad Company"),
         (6, "The Way I Choose"),
         (7, "Movin' On"),
         (8, "Seagull"),
     ]
     ),
    ("Nightflight", "Budgie", 1981,
     [
         (1, "I Turned to Stone"),
         (2, "Keeping a Rendezvous"),
         (3, "Reaper of the Glory"),
         (4, "She Used Me Up"),
     ]
     ),
    ("More Mayhem", "Imelda May", 2011,
     [
         (1, "Pulling the Rug"),
         (2, "Psycho"),
         (3, "Mayhem"),
         (4, "Kentish Town Waltz"),
     ]
     ),
]

album = input()
song = input()
print("Playing " + "\"" + str(albums[int(album)-1][3][int(song)-1][1]) + "\"")
Playing "Welcome to my Nightmare"

Extra

The following code displays information of the album from the list using JavaScript and HTML.




<table id="list">
<tr>
<th>Album name</th>
<th>Artist</th>
<th>Year</th>
<th>Tracks</th>
</tr>
</table>


<script>

albums = [["Welcome to my Nightmare", "Alice Cooper", 1975, 
[
    [1, "Welcome to my Nightmare"], 
    [2, "Devil's Food"],
    [3, "The Black Widow"], 
    [4, "Some Folks"],
    [5, "Only Women Bleed"]
]
],
["Bad Company", "Bad Company", 1974, 
[
    [1, "Can't Get Enough"], 
    [2, "Rock Steady"],
    [3, "Ready for Love"], 
    [4, "Don't Let Me Down"],
    [5, "Bad Company"],
    [6, "The Way I Choose"],
    [7, "Movin' On"],
    [8, "Bad Seagull"]
]
], 
["Nightflight", "Budgie", 1981, 
[
    [1, "I Turned to Stone"], 
    [2, "Keeping a Rendezvous"],
    [3, "Reaper of the Glory"], 
    [4, "She Used Me Up"]
]
], 
["More Mayhem", "Imelda May", 2011, 
[
    [1, "Pulling the Rug"], 
    [2, "Psycho"],
    [3, "Mayhem"], 
    [4, "Kentish Town Waltz"]
]
] 
];

var table = document.getElementById("list");
//var row = table.insertRow(1);
//var row2 = table.insertRow(2);


for (let i = 0; i < albums.length; i++) {
var row = table.insertRow(i+1);
    for (let j = 0; j < albums[i].length; j++) {

var cell1 = row.insertCell(j);
if (j == 3) {
    for (let k = 0; k < albums[i][3].length; k++ )
    cell1.innerHTML += albums[i][3][k][0] + " " + albums[i][3][k][1] + "<br>"; 
} else {
cell1.innerHTML = albums[i][j];
}
    }


}


</script>
Album name Artist Year Tracks


Ignore this, this is just testing JavaScript

albums = [["Welcome to my Nightmare", "Alice Cooper", 1975, 
[
    [1, "Welcome to my Nightmare"], 
    [2, "Devil's Food"],
    [3, "The Black Widow"], 
    [4, "Some Folks"],
    [5, "Only Women Bleed"]
]
], 
["Bad Company", "Alice Cooper", 1975, 
[
    [1, "Welcome to my Nightmare"], 
    [2, "Devil's Food"],
    [3, "The Black Widow"], 
    [4, "Some Folks"],
    [5, "Only Women Bleed"]
]
], 

];

console.log(albums[1]);
[ 'Welcome to my NightmareBad Company',
  'Alice Cooper',
  1975,
  [ [ 1, 'Welcome to my Nightmare' ],
    [ 2, 'Devil\'s Food' ],
    [ 3, 'The Black Widow' ],
    [ 4, 'Some Folks' ],
    [ 5, 'Only Women Bleed' ] ] ]


data = [104, 101, 4, 105, 308, 103, 5, 107,
        100, 306, 106, 102, 108]    # list of the different numerical values
min_valid = 100  # minimum value
max_valid = 200  # maximum value

for i in range(0, len(data)): 
    if data[i] < 100:
        print(str(i) + " " + str(data[i]))
    if data[i] > 200: 
        print(str(i) + " " + str(data[i]))
2 4
4 308
6 5
9 306


Vocab

3.1 Variables and Assignment

  • Variables: Abstractions that store values (numbers, boolean values, strings) Variables are categorized based on data type, which typically includes: integer, double/float, string, and booleans.
  • List: Contains elements
  • Dictionaries: Consist of key value pairs

The homework example above demonstrates the use of lists and variables in Python. The album and song variable takes in a string input which is converted into an integer. The album list consists of lists and tuples, which are used to separate albums and songs.

Python

Variables

Assigning variables: variable name = value

Why use variables?

Variables help to manage complexity by storing values. They can have descriptive names that indicate what the variable does. Furthermore, variables can be reused throughout the program, and if the value needs to be changed, only the variable assignment needs to be changed, while the rest of the code can remain the same.

Lists

Assigning a list to a variable example: myList = [1, 2, 3]

Add elements to a list with: list.append(element)

Dictionaries

Create a dictionary with this syntax: dictionary name = {key:value}

JavaScript

Variables

Defining variables:

  • var variable name = value
  • const variable name = value
  • let variable name = value

Boolean

Receive a true/false output from a boolean with: Boolean(conditional)

List

const list name = [elements]

Newlines can also be used in a list for increased readability:

const name = [
    element 1,
    element 2
]


3.2 Data Abstraction

  • Data abstraction: Simplifies code by hiding unnecessary information. An example of using data abstraction is lists, which are able to store multiple values. Lists can be more useful than variables in that you can have one list that stores multiple values which would otherwise have needed multiple variables to store them.

Python

Lists

Data abstraction: Simplifies code by hiding unnecssary information. An example of using data abstraction is lists, which are able to store multiple values. Lists can be more useful than variables in that you can have one list that stores multiple values which would otherwise have needed multiple variables to store them.

Code:

  • name = [element 1 , element 2]

    Elements can also be separated by newlines to increase readability:

    name = [
      element 1, 
      element 2
    ]
    
  • id(list name): Each list has an id that can be displayed

  • list name += [element] or list name.append(element): Add an element to the list

Iterating through lists

for variable 1, variable 2 in list name:

Miscellaneous

  • string.split(): Split each word of a string into an element in a list
  • "".join(list name): Join elements of a list into a string

JavaScript

Functions

To make a function, you need to specify a function name and parameters: function name(parameter 1, parameter 2){}

def binaryCalc(num): 
    binaryNum = 0; 
    count = 0;
    binaryString = ""; 
    while binaryNum < num:
        binaryNum = 2**count 
        count += 1
    #print(count) # count = 5

    count -= 1
    while (num > 0 and count >= 0): 
        if num >= 2**count: 
            num -= (2**count)
            binaryString += "1"
            count -= 1
        else: 
            binaryString += "0"
            count -= 1
    count += 1
    #print("Count" + str(count))
    
    if count > 0: 
        while count > 0:
            binaryString += "0"
            count -= 1
    #print("binary" + binaryString)
    return binaryString
    
binaryList = []
def saveBinary(binary):
    binaryList.append(binary)
    
firstCalc = binaryCalc(25)
saveBinary(firstCalc)
secondCalc = binaryCalc(128)
saveBinary(secondCalc)
thirdCalc = binaryCalc(234)
saveBinary(thirdCalc)
print(binaryList)
['011001', '10000000', '011101010']