EXPERIMENT No. 5
Python Lab Practical Record · Python programming laboratory
Read with diagrams, search and self-test
5A. List of student names – adding, removing and sorting
Aim
To create a Python program that stores names of students in a list and performs operations like adding, removing and sorting.
Program (list_students.py)
# Exp 5A: List of student names - adding, removing and sorting
students = ["Rahul", "Ravi", "Megha", "Gagan"]
print("Original list:", students)# Adding a student
students.append("Suman")
print("After adding Suman:", students)# Removing a student
students.remove("Ravi")
print("After removing Ravi:", students)# Sorting the list
students.sort()
print("After sorting:", students)Output
Original list: ['Rahul', 'Ravi', 'Megha', 'Gagan'] After adding Suman: ['Rahul', 'Ravi', 'Megha', 'Gagan', 'Suman'] After removing Ravi: ['Rahul', 'Megha', 'Gagan', 'Suman'] After sorting: ['Gagan', 'Megha', 'Rahul', 'Suman']
Result
A list of student names was created, and adding, removing and sorting operations were performed.
5B. Convert a list of temperatures from Celsius to Fahrenheit
Aim
To write a Python program that converts a list of temperatures from Celsius to Fahrenheit.
Program (celsius_list_to_fahrenheit.py)
# Exp 5B: Convert a list of temperatures from Celsius to Fahrenheit
celsius = [0, 10, 20, 30, 40]
fahrenheit = []
for temp in celsius:
f = (temp * 9 / 5) + 32
fahrenheit.append(f)
print("Celsius temperatures:", celsius)
print("Fahrenheit temperatures:", fahrenheit)Output
Celsius temperatures: [0, 10, 20, 30, 40] Fahrenheit temperatures: [32.0, 50.0, 68.0, 86.0, 104.0]
Result
A list of Celsius temperatures was converted to Fahrenheit using F = C × 9/5 + 32.
5C. Dictionary for storing a student record
Aim
To create a Python dictionary for storing student records (name, age and grade) and write functions to add and remove entries.
Program (student_dictionary.py)
# Exp 5C: Dictionary of student record with functions to add and remove
entries
student = {
"name": "Ravi",
"age": 18,
"grade": "A"
}def add_entry(d, key, value):
d[key] = valuedef remove_entry(d, key):
del d[key]print("Original record:", student)
add_entry(student, "branch", "Mechanical")
print("After adding branch:", student)
remove_entry(student, "age")
print("After removing age:", student)Output
Original record: {'name': 'Ravi', 'age': 18, 'grade': 'A'}
After adding branch: {'name': 'Ravi', 'age': 18, 'grade': 'A', 'branch':
'Mechanical'}
After removing age: {'name': 'Ravi', 'grade': 'A', 'branch': 'Mechanical'}Result
A student record was stored in a dictionary, and entries were added and removed using functions.
5D. Tuples to store and display coordinates in a 2D plane
Aim
To write a Python program that uses tuples to store and display coordinates in a 2D plane.
Program (tuple_coordinates.py)
# Exp 5D: Tuple to store and display coordinates in a 2D plane
point = (10, 20)
print("x coordinate =", point[0])
print("y coordinate =", point[1])Output
x coordinate = 10 y coordinate = 20
Result
The coordinates of a point were stored in a tuple and displayed.