Course Overview | Previous Session | Review
Introduction
After successfully using Python to manage Sam’s care, I got a wonderful opportunity to help the shelter where I adopted him. They were so impressed with the software I developed that they offered me a job as a junior programmer! My first task is to create a system to help the shelter manage and name all the new pets that come in. This will help make their daily operations smoother and more efficient.
A Pet Shelter’s Need
The shelter is always busy with new animals arriving. My job is to create a software solution that helps the staff quickly and efficiently name these new pets. We can do this by using Python to manage a list of potential pet names.
Introducing Arrays
In Python, instead of creating a separate variable for each pet name, we can store all the names in one variable called a list (or array). This allows us to easily manage, add, and remove names.
Creating and Using an Array
Let’s start by creating a list of pet names in a new Python file. Follow these steps:
- Open your text editor (Gnome Text, Kate, KWrite, or another editor) and create a new file named pet_names.py.
- Type the following code in the text editor:
pet_names = ["Buddy", "Max", "Luna", "Daisy", "Rocky", "Gracie", "Bella", "Charlie"]
- Save the file in your “code” directory.
Next, let’s create a second file to use this list:
- Create a new file in your text editor named
names.py
. - Type the following code:
import pet_names
print(pet_names.pet_names)
- Save the file.
- Open your terminal where both
pet_names.py
andnames.py
are located. - Run the code by typing
python3 names.py
and press ENTER. You should see a list of all the names in the array.
Getting a Random Name
To add some fun to the process, let’s randomly pick a pet name from the list. Create another new file:
- Create a new file in your text editor named
lucky_name.py
. - Type the following code:
import random
from pet_names import pet_names
pet_name = random.choice(pet_names)
print(pet_name)
- Save the file.
- Open your terminal and run the code by typing
python3 lucky_name.py
. You will see a randomly chosen pet name from your list.
Editing, Adding, and Deleting Names
As new pets arrive and some are adopted, the list of names will need to be updated. You’ll perform these changes directly in the pet_names.py
file.
- Open the
pet_names.py
file in your text editor. - To add a name to the list, use the
append()
function. For example, add the following line inside thepet_names.py
file:
pet_names.append(“Fido”)
Save the file after adding the line.
- To remove a name from the list, also modify the
pet_names.py
file. Use theremove()
function:
pet_names.remove(“Buddy”)
Save the file after removing the line.
Note: If the name isn’t in the list, you’ll get a ValueError
.
Practice Time!
Now it’s your turn. Open your pet_names.py
file and try adding, removing, and printing names to see how it works. Try different combinations to see how the list updates.
Study Tip
Arrays (or lists) can be tricky at first, so don’t worry if it takes a few days to get comfortable with them. Revisiting the material and practicing will help solidify your understanding.
Quiz
Keep Going!
Learning to program is a journey filled with new skills and exciting challenges. Keep practicing and don’t give up. You’re making great progress!