Thursday Mornings with Python, Part 8 - But It's A Wednesday

Thursday Mornings with Python, Part 8 - But It's A Wednesday

As I am going on annual leave tomorrow (good luck to me as I am still ill) I wanted to get one last Python session in so we've put it on a Wednesday. Hopefully I can get Sets done today. 

This will be my last blog post in a while unless I get a flash of inspiration as I am going on retreat. I still have a couple of days to heal from the cough. 

So it should all be alright. And I can take it super easy there.

Introduction to Set Operations

"A lot of the usefulness of a set container comes from the set operations."

A green image with fairy lights and gold confetti. Text reads: python: sets and lists topics

I had a brief introduction to one of these by my manager last week when I asked him for some real life examples of sets from the code. So cool! You can also combine these operations - WOW!!! You can perform complex logic.

Set Unions

This is like performing a merge. 

This works on both sets and frozensets. (A reminder - a frozenset is a set that cannot be mutated - thanks. Might skim over my last blog post thanks). 

We can return the union of two sets using the .union() method or the | operator. Doing so will return a set or a frozenset that contains the elements from both sets or frozensets. It gets rid of any duplicates. 

We can perform this operation on as many sets as we need. It definitely doesn't just stop at two!

OMG THIS IS SO COMPLICATED WHAT 

So... when combining the two sets or frozensets it is the operand on the left that takes precedences for the type. So in our two ways of forming unions - whatever we put on the left determines the type.

Let me show you:
# My favourite ice cream flavours are a set
susannas_favourite_ice_cream_flavours = {"vanilla", "mint chocolate chip", "strawberry", "mango", "chocolate"}

# My sister's favourite ice cream flavours are a frozenset 
francescas_favourite_ice_cream_flavours = frozenset({"vanilla", "straciatella", "mint chocolate chip", "maple walnut", "maple pecan"})

# I am going to use union and put my frozenset first
# This one outputs a frozen set

frozen_ice_cream_set = francescas_favourite_ice_cream_flavours.union(susannas_favourite_ice_cream_flavours)

print(frozen_ice_cream_set)

# Here I put my regular set first
# This one outputs a regular set 
regular_ice_cream_set = susannas_favourite_ice_cream_flavours | francescas_favourite_ice_cream_flavours

print(regular_ice_cream_set)
The output would be: 
frozenset({'maple walnut', 'mint chocolate chip', 'vanilla', 'mango', 'straciatella', 'maple pecan', 'chocolate', 'strawberry'})
{'mint chocolate chip', 'vanilla', 'maple walnut', 'mango', 'straciatella', 'maple pecan', 'chocolate', 'strawberry'}

Set Intersection

Okay, so what might this one be? Hmmm? This would be the one where we find out what two or more sets have in common, I guess. The method .intersection() returns a new set of all of the elements the set or mutliple sets have in common wow. 

I already love it it's genius. So cool. I LOVE PYTHON, WOW!!! This intersection method can also be done with the & operator. I am already spotting a them here that these set methods can be done with a series of operators - which is so so so so cool this is really so so so so so so cool wow.

The theme continues - and it's so much easier to get this time round - the type of the first operand determines the overall type of the output. I am gonna need to grasp this one.

Thank you so much.

So for this code:
# I want to find the intersection between our favourite flavours and keep it a frozenset

frozenset_intersection = francescas_favourite_ice_cream_flavours & susannas_favourite_ice_cream_flavours

print(frozenset_intersection)

# I want to find the intersection and make it a regular set

intersection = susannas_favourite_ice_cream_flavours.intersection(francescas_favourite_ice_cream_flavours)

print(intersection)
The output would be:
frozenset({'vanilla', 'mint chocolate chip'})
{'vanilla', 'mint chocolate chip'}
I also learned about .intersection_update

So if we say I want to copy Francesca and only have her favourite ice cream flavours - we could do this.
# I only want to have flavours I share with Franesca as my faves
susannas_favourite_ice_cream_flavours.intersection_update(francescas_favourite_ice_cream_flavours)

print (susannas_favourite_ice_cream_flavours)

The output would be:
{'vanilla', 'mint chocolate chip'}

Set Difference

We can also find unique elements in one set. Sets and frozensets use the .difference() method or the - operator - aha you see, another operator. I was right after all! I love patterns like these. I love patterns.

This returns a set or a frozenset which contains only elements from the first set which were not found in the second set. 

So supposing I decide that I hate all of Francesca's ice cream flavours

This code:
# I want the difference of only mine so it will be a normal set

susannas_difference = susannas_favourite_ice_cream_flavours.difference(francescas_favourite_ice_cream_flavours)

print(susannas_difference)

Would output:
{'chocolate', 'mango', 'strawberry'}

Or that she hates all of mine...

This code:
# I want only my sister's difference and so it will remain a frozen set

francescas_difference = francescas_favourite_ice_cream_flavours - susannas_favourite_ice_cream_flavours

print(francescas_difference)

Would output:
frozenset({'maple walnut', 'maple pecan', 'straciatella'})

As always the type of the FIRST OPERAND determines the return type.

Similarly to the above, you can also update the existing set. You can do .difference_update()

Here is my code:
# I only want to have flavours I don't share with Franesca as my faves
susannas_favourite_ice_cream_flavours.difference_update(francescas_favourite_ice_cream_flavours)

print (susannas_favourite_ice_cream_flavours)

And here is the output: 
{'chocolate', 'strawberry', 'mango'}

Symmetric Difference

The lest lesson on my list is symmetric difference.

We have covered it with my manager twice now so it all makes good sense.

And I also like the way the course explains them. 

They are the opposite of an intersection.

"A resulting set will include all of the elements which are from one set or the other, but not both."

"In other words, elements that are unique to each set."

We can achieve this using the .symmetric_difference() method or ^ operator. As with the others, you can do .symmetric_difference_update() on an existing set as well. Here are some code examples.

So here is all of my remaining code for this lesson:
# Symmetric difference as a set

set_symmetric_difference = susannas_favourite_ice_cream_flavours ^ francescas_favourite_ice_cream_flavours

print(set_symmetric_difference)

# Symmetric difference as a frozenset

frozenset_symmetric_difference = francescas_favourite_ice_cream_flavours.symmetric_difference(susannas_favourite_ice_cream_flavours)

print(frozenset_symmetric_difference)

# Update my existing set to be symmetric difference

susannas_favourite_ice_cream_flavours.symmetric_difference_update(francescas_favourite_ice_cream_flavours)

print(susannas_favourite_ice_cream_flavours)

And here is the output:
{'chocolate', 'strawberry', 'maple pecan', 'straciatella', 'mango', 'maple walnut'}
frozenset({'chocolate', 'strawberry', 'mango', 'maple pecan', 'straciatella', 'maple walnut'})
{'mango', 'maple pecan', 'straciatella', 'chocolate', 'strawberry', 'maple walnut'} 

Sets Review 


A Venn diagram with all four types of set operations that I have covered:

A venn diagram from the codecademy website covering all four types of set operations

This has been an amazing lesson. 

Thank you.

Quiz

Now time to quickly do a quiz before lunch:

YAYY I GOT 100% ON THE FIRST GO.

A python quiz score of 100% on a Sets quiz from codecademy

What is not covered

The course does not cover subsets and supersets and instead it directed me to the docs.

I might ask about this later today - thank you. xxx

Questions 

  • Collections are coming next. What is a collection?

Comments

Popular posts from this blog

Hello World

Yosemite

Where To Hide A Star