Course 1.2Lesson 3 of 5
Two sets can be compared. The sign & gives the values in both sets, the intersection. The sign | gives the values in either, the union. The sign - gives the values only in the first, the difference, and ^ gives the values in exactly one of the two. The same operations exist as methods, and a few tests ask whether one set is inside another, or whether two sets share nothing. Use sets when the questions are about overlap or about different values, and use sorted to show the result.
Try the code and the simulations beside the lesson.
After this lesson you can
Two sets can be compared. This is where sets are very strong. A school has a cricket club and a chess club. Some students are in both. The questions are: who is in both? Who is in at least one? Who is only in cricket?
The picture shows two overlapping circles. The circle on the left is cricket, and the circle on the right is chess. The part where they overlap is the students in both.
Predict first. Cricket is Asha, Ravi, Meera and chess is Ravi, Kabir, Meera. Which names will each line show? Then run it.
A & B is the intersection: in both. Here: Meera, Ravi.A | B is the union: in either (at least one). Here: Asha, Kabir, Meera, Ravi.A - B is the difference: only in A. Here: Asha.B - A is only in B. Here: Kabir.A ^ B is the symmetric difference: in exactly one. Here: Asha, Kabir.Two points to notice.
A - B and B - A are different. Order matters for the difference, and does not for &, | and ^.At the end of the program, two yes or no questions:
{"Ravi"} <= cricket asks: is the first set inside the second? Every member of the first must be in the second. This is the "subset" test.cricket.isdisjoint(...) asks: do the two sets have nothing in common?| counts the members in both clubs once. That is why its size is a + b − c, and not a + b.
Use a set when the questions are about overlap: "which students do both?", "which words are in both books?", "who is in the first list and not in the second?". Also when the question is about different values: "how many different words?".
If you tried to do the same with two lists, you would write a loop with an if, and the loop would be slow for big lists. With sets, it is one line, and it is fast.
& is the word "and": in A and in B.| is the word "or": in A or in B.- is "take away": A, take away B.^ is "one or the other, but not both".| and &. Say the question aloud: "and" or "or".A - B to be the same as B - A.sorted.+ to join sets. A TypeError. Use |.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
& both, | either, - only in the first, ^ in exactly one.A - B is not the same as B - A.sorted() to show a set in order.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
sorted({1, 2} | {2, 3})?sorted({1, 2, 3} & {2, 3, 4})?sorted({1, 2, 3} - {2})?sorted({1, 2} ^ {2, 3})?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.