Python Dictionaries for Stellar Exploration

PythonPythonBeginner
Practice Now

Introduction

In the mystic canvas of the night sky, where twinkling stars whisper the secrets of the cosmos, our scenario unfolds. Imagine you are a renowned interstellar explorer, commissioned by the Galactic Council to map the uncharted territories of the Polaris Sector. Your mission is to catalog celestial bodies and anomalies, discern their attributes, and store this vital data within the databanks of your spacecraft's computer.

With the vastness of space as your frontier, the challenge lies in organizing this information efficiently and effectively. This is where Python dictionaries come into play – your most trusted tool in the vastness of the stars. Your goal is to master the Python dictionary to store and manipulate data about stars, planets, and other celestial phenomena you encounter on your journey.

Prepare to dive into the realm of Python dictionaries and wield their power to chart the stars!


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") subgraph Lab Skills python/dictionaries -.-> lab-271547{{"`Python Dictionaries for Stellar Exploration`"}} end

Creating Your First Dictionary

In this step, let's start by creating a dictionary to represent a single star system. We will record its name, type, and the number of confirmed planets.

In /home/labex/project/star_catalog.py, write the following Python code:

## Define a dictionary for the star system
star_system = {
    "name": "Terra Nova",
    "type": "G-type main-sequence",
    "confirmed_planets": 3
}

## Print the dictionary to check its contents
print(star_system)

After saving the file, you can execute the script with the following command:

python star_catalog.py

You should see an output that resembles:

{'name': 'Terra Nova', 'type': 'G-type main-sequence', 'confirmed_planets': 3}

This output confirms that you have successfully created and printed a Python dictionary.

Accessing and Modifying Dictionary Values

Now that you have a star system dictionary, let's practice accessing and modifying its values to reflect new astronomical data.

In the /home/labex/project/star_catalog.py, append the following code:

## Access the number of confirmed planets
num_planets = star_system["confirmed_planets"]
print(f"The star system {star_system['name']} has {num_planets} confirmed planets.")

## Update the number of confirmed planets
star_system["confirmed_planets"] = 4
print(f"Updated number of confirmed planets: {star_system['confirmed_planets']}")

Run the script again with:

python3 star_catalog.py

The terminal should output the current number of planets followed by the updated number:

The star system Terra Nova has 3 confirmed planets.
Updated number of confirmed planets: 4

Summary

In this lab, we embarked on a fantastic journey through the stars, empowering ourselves with the magic of Python dictionaries. Starting from the creation of a simple star system record, progressing to data retrieval and modifications, we've unraveled some of the capabilities these versatile data structures hold.

By practicing within the context of space exploration, we've learned to anchor abstract concepts to tangible goals—an approach proven to enhance learning. Moving beyond the basics, future labs will introduce more advanced dictionary operations and their applications in diverse programming scenarios.

Through this lab, not only have you gained mastery over Python dictionaries, but also sparked an explorer's curiosity that drives us to uncover the unknown one dictionary key at a time.

Other Python Tutorials you may like