-1

I am trying to run multiple lines of code via the same dataset. What I am trying to run is this:

10
x = {1, 2, 3, 4, 5}
y = {2, 8, 5, 10}

z = x.union(y)
z = x.intersection(y)
z = x-y
z = y-x
z = x.union(y) - x
z = z = x.union(y) - y

It won't let me run all the commands at the same time. Is there a way to do this that i'm missing?

EDIT: I want to do this:

10
x = {1, 2, 3, 4, 5}
y = {2, 8, 5, 10}

z = x.union(y)
print(z)

z = x.intersection(y)
print(z)

z = x-y
print(z)

z = y-x
print(z)

z = x.union(y) - x
print(z)

z = z = x.union(y) - y
print(z)

I want to run them all at the same time to save time. Let me know if things make sense

ab4444
  • 37
  • 6
  • Elaborate a bit more please, do you mean you want to run all of these lines of code in one line and assign them to z? – ConnerWithAnE Nov 28 '20 at 04:20
  • I basically want to run each line and get a different output for every equation listed. However, I want to run it all at the same time, if that makes sense – ab4444 Nov 28 '20 at 04:22
  • 3
    *What do you want to happen* as a result of running this code? What does "at the same time" actually mean? After all of this has happened, what should `z` be equal to? What do you mean about "getting output"? When you assign to a variable, that *does not produce output*, so it's hard to understand you. – Karl Knechtel Nov 28 '20 at 04:22
  • Since the equations are using the same data set, I want to separate them and output a new set via print(z). I want to do this every time for each equation. I guess chain the lines of code? Sorry, this is my first time using Python so I apologize if i'm not being clear – ab4444 Nov 28 '20 at 04:28
  • 1
    Those are *sets*, not datasets. – Brian McCutchon Nov 28 '20 at 04:40

1 Answers1

1

You can chain most set operations by using the method of the returned object directly (which is a reference to a new set)

>>> x = {1, 2, 3, 4, 5}
>>> y = {2, 8, 5, 10}
>>> x.symmetric_difference(y)
{1, 3, 4, 8, 10}
>>> x.symmetric_difference(y).union(y)
{1, 2, 3, 4, 5, 8, 10}

operations which can't be directly chained can be enclosed in parentheses

>>> (y - x)
{8, 10}
>>> (y - x) ^ x
{1, 2, 3, 4, 5, 8, 10}
ti7
  • 16,375
  • 6
  • 40
  • 68