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:
A list of the first four numbers
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
printnums #
N setnums
printN #
The third instruction above shows how a set can be constructed from a list. In this
case N 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
print
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
print
This set can be constructed with less typing as follows:
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
