Practice and reinforce the concepts from Lesson 12
:information_source: Info :computer: Activity Type: Coding Exercise
⏱️ Estimated Time: 30-40 minutes
:emoji: Work Type: Individual
from prettytable import PrettyTable
:bulb: Make sure to run the import statement first before attempting any exercises!
Question One: Creating a Sports Day Medal Table
⏱️ Time: 15-20 minutes
Let's create a table showing medal counts for each house in Telebort Sports Day.
Target Table:
Houses Gold Silver Bronze Total Gryffindor 5 4 5 14 Ravenclaw 7 5 3 15 Hufflepuff 6 5 4 15 Slytherin 2 6 8 16 Step One: Create the table
- Create a new table named
medals
usingPrettyTable()
- Print the empty table to verify it's created
python
medals = PrettyTable() print(medals)
Step 2: Add column headers
- Use the
field_names
property to add column headers- Copy and use this list for the field names:
python
medals.field_names = ["Houses", "Gold", "Silver", "Bronze", "Total"] print(medals)
Expected output:
text+-----------+------+--------+--------+-------+ | Houses | Gold | Silver | Bronze | Total | +-----------+------+--------+--------+-------+ +-----------+------+--------+--------+-------+
tip The table now shows headers but no data rows yet!
add_row()
method to add each house's medal datamedals.add_row(["Gryffindor", 5, 4, 5, 14])
medals.add_row(["Ravenclaw", 7, 5, 3, 15])
medals.add_row(["Hufflepuff", 6, 5, 4, 15])
medals.add_row(["Slytherin", 2, 6, 8, 16])
print(medals)
Expected output:
+-----------+------+--------+--------+-------+
| Houses | Gold | Silver | Bronze | Total |
+-----------+------+--------+--------+-------+
| Gryffindor| 5 | 4 | 5 | 14 |
| Ravenclaw | 7 | 5 | 3 | 15 |
| Hufflepuff| 6 | 5 | 4 | 15 |
| Slytherin | 2 | 6 | 8 | 16 |
+-----------+------+--------+--------+-------+
title
property to add a heading to your tablemedals.title = "Telebort Sports Day: Number of Medals For Each House"
print(medals)
Expected output:
+------------------------------------------------------+
| Telebort Sports Day: Number of Medals For Each House |
+---------------------+------+--------+--------+-------+
| Houses | Gold | Silver | Bronze | Total |
+---------------------+------+--------+--------+-------+
| Gryffindor | 5 | 4 | 5 | 14 |
| Ravenclaw | 7 | 5 | 3 | 15 |
| Hufflepuff | 6 | 5 | 4 | 15 |
| Slytherin | 2 | 6 | 8 | 16 |
+---------------------+------+--------+--------+-------+
:bulb: Notice how the title appears centered at the top of the table!
Question 2: Creating a Student Count Table
⏱️ Time: 15-20 minutes
In this exercise, you'll learn an alternative way to build tables using the
add_column()
method.Target Table:
sql
Number of Students in Each Classes +----------+--------+--------+-------+ | Classes | Male | Female | Total | +----------+--------+--------+-------+ | Class 1 | 9 | 12 | 21 | | Class 2 | 10 | 6 | 16 | | Class 3 | 8 | 15 | 23 | +----------+--------+--------+-------+
Step One: Create the table
- Create a new table named
students
- Print the empty table
python
students = PrettyTable() print(students)
Step 2: Add columns with data
- Use
add_column(field_name, data)
to add each column- Add columns in order using this data:
python
# Column 1: Classes students.add_column("Classes", ["Class 1", "Class 2", "Class 3"]) # Column 2: Male students students.add_column("Male", [9, 10, 8]) # Column 3: Female students students.add_column("Female", [12, 6, 15]) # Column 4: Total students students.add_column("Total", [21, 16, 23]) print(students)
Expected output:
text+---------+------+--------+-------+ | Classes | Male | Female | Total | +---------+------+--------+-------+ | Class 1 | 9 | 12 | 21 | | Class 2 | 10 | 6 | 16 | | Class 3 | 8 | 15 | 23 | +---------+------+--------+-------+
tip With
add_column()
, you add all data for one column at once, unlikeadd_row()
where you add one row at a time!
students.title = "Number of Students in Each Class"
print(students)
Expected output:
+---------------------------------+
| Number of Students in Each Class|
+---------+------+--------+-------+
| Classes | Male | Female | Total |
+---------+------+--------+-------+
| Class 1 | 9 | 12 | 21 |
| Class 2 | 10 | 6 | 16 |
| Class 3 | 8 | 15 | 23 |
+---------+------+--------+-------+
!pip install prettytable
print()
to display the table:warning: Warning Important: Before submitting, ensure you have:
- :white_check_mark: Completed both Question 1 and Question 2
- :white_check_mark: Tested that your tables display correctly
- :white_check_mark: Saved your Colab notebook
:link: Submit your exercise here
:information_source: Info :bulb: Submission Tip: Take screenshots of your output tables to include with your submission if required!