Lec 5 | MIT 6.00SC Introduction to Computer Science and Programming,
There are three data structures in Python that are commonly used to collect items: tuples,
lists, and dictionaries. They are ordered sequences of objects, meaning that we can refer to
a first object, a second object, a last object, and so on. We can also get what are called
slices of tuples, and as we'll see, we can also slice lists. Lists are more useful than tuples,
but also more complicated. Once you've created a tuple, you can create a new tuple, but
you cannot change the value of the old one. This is referred to as mutability. It is the first
time we've seen a way to actually change the value of an object itself. However, this is also
a powerful concept and an opportunity to make programming mistakes. Let's look at an
example here: univs.append. In Python, this is called a method.
The professor mentioned that they are using a function to modify the list and that
appending is different from append. Append is a builtin function in Python that works on
sequence types like lists, and it has a side effect on the list by modifying it. The correct
answer for the problem they were discussing was 'flat' without the word 'Harvard.' Another
method that can have a side effect on a list is 'sorted.' This method puts the elements of
the list in alphabetical order and modifies the list. The professor also explained that an
assignment statement is not the same as rebinding. Assignment refers to the binding of
names to objects, whereas mutation has a side effect of modifying the object. The next
example they will discuss will illustrate this point further.
If I print l2, I will get a different value, 3, 3. This is something to keep in mind that can be
useful, but can also be confusing. If you print a new object, you'll get list two, comma, list
two, and list two. An interesting little program not in your hand out. Let me get rid of all this
cruft. So here's a function, copylist. It takes a source list and a destination list, and for e in
the source list, it appends it to whatever the destination list used to be. I am putting in a
little print statement so we'll be able to see as it runs what it's doing. A dictionary differs
from a list in two ways: the elements are not ordered, and more profoundly, the indices
need not be integers. They're not indices, and they're called keys. They can be any
immutable type. So what we have here is a dict is a set of key-value pairs. I can access it
by looking at the keys.
The key value pairs of a dictionary in Python are unordered by specification because they
are sets, not sequences. Hence, it is impossible to predict the order in which Python prints
the key value pairs. In order to ensure efficient implementation of dictionary lookup, it is
crucial to use immutable keys. Mutable types can't be employed for keys. In subsequent
lectures on the implementation of dictionaries in Python, the reasons for this will be
explained. Immutable keys can be quite useful in the upcoming problem set and in other
programming tasks. Another problem set will be posted shortly. Thank you and take care.
There are three data structures in Python that are commonly used to collect items: tuples,
lists, and dictionaries. They are ordered sequences of objects, meaning that we can refer to
a first object, a second object, a last object, and so on. We can also get what are called
slices of tuples, and as we'll see, we can also slice lists. Lists are more useful than tuples,
but also more complicated. Once you've created a tuple, you can create a new tuple, but
you cannot change the value of the old one. This is referred to as mutability. It is the first
time we've seen a way to actually change the value of an object itself. However, this is also
a powerful concept and an opportunity to make programming mistakes. Let's look at an
example here: univs.append. In Python, this is called a method.
The professor mentioned that they are using a function to modify the list and that
appending is different from append. Append is a builtin function in Python that works on
sequence types like lists, and it has a side effect on the list by modifying it. The correct
answer for the problem they were discussing was 'flat' without the word 'Harvard.' Another
method that can have a side effect on a list is 'sorted.' This method puts the elements of
the list in alphabetical order and modifies the list. The professor also explained that an
assignment statement is not the same as rebinding. Assignment refers to the binding of
names to objects, whereas mutation has a side effect of modifying the object. The next
example they will discuss will illustrate this point further.
If I print l2, I will get a different value, 3, 3. This is something to keep in mind that can be
useful, but can also be confusing. If you print a new object, you'll get list two, comma, list
two, and list two. An interesting little program not in your hand out. Let me get rid of all this
cruft. So here's a function, copylist. It takes a source list and a destination list, and for e in
the source list, it appends it to whatever the destination list used to be. I am putting in a
little print statement so we'll be able to see as it runs what it's doing. A dictionary differs
from a list in two ways: the elements are not ordered, and more profoundly, the indices
need not be integers. They're not indices, and they're called keys. They can be any
immutable type. So what we have here is a dict is a set of key-value pairs. I can access it
by looking at the keys.
The key value pairs of a dictionary in Python are unordered by specification because they
are sets, not sequences. Hence, it is impossible to predict the order in which Python prints
the key value pairs. In order to ensure efficient implementation of dictionary lookup, it is
crucial to use immutable keys. Mutable types can't be employed for keys. In subsequent
lectures on the implementation of dictionaries in Python, the reasons for this will be
explained. Immutable keys can be quite useful in the upcoming problem set and in other
programming tasks. Another problem set will be posted shortly. Thank you and take care.