Organizing Palace Inventory with Python Lists

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will delve into the world of Python lists using the context of an ancient Egyptian pharaoh's palace. You will play the role of a guardian responsible for organizing and managing the items stored in the palace. Your mission is to learn about the fundamentals of Python lists and use your newly acquired knowledge to keep track of the palace inventory.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python/DataStructuresGroup -.-> python/lists("`Lists`") subgraph Lab Skills python/lists -.-> lab-271570{{"`Organizing Palace Inventory with Python Lists`"}} end

Understanding List Basics

In this step, you will start by creating a Python list to represent the items stored in the palace. You will learn how to add and remove items from the list and access specific elements.

First, let's open a Python script file named list_basics.py at the path ~/project. Inside the script file, write the following code:

## list_basics.py

## Create a list of palace items
palace_inventory = ["gold statue", "jeweled crown", "antique vase"]

## Add a new item to the list
palace_inventory.append("precious gems")

## Remove an item from the list
palace_inventory.remove("jeweled crown")

## Access the first item in the list
first_item = palace_inventory[0]
print(first_item)

Run the script:

python list_basics.py

The information below should be displayed on your terminal:

gold statue

List Manipulation and Slicing

In this step, you will learn advanced list manipulation techniques, such as slicing and updating list elements.

Open a new file named list_manipulation.py at the path ~/project and add the following code:

## list_manipulation.py

## Create a list of numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

## Slice the list to get a subset
subset = numbers[2:7]
print(subset)

## Update specific elements in the list
numbers[5] = 100
print(numbers)

Run the script:

python list_manipulation.py

The information below should be displayed on your terminal:

[3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 100, 7, 8, 9, 10]

Summary

In this lab, we designed a scenario in an ancient Egyptian palace to introduce the concept of Python lists. You acted as a guardian responsible for managing the palace's inventory, providing a practical and engaging context for learning. By completing the lab, you gained essential skills in creating, manipulating, and accessing Python lists, which are fundamental for any aspiring Python programmer.

Other Python Tutorials you may like