Q1. Create a list in Python.
Also illustrate the use of negative
indexing of list with example.
A list in Python is a built-in data structure used to store multiple items in a single variable. Lists are
ordered, mutable (can be changed), and allow duplicate elements. They are written using square
brackets [ ]. Lists can store integers, strings, floats, or even a combination of data types. Features
of Lists: 1. Ordered collection of items. 2. Index based access (first element at index 0). 3. Allows
duplicates. 4. Mutable – elements can be added, deleted, or replaced. 5. Supports positive and
negative indexing. Negative Indexing: Normally elements are accessed using positive index
(starting from 0). Negative indexing starts from -1 (last element), -2 (second last) and so on.
numbers = [10, 20, 30, 40, 50]
print("First element:", numbers[0])
print("Second element:", numbers[1])
print("Last element:", numbers[-1])
print("Second last element:", numbers[-2])
print("Third last element:", numbers[-3])
First element: 10
Second element: 20
Last element: 50
Second last element: 40
Third last element: 30
Conclusion: Lists are powerful and flexible. Negative indexing simplifies access to elements from
the end of a list.
Q2. Perform the following operations on lists: (a) Replacing
elements (b) Inserting and Removing (c) Searching and Sorting
Python lists support many operations that allow modification and management of data easily. Some
of the most common operations are replacing elements, inserting or removing elements, and
searching or sorting elements. (a) Replacing: Any element can be replaced by directly assigning a
new value at a given index. (b) Inserting and Removing: New values can be inserted using insert()
and deleted using remove() or pop(). (c) Searching and Sorting: 'in' keyword is used for searching,
while sort() arranges elements.
lst = [1, 2, 3, 4]
# Replacing
lst[2] = 100 # replaces 3 with 100
# Inserting and Removing
lst.insert(1, 50) # insert 50 at index 1
lst.remove(100) # remove first occurrence of 100
# Searching and Sorting
print(50 in lst) # searching
lst.sort() # sort the list ascending
print(lst)
True
[1, 2, 4, 50]
Conclusion: Python provides simple list operations which make it efficient for handling ordered
data.
Q3. How dictionaries are different from lists?
Lists and dictionaries are two different but important Python data structures. List: - Ordered
collection of elements. - Accessed by numeric index starting from 0. - Allows duplicate values. -
Best suited for sequential data. Dictionary: - Collection of key–value pairs. - Accessed using keys
instead of numeric indexes. - Keys are unique, values may be duplicate. - Best suited for mapping
and structured data.
# List Example
lst = ["apple", "banana", "apple"]
print(lst[0]) # access by index
# Dictionary Example
d = {"name": "Amit", "age": 20}
print(d["name"]) # access by key
apple
Amit
Conclusion: Lists are ideal for sequences, whereas dictionaries are better for structured data with
mappings.
Q4. Perform the following operations on dictionaries: (a) Addition
and Removal of keys (b) Accessing and Replacing values (c)
Traversing dictionary
Dictionaries in Python allow powerful operations to manage key–value data. (a) Addition and
Removal of keys: New keys can be added by assignment and removed using del keyword. (b)
Accessing and Replacing values: Values are accessed by their keys, and can also be updated. (c)
Traversing Dictionary: Looping using items() method returns key–value pairs.
d = {"name": "Amit"}
# Addition and Removal
d["age"] = 20
del d["age"]
# Access and Replace
d["name"] = "Rahul"
# Traversal
for k, v in d.items():
print(k, v)
name Rahul
Conclusion: Dictionaries provide flexibility for storing and manipulating structured data in Python.
Q5. Write a program to input and display student information
(Name, Registration Number, Total Marks).
We can design a program to input and display information of n students such as name, registration
number and total marks. The best approach is to use a list of dictionaries, where each dictionary
stores one student's details.
n = int(input("Enter number of students: "))
students = []
for i in range(n):
name = input("Enter Name: ")
reg = input("Enter Reg No: ")
marks = int(input("Enter Total Marks: "))
students.append({"Name": name, "RegNo": reg, "Marks": marks})
print("Student Records:")
for s in students:
print(s)
Enter number of students: 2
Enter Name: Amit
Enter Reg No: 101
Enter Total Marks: 450
Enter Name: Rahul
Enter Reg No: 102
Enter Total Marks: 400
Student Records:
{'Name': 'Amit', 'RegNo': '101', 'Marks': 450}
{'Name': 'Rahul', 'RegNo': '102', 'Marks': 400}
Conclusion: A list of dictionaries is efficient to store details of multiple students together.
Q6. Write a program to check whether a given key is present in a
dictionary or not and also find sum of values.
We can check if a given key is present in a dictionary using the 'in' keyword. The sum of values can
be found using Python's built-in sum() function applied to dictionary values.
d = {'a': 10, 'b': 20, 'c': 30}
key = 'b'
# Check key
print("Key present?", key in d)
# Sum of values
print("Sum of values:", sum(d.values()))
Key present? True
Sum of values: 60
Conclusion: The 'in' operator is useful for key checking and sum() provides quick addition of
numeric values.
Q7. Write a program to count the frequency of characters in a
string using dictionary and display it in alphabetical order of keys.
Dictionaries can be used to count the frequency of characters in a string. Each character is taken as
a key and its count as the value. The dictionary can then be traversed in sorted order of keys to
display the result.
s = "banana"
d = {}
for ch in s:
d[ch] = d.get(ch, 0) + 1
for k in sorted(d):
print(k, ":", d[k])
a : 3
b : 1
n : 2
Conclusion: Counting with dictionaries simplifies frequency analysis problems and sorted()
arranges output alphabetically.