Python Sets Management Lab

PythonPythonBeginner
Practice Now

Introduction

Welcome to the "Python Sets Lab," where you are tasked with the challenge to master the arcane powers of Python sets in a breath-taking underworld scenario. The scene is set within the Infernal Forge, a shadowy realm where fire crackles and the air is thick with sultry vapors. Here, the Demon King, ruler of the nether regions, has found his domain's treasures disappearing—magical entities unique in nature, never to be duplicated. An ancient script reveals a solution: the utilization of Python sets to manage his kingdom's assets without redundancy.

Your mission, should you accept it, is to harness the power of Python sets to assist the Demon King in organizing his mystical treasures ensuring each is as unique as the ghastly flames of the forge itself. Fascinate the Demon King with your prowess, and you may find yourself praised amongst the legends of the netherworld.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python/DataStructuresGroup -.-> python/sets("`Sets`") subgraph Lab Skills python/sets -.-> lab-271590{{"`Python Sets Management Lab`"}} end

Creating Your First Set

In this step, you will create your first Python set to hold the unique treasures of the Demon King. We assume you are already seated at your terminal in /home/labex/project. Let's begin with the basics by creating a simple set and adding a few entities to it.

Open /home/labex/project/unique_treasures.py and input the following code:

## unique_treasures.py

## Creating an empty set
treasures_set = set()

## Adding unique treasures to the set
treasures_set.add('Soul Gem')
treasures_set.add('Infernal Blade')
treasures_set.add('Cursed Crown')

print(treasures_set)

After saving the file, run your code with:

python unique_treasures.py

The expected output should display a set with the three unique treasures:

{'Soul Gem', 'Cursed Crown', 'Infernal Blade'}

The order might differ since sets do not maintain order.

Set Operations

Now that you have created a set, it's time to learn how to perform set operations. The Demon King wishes to combine his treasures with those of an allied Overlord but only wants to list each unique item once. Additionally, he wants to know which treasures he has in common with the ally for a special ritual.

Here's what you'll add to /home/labex/project/set_operations.py:

## set_operations.py

## Demon King's treasures
demon_king_treasures = {'Soul Gem', 'Infernal Blade', 'Cursed Crown'}

## Allied Overlord's treasures
overlord_treasures = {'Elixir of Immortality', 'Soul Gem', 'Chalice of Power'}

## Union of both sets for a combined unique collection
all_unique_treasures = demon_king_treasures.union(overlord_treasures)

## Intersection of both sets for common treasures
common_treasures = demon_king_treasures.intersection(overlord_treasures)

print('All Unique Treasures:', all_unique_treasures)
print('Common Treasures:', common_treasures)

After saving, execute the program with:

python set_operations.py

You should see an output listing all unique treasures, and the common ones should list the Soul Gem:

All Unique Treasures: {'Infernal Blade', 'Elixir of Immortality', 'Cursed Crown', 'Chalice of Power', 'Soul Gem'}
Common Treasures: {'Soul Gem'}

Summary

In this lab, we delved into the world of Python sets within an imaginative scenario involving the Infernal Forge and the Demon King. We started by creating a simple set in Python to manage unique items and then progressed to applying set operations such as union and intersection to unravel complex problems.

Overall, not only did we achieve our goal to assist the Demon King in managing his treasures, but we also enhanced our understanding of a powerful data structure in Python: the set.

Other Python Tutorials you may like