Software Carpentry
Strings, Lists, and Files


Introduction


You Can Skip This Lecture If...


Strings


Immutability


Slicing


Bounds Checking


Negative Indices


Consequences


Methods


String Methods


Notes on String Methods


Chaining Method Calls


Testing for Membership


Lists


Modifying Lists


Concatenation


Deleting List Elements


List Methods


Notes on List Methods


For Loops


Ranges


Ranged Loops


Membership


Nesting Lists


Aliasing


Indexing vs. Slicing


Tuples


Multi-Valued Assignment


Unpacking Structures in Loops


Files


Copying a File


Looping Over Files


Other Ways To Copy Files


Summary


Exercises

Exercise 8.1:

What does "aaaaa".count("aaa") return? Why?

Exercise 8.2:

What do each of the following five code fragments do? Why?

x = ['a', 'b', 'c', 'd']
x[0:2] = []
x = ['a', 'b', 'c', 'd']
x[0:2] = ['q']
x = ['a', 'b', 'c', 'd']
x[0:2] = 'q'
x = ['a', 'b', 'c', 'd']
x[0:2] = 99
x = ['a', 'b', 'c', 'd']
x[0:2] = [99]

Exercise 8.3:

What does 'a'.join(['b', 'c', 'd']) return? If you have a list of strings, how can you concatenate them in a single statement? Why do you think join is written this way, rather than as ['b', 'c', 'd'].join('a')?

Send comments