Lists

Lists allow for data abstraction

  • Variables like strings, numbers, characters, and more can be bundled together
  • It could be empty from the start and you can add more variables as needed
  • If you set one list equal to another list, or transfer data from one list to another, the data will be completely replaced.
  • You can also append data from one list to another, keeping the old data and adding the new data.

Key Vocabulary

  • list: a sequence of several variables grouped together
  • variable: a way of storing information in a computer program, which could later be changed, referenced, and used
  • data types: a set of values and operations on those values
  • abstract data types: a data type whose internal representation is hidden from the client
  • client: a program that uses a data type
  • objects: a structure that can take on a data-type value
  • Applications programming interface (API): which is a list of constructors and instance methods or operations, used to specify the behavior of an abstract data type

Big Ideas

  1. Lists can store any types of elements.
  2. In all lists operations, if a list index is less than 1 or greater than the length of the list, an error message is made and the program will terminate.
  3. Data abstraction provides a separation between the abstract properties of a data type and the concrete details of its representation.

1. Your turn to interact, try it now!

Modify the list to change the length to 5.

languages_list = []
languages_list = ["Python", "C++", "JavaScript", "Java", "C#"]

2. Change the following code to print out the list.

languages_list = []
languages_list = ["Python", "C++", "JavaScript"]
print(languages_list)
['Python', 'C++', 'JavaScript']

3. Change the following code to print out only "Python"

languages_list = []
languages_list = ["Python", "C++", "JavaScript"]
print(languages_list)
['Python', 'C++', 'JavaScript']

4. Replace contents/data of listA with contents/data from listB.

Print listA out afterward

listA = []
listA = [1, 55, 8, 2, 76]
listB = []
listB = [22, 7, 13]
listA = listB
print(listA)
[22, 7, 13]

5. Make 2 lists:

  • a list of string data
  • a list of number data
  • the length of the each list is a minimum of 4
  • change the names of the lists
letters = ["a", "b", "c", "d"]
numbers = [1, 2, 3, 4]

6. Combine the list to contain all of the data from both lists.

Hint! Use extend or append
letters = ["a", "b", "c", "d"]
moreLetters = ["e", "f", "g", "h"]
for letter in moreLetters:
    letters.append(letter)

print(letters)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

A Little Bit on Binary

  • A type of positional number system, with the base of 2 and uses exponents within it.
  • It is represented in zeroes and ones, each different column, lightbulb, or place representing a different value. From the right it starts as 2^0 which equals 1, then 2^1 = 2, then 2^2 = 4, and so on
  • It connects with data abstraction because the more complex data, calculating the value, is represented in binary with base of 2, however it is simplified to only show combinations of the digits zero and one.

Binary Hacks

Convert these binary notation to decimal notation. (the way we normally count)

  1. The binary number 111.
    Click for the answer! 7

  1. The binary number 1011.
    Click for the answer! 11

  1. The binary number 1101011.
    Click for the answer! 107

Convert the decimal notation to binary notation. (You can use the Binary Math from Mr. Yeung or the one you have)

  1. 12
    Click for the answer! 1100

  1. 44
    Click for the answer! 101100

  1. 254
    Click for the answer! 11111110

Extra Binary Hacks if you changed your bits to 24

Convert decimal notation to binary notation.

  1. 57345
    Click for the answer! 1110000000000001

  1. 16777215
    Click for the answer! 111111111111111111111111

  1. 11184810
    Click for the answer! 101010101010101010101010

Convert the binary notation to decimal notation

  1. 101011101010
    Click for the answer! 2794

  1. 10011100000
    Click for the answer! 1248

  1. 1101001000101000
    Click for the answer! 53800

Homework/Hacks

Consider the following code segment:

  • scores1 <- [89, 78, 92, 63, 95, 88]
  • scores2 <- [92, 79, 97, 63]
  • scores1 <- scores2

What are the contents of scores1 after the code segment is executed?:

  1. [89, 78, 92, 63, 95, 88]
  2. [89, 78, 92, 63, 95, 88, 92, 79, 97, 63]
  3. [92, 79, 97, 63, 89, 78, 92, 63, 95, 88]
  4. [92, 79, 97, 63]
Click for the answer! 4. Because the data is not being appended from scores2 into scores1.

Consider the following code segment:

  • listA <- ["Sam", "Ann"]
  • listB <- ["Jamal", "Tamara"]
  • listB <- listA
  • listA <- listB

What are the contents of listA after the code segment is executed?

  1. ["Sam", "Ann"]
  2. ["Jamal", "Tamara"]
  3. ["Sam", "Ann", "Jamal", "Tamara"]
  4. ["Jamal", "Tamara", "Sam", "Ann"]
Click for the answer! 1. Because all of the data from listA "Sam" and "Ann" replace what is in listB. So if listB replaces what is in listA, "Sam" and "Ann" will still be the only data in that list.

What is the length of this list? ["Red", "Orange", "Yellow", "Green", "Blue", "Purple"]

  1. 5
  2. 7
  3. 6
  4. 4
Click for the answer! 3. The length of the list is 6.

What is the index number of "Purple" in this list? ["Red", "Orange", "Yellow", "Green", "Blue", "Purple"]

  1. 7
  2. 0
  3. 6
  4. 5
Click for the answer! 4. The index count starts at 0, making "Red" 0 and "Purple" index 5.

Which of the following types of data can be stored in a list?

  1. Boolean
  2. String
  3. Float
  4. All of the above
Click for the answer! 4. Any type of data can be stored within a list. A list can contain a mix of types of data.

Which of the following variables is a float?

  1. Apples
  2. -106.2
  3. 34
  4. True
Click for the answer! 2. A float is a decimal number.

If a list has a length of 24 items, what is the index number of the 17th item?

  1. 21
  2. 17
  3. 16
  4. 69
Click for the answer! 3. An index count starts at 0 so the index number of a variable is one less than it's spot in the length.

A variable is permanent and cannot be changed later on.

  1. True
  2. False
Click for the answer! 2. False. Variables can be changed later on in the code.

Which of the following is true about the list? ["Apples", 42.0, "Bananas", 0.5, "Avocado", -902.2, "Lychee", 6.9, "Orange", 7.2]

  1. The list has floats and string variable types.
  2. The ratio of float variables to string variables is 2:1.
  3. The length is 9.
  4. The index of "Avocado" is 4.
  5. All of the above
  6. 1 and 4
  7. 1, 3, and 4
Click for the answer! 6. The list has floats or decimals and strings, and the length is 10, the ratio of string to floats is 1:1, and the index number of "Avocado" is 4.