Question: Sets in Python Enter each instruction in the Thonny shell. For each numbered instruction type the output on the answer sheet. Submit this on Piazza

Sets in Python
Enter each instruction in the Thonny shell. For each numbered instruction type
the output on the answer sheet. Submit this on Piazza by the end of class.
The set of the first four counting numbers can be created in Python by listing
the numbers in curly braces:
N4={1,2,3,4}
A list of the first four numbers
L4=[1,2,3,4]
is constructed in Python with square brackets. Unlike a set, a list of numbers can
contain duplicates and the list remembers the order of the items in the list. For
example, the set of the first four counting numbers will always contain just four
numbers and the order is not remembered. The difference between a list and a set
can be seen as follows:
nums =[4,1,4,3,2,1,4,2]
print(nums) #1
N4= set(nums)
print(N4) #2
The third instruction above shows how a set can be constructed from a list. In this
case N4 names the set of the first four integers (duplicates in the list are ignored)
and the order of the numbers in the list is forgotten.
Because the order does not matter for a set, Python displays the set elements
according to some internal representation (which makes accessing elements of
the set easy for Python) rather than the order of the items in the list from which
the set is made. For example, try to predict how the following set is presented (by
the print instruction):
nums 2={21,22,31,44}
print (nums2)
The fact that the printed order cannot be easily predicted corresponds to fact that
set elements are not ordered by any usable scheme.
Another example of a set is the set letters in the word 'example':
letters ={'a',?'e',?'m',?'l',?'p',?'x'}
print(letters)
This set can be constructed with less typing as follows:
Sets in Python Enter each instruction in the

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!