0% found this document useful (0 votes)
30 views7 pages

Assignment 3 Syandilya Sai Vardhan

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)
30 views7 pages

Assignment 3 Syandilya Sai Vardhan

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/ 7

2/28/24, 1:15 PM Assignment 3 Syandilya Sai Vardhan

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)

Enter the length 5


Enter a number 1
Enter a number 2
Enter a number 3
Enter a number 4
Enter a number 5
Original list [1, 2, 3, 4, 5]
Squared List [1, 4, 9, 16, 25]

In [2]: #List Filtering:


n=int(input("Enter the length "))
c=0
l=[];
while(c<n):
a=int(input("Enter a number "))
l.append(a)
c=c+1
oddlist=[i for i in l if i%2!=0]
print("Orginal list ",l)
print("Odd list ",oddlist)

Enter the length 3


Enter a number 4
Enter a number 5
Enter a number 7
Orginal list [4, 5, 7]
Odd list [5, 7]

In [3]: #List Sum:


n=int(input("Enter the length "))
c=0
l=[];
while(c<n):

localhost:8888/nbconvert/html/Assignment 3 Syandilya Sai Vardhan.ipynb?download=false 1/7


2/28/24, 1:15 PM Assignment 3 Syandilya Sai Vardhan
a=input("Enter ")
l.append(a)
c=c+1
sum=0
for i in range(len(l)):
if(l[i].isnumeric()):
sum=sum+int(l[i])
print("List ",l)
print("sum is ",sum)

Enter the length 4


Enter 4
Enter 7
Enter 8
Enter 9
List ['4', '7', '8', '9']
sum is 28

In [4]: #List reversal


n=int(input("Enter the length "))
c=0
l=[];
while(c<n):
a=int(input("Enter a number "))
l.append(a)
c=c+1
c=n-1;
revlist=[]
while(c>=0):
revlist.append(l[c]);
c=c-1
print("Orginal list ",l)
print("Reversal list ",revlist)

Enter the length 5


Enter a number 1
Enter a number 4
Enter a number 7
Enter a number 8
Enter a number 9
Orginal list [1, 4, 7, 8, 9]
Reversal list [9, 8, 7, 4, 1]

In [5]: #List Concatenation:


n1=int(input("Enter the length "))
c=0
l1=[];
while(c<n1):
a=int(input("Enter a number "))
l1.append(a)
c=c+1
n2=int(input("Enter the length "))
c=0
l2=[];
while(c<n2):
a=int(input("Enter a number "))
l2.append(a)
c=c+1
combinedlist=l1+l2
removedup=list(set(combinedlist))
print("Remove duplicates in a combined list ",removedup)

localhost:8888/nbconvert/html/Assignment 3 Syandilya Sai Vardhan.ipynb?download=false 2/7


2/28/24, 1:15 PM Assignment 3 Syandilya Sai Vardhan
Enter the length 6
Enter a number 1
Enter a number 4
Enter a number 7
Enter a number 8
Enter a number 9
Enter a number 6
Enter the length 5
Enter a number 1
Enter a number 4
Enter a number 2
Enter a number 3
Enter a number 6
Remove duplicates in a combined list [1, 2, 3, 4, 6, 7, 8, 9]

Tuple
In [6]: input_tuple = eval(input("Enter a tuple "))

var1, var2, var3 = input_tuple

print("Unpacked variables:")
print("Variable 1:", var1)
print("Variable 2:", var2)
print("Variable 3:", var3)

Enter a tuple 1,2,7


Unpacked variables:
Variable 1: 1
Variable 2: 2
Variable 3: 7

In [7]: input_str1 = input("Enter elements of the first tuple separated by spaces: ")
tuple1 = tuple(input_str1.split())

input_str2 = input("Enter elements of the second tuple separated by spaces: ")


tuple2 = tuple(input_str2.split())

concatenated_tuple = tuple1 + tuple2


print("Concatenated Tuple:", concatenated_tuple)

Enter elements of the first tuple separated by spaces: 1 2 3


Enter elements of the second tuple separated by spaces: 4 5 8 2
Concatenated Tuple: ('1', '2', '3', '4', '5', '8', '2')

In [8]: input_str = input("Enter numbers separated by spaces: ")

numbers = tuple(input_str.split())
sorted_numbers = tuple(sorted(numbers))

print("Sorted Tuple:", sorted_numbers)

Enter numbers separated by spaces: 9 7 5 3 1 5 8


Sorted Tuple: ('1', '3', '5', '5', '7', '8', '9')

In [12]: input_str = input("Enter numbers separated by spaces: ")

numbers = tuple(input_str.split())
sorted_numbers = tuple(sorted(numbers))

length = len(sorted_numbers)
print("Length of the tuple:", length)

localhost:8888/nbconvert/html/Assignment 3 Syandilya Sai Vardhan.ipynb?download=false 3/7


2/28/24, 1:15 PM Assignment 3 Syandilya Sai Vardhan
element = int(input("Enter an element to check its existence in the tuple: "))
if element in sorted_numbers:
print(f"The element {element} exists in the tuple.")
else:
print(f"The element {element} does not exist in the tuple.")

Enter numbers separated by spaces: 1 2 5 4 7 8


Length of the tuple: 6
Enter an element to check its existence in the tuple: 6
The element 6 does not exist in the tuple.

In [14]: input_str = input("Enter numbers separated by spaces: ")

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)

Enter numbers separated by spaces: 1 4 5 8 7 2 3


Converted tuple to list: ['1', '2', '3', '4', '5', '7', '8']
Modified list: [0, '2', '3', '4', '5', '7', '8', 6]
Converted list back to tuple: (0, '2', '3', '4', '5', '7', '8', 6)

Dictionary
In [2]: input_str = input("Enter students and their grades in the format 'name:grade' separ

student_grades_list = input_str.split(',')

students = {}

for pair in student_grades_list:


name, grade = pair.split(':')
students[name.strip()] = int(grade.strip())
print("Initial Dictionary of Students:")
print(students)

operation = input("Do you want to add or remove a student? (add/remove): ")

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'.")

localhost:8888/nbconvert/html/Assignment 3 Syandilya Sai Vardhan.ipynb?download=false 4/7


2/28/24, 1:15 PM Assignment 3 Syandilya Sai Vardhan
print("Updated Dictionary of Students:")
print(students)

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 [3]: items = {'apple': 2, 'banana': 3, 'orange': 1, 'grape': 4}

print("Keys and Values of the Dictionary:")


for key, value in items.items():
print(f"Key: {key}, Value: {value}")

Keys and Values of the Dictionary:


Key: apple, Value: 2
Key: banana, Value: 3
Key: orange, Value: 1
Key: grape, Value: 4

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']
}
}

for student, info in students.items():


print(f"Student: {student}")
print(f"Age: {info['age']}")
print("Courses:")
for course in info['courses']:
print(f"- {course}")
print()

localhost:8888/nbconvert/html/Assignment 3 Syandilya Sai Vardhan.ipynb?download=false 5/7


2/28/24, 1:15 PM Assignment 3 Syandilya Sai Vardhan
Student: sai
Age: 20
Courses:
- Math
- Physics
- English

Student: Krithik
Age: 20
Courses:
- Economics
- Commerce
- Political Science

Student: Rammya
Age: 19
Courses:
- CSE
- Pshycology
- Chemistry

In [6]: dict1 = {'a': 1, 'b': 2, 'c': 3}


dict2 = {'b': 3, 'c': 4, 'd': 5}

merged_dict = {**dict1, **dict2}

print("Merged Dictionary:", merged_dict)

Merged Dictionary: {'a': 1, 'b': 3, 'c': 4, 'd': 5}

In [7]: my_dict = {'a': 10, 'b': 20, 'c': 30, 'd': 40, 'e': 50}

threshold = int(input("Enter the threshold value: "))

filtered_dict = {key: value for key, value in my_dict.items() if value >= threshold

print("Filtered Dictionary:")
print(filtered_dict)

Enter the threshold value: 40


Filtered Dictionary:
{'d': 40, 'e': 50}

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)

localhost:8888/nbconvert/html/Assignment 3 Syandilya Sai Vardhan.ipynb?download=false 6/7


2/28/24, 1:15 PM Assignment 3 Syandilya Sai Vardhan
Set1: {0, 1, 2, 3, 4, 5, 6, 8}
Set2: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Union: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Intersection: {1, 2, 3, 4, 5, 6, 8}
Difference of Set1 - Set2: {0}
Difference of Set2 - Set1: {9, 10, 7}

In [10]: my_set = {1, 2, 3, 4, 5}

element = int(input("Enter the element to check: "))

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.")

Enter the element to check: 8


The element 8 is not present in the set.

In [11]: set1 = {1, 2, 3, 4, 5}


set2 = {4, 5, 6, 7, 8}

symmetric_difference_set = set1.symmetric_difference(set2)

print("Symmetric Difference:", symmetric_difference_set)

Symmetric Difference: {1, 2, 3, 6, 7, 8}

In [12]: set1 = {1, 2, 3}


set2 = {3, 4, 5}

set1.update(set2)

print("Updated set1 after merging with set2:", set1)

Updated set1 after merging with set2: {1, 2, 3, 4, 5}

In [13]: start = 1
end = 10

squares = {x ** 2 for x in range(start, end + 1)}


print("Set of squares from", start, "to", end, ":", squares)

Set of squares from 1 to 10 : {64, 1, 4, 36, 100, 9, 16, 49, 81, 25}

In [ ]:

localhost:8888/nbconvert/html/Assignment 3 Syandilya Sai Vardhan.ipynb?download=false 7/7

You might also like