Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Summary Lec 5 | MIT 6.00SC Introduction to Computer Science and Programming,

Rating
-
Sold
-
Pages
2
Uploaded on
15-03-2023
Written in
2022/2023

Introduction to Computer Science and Programming,

Institution
Course

Content preview

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.

Written for

Course

Document information

Uploaded on
March 15, 2023
Number of pages
2
Written in
2022/2023
Type
SUMMARY

Subjects

$8.49
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
toufiqsamurai

Get to know the seller

Seller avatar
toufiqsamurai
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
3 year
Number of followers
0
Documents
13
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions