CS 111 f21 – Nested lists; strings; input/output

1 Poll

What will be printed?1

data = [[1,2],[3,4],[5,6]]
acc = 0
for x in data:
    for num in x:
        acc = acc + num
print(acc)

What might nested lists be useful for?

1.1 Indexing a nested list

  • We can use the indexing we've seen to go one layer into a nested list:

    data = [[1, 2], [3, 4], [5, 6]]
    print(data[0]) # prints [1, 2]
    print(data[1]) # prints [3, 4]
    
  • and then the same idea applies to accessing elements within the inner lists:

    x = data[0]
    print(x[0]) # prints 1
    print(x[1]) # prints 2
    
  • We can combine these into a single expression:

    print(data[0][0]) # prints 1
    print(data[0][1]) # prints 2
    print(data[2][0]) # prints 5
    print(data[1][1]) # prints 4
    
  • Given this list

    data = [[3, 5, 7], [[2, 4], [6, 8]]]
    

    what code would print out 4?2

2 Concatenation

  • We can use + to combine two lists into a single list
nums1 = [3, 9, 2]
nums2 = [4, 5, 6]
print(nums1 + nums2) # prints [3, 9, 2, 4, 5, 6], not [7, 14, 8]
  • How would you write code to do the pairwise addition?

3 Contains

Write a function contains that takes a list items and another parameter x. contains should return True if an element of items has the same value as x, otherwise it should return False.

def contains(items, x):
    for item in items:
        if x == item:
            return True
    return False

3.1 in operator

Python gives us a built-in shortcut for the function we wrote above: x in items returns the same result as contains(items, x)

4 Strings

  • a sequence of characters just like a list is a sequence of elements
  • lists can contain anything, even a mix of different things, but strings only have characters
    • a character is a single letter, number, symbol, or blank
      • blanks: " " (space), "\t" (tab), "\n" (newline)
  • just like lists:
    • len returns the length
    • can loop over characters with for
    • use in to check for containment
    • access individual characters with indexes
    • concatenate with +
  • unlike lists
    • fixed, cannot be modifed
      • immutable vs mutable, more detail next week
    • in checks for matching substrings instead of single characters
      • [2, 3] in [1, 2, 3] is False because [2, 3] does not match any single element of the list
      • but "23" in "123" is True because "23" matches up with part of the string
  • Python provides lots of useful operations

5 Files

happy_file = open("happy_little_file.txt")
file_text = happy_file.read()
print(file_text)
happy_file.close()

favorite_number = 42
new_file = open("another_happy_file.txt", "w")
new_file.write("this file is so happy\n") # need to include \n to make a newline
# convert favorite_number to a string with the str function
# use + to concatenate the parts of the output together into one string
new_file.write("my favorite number is " + str(favorite_number) + ", it's the best\n")
new_file.write("just happy to be a file\n")
new_file.close()

6 Debugging word_check.py

open("words.txt")
for line in words:
    words.append(line)

check-word = input("Enter your potential word: ")

for word in words:
    word = words[i]
    if word = check-word:
        print check-word, "is a word"
        sys.exit()

print(check-word, "is not a word")
print("trust me, I checked", len(words), "words")

Footnotes:

1

21

2

print(data[1][0][1])