0% found this document useful (0 votes)
36 views

Python Assignment 5 SUK BCA

Uploaded by

Dipali Gaikwad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Python Assignment 5 SUK BCA

Uploaded by

Dipali Gaikwad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

List Operations
Question: Write a Python program to perform basic list
operations like adding, removing, and sorting elements.

numbers = [5, 3, 8, 6, 7]
numbers.append(10)
print("After appending 10:", numbers)

numbers.remove(3)
print("After removing 3:", numbers)
numbers.sort()
print("After sorting:", numbers)
numbers.reverse()
print("After reversing:", numbers)
Expected Output:

After appending 10: [5, 3, 8, 6, 7, 10]


After removing 3: [5, 8, 6, 7, 10]
After sorting: [5, 6, 7, 8, 10]
After reversing: [10, 8, 7, 6, 5]

2. Tuple Operations
Question: Write a Python program to demonstrate tuple
creation, indexing, and slicing.
my_tuple = ('apple', 'banana', 'cherry', 'date')
print("Element at index 1:", my_tuple[1])
print("Elements from index 1 to 3:", my_tuple[1:3])
print("Length of tuple:", len(my_tuple))
new_tuple = my_tuple + ('elderberry', 'fig')
print("Concatenated tuple:", new_tuple)
Expected Output:

Element at index 1: banana


Elements from index 1 to 3: ('banana', 'cherry')
Length of tuple: 4
Concatenated tuple: ('apple', 'banana', 'cherry', 'date',
'elderberry', 'fig')

3. Dictionary Operations
Question: Write a Python program to create a dictionary
and demonstrate adding, updating, and removing
elements.

student_scores = {'John': 85, 'Emma': 92, 'Liam': 78}


student_scores['Sophia'] = 88
print("After adding Sophia:", student_scores)
student_scores['Liam'] = 82
print("After updating Liam's score:", student_scores)
del student_scores['Emma']
print("After removing Emma:", student_scores)
print("Score of John:", student_scores['John'])

Expected Output:

After adding Sophia: {'John': 85, 'Emma': 92, 'Liam': 78,


'Sophia': 88}
After updating Liam's score: {'John': 85, 'Emma': 92,
'Liam': 82, 'Sophia': 88}
After removing Emma: {'John': 85, 'Liam': 82, 'Sophia':
88}
Score of John: 85
4. List Comprehension
Question: Write a Python program to generate a list of
squares of numbers from 1 to 10 using list
comprehension.

squares = [x2 for x in range(1, 11)]


print("Squares of numbers from 1 to 10:", squares)

Expected Output:

Squares of numbers from 1 to 10: [1, 4, 9, 16, 25, 36, 49,


64, 81, 100]

5. Tuple Packing and Unpacking


Question: Write a Python program to demonstrate tuple
packing and unpacking
person = ('John', 25, 'Engineer')
name, age, profession = person
print(f"Name: {name}, Age: {age}, Profession:
{profession}")

Expected Output:

Name: John, Age: 25, Profession: Engineer

6. Dictionary Methods
Question: Write a Python program to demonstrate various
dictionary methods.
person = {'name': 'Alice', 'age': 30, 'profession': 'Doctor'}
print("Name:", person.get('name'))
print("Keys:", person.keys())
print("Values:", person.values())
print("Items:", person.items())
Expected Output:

Name: Alice
Keys: dict_keys(['name', 'age', 'profession'])
Values: dict_values(['Alice', 30, 'Doctor'])
Items: dict_items([('name', 'Alice'), ('age', 30),
('profession', 'Doctor')])

7. Sorting a List of Tuples


Question: Write a Python program to sort a list of tuples
by the second element.
students = [('John', 85), ('Emma', 92), ('Liam', 78),
('Sophia', 88)]

sorted_students = sorted(students, key=lambda x: x[1])


print("Sorted by score:", sorted_students)
Expected Output:

Sorted by score: [('Liam', 78), ('John', 85), ('Sophia', 88),


('Emma', 92)]

8. Dictionary with List Values


Question: Write a Python program to create a dictionary
with lists as values.

subjects_scores = {
'Math': [85, 90, 80],
'Science': [78, 88, 92],
'English': [88, 84, 91]
}

print("Math scores:", subjects_scores['Math'])


subjects_scores['History'] = [75, 82, 88]
print("Updated dictionary:", subjects_scores)
Expected Output:

Math scores: [85, 90, 80]


Updated dictionary: {'Math': [85, 90, 80], 'Science': [78,
88, 92], 'English': [88, 84, 91], 'History': [75, 82, 88]}

9. Nested Dictionary
Question: Write a Python program to create a nested
dictionary and access its elements.

students = {
'John': {'age': 20, 'grade': 'A'},
'Emma': {'age': 22, 'grade': 'B'},
'Liam': {'age': 21, 'grade': 'A'}
}
print("John's age:", students['John']['age'])
print("Emma's grade:", students['Emma']['grade'])
students['Sophia'] = {'age': 23, 'grade': 'A'}
print("Updated dictionary:", students)

Expected Output:
John's age: 20
Emma's grade: B
Updated dictionary: {'John': {'age': 20, 'grade': 'A'},
'Emma': {'age': 22, 'grade': 'B'}, 'Liam': {'age': 21, 'grade':
'A'}, 'Sophia': {'age': 23, 'grade': 'A'}}

You might also like