#8- Case Study | Managing Student Data with Dictionaries in Python

NUR ARIF
2 min readJan 8, 2023

As an example, let’s create a simple program that will manage student data at a school. We will create a dictionary that will store student data, where the student’s name will be the key (key) of each data.

First, let’s create an empty dictionary that will hold student data:

students = {}

Then, let’s create a function called add_student that will be used to add student data to the dictionary:

def add_student(name, age, class_, address):
students[name] = {'age': age, 'class': class_, 'address': address}

This function will accept 4 parameters: name, age, class_, and address. Then, we will add the student data to the students dictionary, where name will be the key of the student data.

Now, let’s try using the add_student function to add some student data:

add_student('Budi', 16, 'XI IPA 2', 'Jakarta')
add_student('Ani', 17, 'XI IPS 1', 'Bogor')
add_student('Caca', 15, 'X IPA 1', 'Depok')

After running the above commands, the students dictionary will be filled with student data as follows:

{
'Budi': {'age': 16, 'class': 'XI IPA 2', 'address': 'Jakarta'},
'Ani': {'age': 17, 'class': 'XI IPS 1', 'address': 'Bogor'},
'Caca': {'age': 15, 'class': 'X IPA 1', 'address': 'Depok'}
}

Next, let’s create a function called view_student that will be used to display student data:

def view_student(name):
data = students[name]
print(f'Name: {name}')
print(f'Age: {data["age"]}')
print(f'Class: {data["class"]}')
print(f'Address: {data["address"]}')

This function will accept 1 parameter, name. Then, we will retrieve the student data with the specified name from the students dictionary using the name key, and display it to the screen.

Now, let’s try running the view_student function to display student data for Budi:

view_student('Budi')

The expected result is:

Name: Budi
Age: 16
Class: XI IPA 2
Address: Jakarta

In addition, we can create a function called edit_student that will be used to change student data:

def edit_student(name, age=None, class_=None, address=None):
if age is not None:
students[name]['age'] = age
if class_ is not None:
students[name]['class'] = class_
if address is not None:
students[name]['address'] = address

This function will accept 4 parameters: name, age, class_, and address. Then, we will check whether these parameters are None or not. If not, we will change the value of the student data.

Now, let’s try running the edit_student function to change student data for Budi:

edit_student('Budi', age=17, class_='XI IPA 1')

After running the above command, the student data for Budi will be changed to:

{'name': 'Budi', 'age': 17, 'class': 'XI IPA 1', 'address': 'Jakarta'}

And if we run the view_student function again to display student data for Budi, the expected result is:

Name: Budi
Age: 17
Class: XI IPA 1
Address: Jakarta

That is a simple case study example of managing student data using a dictionary in Python. I hope it helps! Let me know if you have any other questions.

--

--

NUR ARIF

Backend | Data Scraping | Content Writer | Python Programming | Passionate Cyber Security