Assignment 3 Syandilya Sai Vardhan
Assignment 3 Syandilya Sai Vardhan
21BCE9037
Assignment-3
List
In [1]: #List Manipulation:
n=int(input("Enter the length "))
c=0
l=[];
while(c<n):
a=int(input("Enter a number "))
l.append(a)
c=c+1
newlist=[]
for i in range(len(l)):
newlist.append(l[i]**2)
print("Original list ",l)
print("Squared List ",newlist)
Tuple
In [6]: input_tuple = eval(input("Enter a tuple "))
print("Unpacked variables:")
print("Variable 1:", var1)
print("Variable 2:", var2)
print("Variable 3:", var3)
In [7]: input_str1 = input("Enter elements of the first tuple separated by spaces: ")
tuple1 = tuple(input_str1.split())
numbers = tuple(input_str.split())
sorted_numbers = tuple(sorted(numbers))
numbers = tuple(input_str.split())
sorted_numbers = tuple(sorted(numbers))
length = len(sorted_numbers)
print("Length of the tuple:", length)
numbers = tuple(input_str.split())
my_tuple = tuple(sorted(numbers))
my_list = list(my_tuple)
print("Converted tuple to list:", my_list)
my_list.append(6)
my_list[0] = 0
print("Modified list:", my_list)
new_tuple = tuple(my_list)
print("Converted list back to tuple:", new_tuple)
Dictionary
In [2]: input_str = input("Enter students and their grades in the format 'name:grade' separ
student_grades_list = input_str.split(',')
students = {}
if operation.lower() == 'add':
name = input("Enter the name of the new student: ")
grade = int(input("Enter the grade of the new student: "))
students[name] = grade
print(f"Student '{name}' with grade {grade} added successfully.")
elif operation.lower() == 'remove':
name = input("Enter the name of the student to remove: ")
if name in students:
del students[name]
print(f"Student '{name}' removed successfully.")
else:
print(f"Student '{name}' not found in the dictionary.")
else:
print("Invalid operation. Please choose 'add' or 'remove'.")
Enter students and their grades in the format 'name:grade' separated by commas: sa
i:85, vardhan:55, Aswatha:89, Brinda:90
Initial Dictionary of Students:
{'sai': 85, 'vardhan': 55, 'Aswatha': 89, 'Brinda': 90}
Do you want to add or remove a student? (add/remove): remove
Enter the name of the student to remove: sai
Student 'sai' removed successfully.
Updated Dictionary of Students:
{'vardhan': 55, 'Aswatha': 89, 'Brinda': 90}
In [5]: students = {
'sai': {
'age': 20,
'courses': ['Math', 'Physics', 'English']
},
'Krithik': {
'age': 20,
'courses': ['Economics', 'Commerce', 'Political Science']
},
'Rammya': {
'age': 19,
'courses': ['CSE', 'Pshycology', 'Chemistry']
}
}
Student: Krithik
Age: 20
Courses:
- Economics
- Commerce
- Political Science
Student: Rammya
Age: 19
Courses:
- CSE
- Pshycology
- Chemistry
In [7]: my_dict = {'a': 10, 'b': 20, 'c': 30, 'd': 40, 'e': 50}
filtered_dict = {key: value for key, value in my_dict.items() if value >= threshold
print("Filtered Dictionary:")
print(filtered_dict)
Sets
In [9]: set1 = {0,2,1,8,4,6,3,2,1,5}
set2 = {8,5,2,1,4,7,9,6,3,10}
unset = set1.union(set2)
inset = set1.intersection(set2)
diset1 = set1.difference(set2)
diset2 = set2.difference(set1)
print("Set1:", set1)
print("Set2:", set2)
print("Union:", unset)
print("Intersection:", inset)
print("Difference of Set1 - Set2:", diset1)
print("Difference of Set2 - Set1:", diset2)
if element in my_set:
print(f"The element {element} is present in the set.")
else:
print(f"The element {element} is not present in the set.")
symmetric_difference_set = set1.symmetric_difference(set2)
set1.update(set2)
In [13]: start = 1
end = 10
Set of squares from 1 to 10 : {64, 1, 4, 36, 100, 9, 16, 49, 81, 25}
In [ ]: