0% found this document useful (0 votes)
24 views8 pages

Python Assignment1

The document contains a series of Python code snippets that demonstrate various list operations, including appending, removing, sorting, and manipulating elements. Each question illustrates different functionalities such as counting occurrences, merging lists, and transforming data structures. The output of each code snippet is also provided, showcasing the results of the operations performed.

Uploaded by

jific26590
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)
24 views8 pages

Python Assignment1

The document contains a series of Python code snippets that demonstrate various list operations, including appending, removing, sorting, and manipulating elements. Each question illustrates different functionalities such as counting occurrences, merging lists, and transforming data structures. The output of each code snippet is also provided, showcasing the results of the operations performed.

Uploaded by

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

#question 1

list1=[]
for i in range(10):
num = int(input("Enter number : "))
list1.append(num)
print(list1)
for i in list1:
if (i%3)==0:
list1.remove(i)
print(list1)
list.clear()
print(list1)

Enter number : 1
Enter number : 2
Enter number : 3
Enter number : 4
Enter number : 56
Enter number : 7
Enter number : 8
Enter number : 9
Enter number : 0
Enter number : 10

[1, 2, 3, 4, 56, 7, 8, 9, 0, 10]


[1, 2, 4, 56, 7, 8, 0, 10]
[1, 2, 4, 56, 7, 8, 0, 10]

# Question 2
list1=[2,3,5,7,11,13,2,3,5]
c=list1.count(2)
print(c)
list2=list1.copy()
print(list2)
list.clear()
print(list1)
print(list2)

2
[2, 3, 5, 7, 11, 13, 2, 3, 5]
[2, 3, 5, 7, 11, 13, 2, 3, 5]
[2, 3, 5, 7, 11, 13, 2, 3, 5]

# Question 3
list1=['apple', 'banana', 'cherry']
list2=['orange', 'grape']
list1.extend(list2)
print(list1)
l=list1.index('grape')
print(l)
['apple', 'banana', 'cherry', 'orange', 'grape']
4

# Question 4
list1=[10, 20, 30, 40, 50]
list1.insert(3,25)
print(list1)

l=list1.pop()
print(l)
print(list1)

[10, 20, 30, 25, 40, 50]


50
[10, 20, 30, 25, 40]

# Question 5
list1=[12, 7, 19, 4, 15, 2]
list1.sort()
print(list1)
list1.reverse()
print(list1)

[2, 4, 7, 12, 15, 19]


[19, 15, 12, 7, 4, 2]

# Question 6
import random

numbers = []
for i in range(5):
numbers.append(random.randint(1, 50))

print("Original List:", numbers)

print("\nFrequency of each unique number:")


for num in set(numbers):
print(f"{num} occurs {numbers.count(num)} time(s)")

max_num = max(numbers)
max_index = numbers.index(max_num)
print(f"\nMaximum number is {max_num} at index {max_index}")

min_num = min(numbers)
numbers.remove(min_num)
print(f"\nList after removing the smallest number ({min_num}):",
numbers)
copied_list = numbers.copy()
print("\nModified List:", numbers)
print("Copied List: ", copied_list)

Original List: [19, 37, 36, 19, 29]

Frequency of each unique number:


29 occurs 1 time(s)
19 occurs 2 time(s)
36 occurs 1 time(s)
37 occurs 1 time(s)

Maximum number is 37 at index 1

List after removing the smallest number (19): [37, 36, 19, 29]

Modified List: [37, 36, 19, 29]


Copied List: [37, 36, 19, 29]

# Question 7
list1= ["John", "Antony", "Bob", "Antony", "David"]
l=list1.count("Alice")
print(l)
list1.remove("Antony")
print(list1)
list1.insert(1,"Antony")
print(list1)

0
['John', 'Bob', 'Antony', 'David']
['John', 'Antony', 'Bob', 'Antony', 'David']

# Question 8
matrix = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
print("Original Matrix:", matrix)
trans = [[row[i] for row in matrix] for i in range(len(matrix[0]))]
print("\nTranspose of Matrix:", trans)
flat = []
for row in trans:
flat.extend(row)
print("\nFlattened List:", flat)
flat.sort(reverse=True)
print("\nSorted List in Descending Order:", flat)

Original Matrix: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Transpose of Matrix: [[1, 4, 7], [2, 5, 8], [3, 6, 9]]

Flattened List: [1, 4, 7, 2, 5, 8, 3, 6, 9]


Sorted List in Descending Order: [9, 8, 7, 6, 5, 4, 3, 2, 1]

# Question 9
list1=[9, 1, 8, 2, 7, 3, 6, 4, 5]
list1.sort()
print(list1)
list1.reverse()
print(list1)
l=list1.pop(4)
print(l)
print(list1)
list1.insert(0,l)
print(list1)

[1, 2, 3, 4, 5, 6, 7, 8, 9]
[9, 8, 7, 6, 5, 4, 3, 2, 1]
5
[9, 8, 7, 6, 4, 3, 2, 1]
[5, 9, 8, 7, 6, 4, 3, 2, 1]

# Question 10
list=["Eam","Shyam","Ravi","Mohan","Aman"]
list.sort()
print(list)
list.append("Saket")
list.append("Sumeet")
print(list)
list.pop(2)
print(list)
d=list.index("Sumeet")
print(d)
list.clear()
print(list)

['Aman', 'Eam', 'Mohan', 'Ravi', 'Shyam']


['Aman', 'Eam', 'Mohan', 'Ravi', 'Shyam', 'Saket', 'Sumeet']
['Aman', 'Eam', 'Ravi', 'Shyam', 'Saket', 'Sumeet']
5
[]

# Question 11
list1= ['abc', 123, 'ab12', '456', 'xyz', '78xy']
for i in list1:
if type(i)==str or type(i)==int:
if i.isalpha():
list1.remove(i)

print(list1)

[123, 'ab12', '456', '78xy']


# Question 12
list2 = ['10', 'apple', '20', 'banana', '30','grape']
for i in list2:
if type(i)==int:
list2.remove(i)
print(list2)

['10', 'apple', '20', 'banana', '30', 'grape']

# Question 13
list3 = [10, 'text', 25.5, 30, 'hello', 45]
list=[]
for i in list3:
if type(i)==int:
list.append(i)
print(list)

[10, 30, 45]

# Question 14
list4 = [1, 2, 3, 4, 2, 5, 3, 6, 7, 1]
list=[]
for i in list4:
if i not in list:
list.append(i)
print(list)

[1, 2, 3, 4, 5, 6, 7]

# Question 15
list5 = ['apple', 'dog', 'elephant', 'cat', 'giraffe']
for i in list5:
if len(i)<3 or len(i)==3:
list5.remove(i)
print(list5)

['apple', 'elephant', 'giraffe']

# Question 16
list6 = ['Python', 100, 'JavaScript', 3.14, 'Java', 42]
list=[]
for i in list6:
if type(i)==str:
list.append(i)
print(list)

['Python', 'JavaScript', 'Java']

# Question 17
list7 = [10, 5, 23, -42, 17, -8, 7]
for i in list7:
if i<0:
list7.remove(i)
print(list7)

[10, 5, 23, 17, 7]

# Question 18
list8 = ['Monday', 'Tuesday', 'Holiday', 'Wednesday', 'Holiday']
for i in range(len(list8)):
if list8[i] == "Holiday":
list8[i] = "Thursday"

print("Updated List:", list8)

Updated List: ['Monday', 'Tuesday', 'Thursday', 'Wednesday',


'Thursday']

# Question 19
list9 = ['basketball', 'baseball', 'cricket', 'ball', 'football']
new=[]
for i in list9:
if "ball" not in i:
new.append(i)
print(new)

['cricket']

# Question 20
list11 = [1, 2, 3]
list12 = [4, 5, 6]
newlist=(set(list11+list12))
print(newlist)

{1, 2, 3, 4, 5, 6}

# Question 21
list13 = [10, 20, 30, 40, 50, 60]
for i in list13:
if (i%2)==0:
list13.remove(i)
print(list13)

[20, 40, 60]

# Question 22
list14 = ['apple', 'banana', 'cherry', 'apple', 'cherry', 'apple']
d=list14.count("apple")
while "apple" in list14:
list14.remove("apple")
print(d)
print(list14)
3
['banana', 'cherry', 'cherry']

# Question 23
list16 = ['a', 'b', 'c']
list17 = [1, 2, 3]
merge = []
for i in range(len(list16)):
merge.append(list16[i])
merge.append(list17[i])
print("Merged List:", merge)

Merged List: ['a', 1, 'b', 2, 'c', 3]

# Question 24
list=[15,42,33,57,21,39]
d=max(list)
list.remove(d)
print(list)

[15, 42, 33, 21, 39]

# Question 25
list19 = ['banana', 'apple', 'grape', 'cherry']
list19.sort(key=len, reverse=True)
print(list19)

['banana', 'cherry', 'apple', 'grape']

# Question 26
list20 = ['python', 'java', 'c++', 'ruby']
for i in range(len(list20)):
list20[i] = list20[i][::-1]
print(list20)

['nohtyp', 'avaj', '++c', 'ybur']

# Question 27
list = ['one two', 'three four', 'five six']
final = []
for i in list:
final.extend(i.split())
print(final)

['one', 'two', 'three', 'four', 'five', 'six']

# Question 28
list22 = ['apple', 'orange', 'banana']
vowels = "aeiouAEIOU"
for i in range(len(list22)):
for v in vowels:
list22[i] = list22[i].replace(v, "")
print(list22)

['ppl', 'rng', 'bnn']

You might also like