Search code examples
sage

Sets and set operations in Sage


How am I able to input this into Sage?

A = {x ∈ ℕ | x ≤ 10}

B = {a, b}

I've looked at the docs and they are not clear. I am trying to find the union and distinction between them afterwards.


Solution

  • This might be what you want.

    sage: A = set(0 .. 10)
    sage: B = {4, 12}
    
    sage: A.union(B)
    {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12}
    sage: A.intersection(B)
    {4}
    
    sage: A.difference(B)
    {0, 1, 2, 3, 5, 6, 7, 8, 9, 10}
    sage: A.symmetric_difference(B)
    {0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 12}