Creating a Set Named Colors with the 7 Colors in a Rainbow
Which statement correctly creates a set named colors that contains the 7 colors in a rainbow?
a. colors = ["red", "orange", "yellow" "green", "blue", "indigo", "violet"]
b. colors = {red, orange, yellow, green, blue, indigo, violet}
c. colors = [red, orange, yellow, green, blue, indigo, violet]
d. colors = {"red", "orange", "yellow", "green", "blue", "indigo", "violet}
Answer: d. colors = {"red", "orange", "yellow", "green", "blue", "indigo", "violet"}
Explanation:
We use "{}" to write a set in it.
Also, the names of the colors are strings so we use inverted commas (") to express them.
So, the set named colors that contains the 7 colors in a rainbow will be:
colors = {"red", "orange", "yellow", "green", "blue", "indigo", "violet"}
Hence, the correct option is d.
Which syntax is used to create a set in Python? The syntax used to create a set in Python is by using "{}" brackets.