0% found this document useful (0 votes)
9 views2 pages

List 2

The document contains 12 programming assignments involving operations on lists such as calculating sum and maximum/minimum, finding second largest element, reversing list, checking presence and counting occurrences, removing duplicates, creating new lists with cubes/duplicates, separating even and odd elements, removing occurrences of an element, finding numbers divisible by two numbers, and creating lists of original, squares and cubes of elements.
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)
9 views2 pages

List 2

The document contains 12 programming assignments involving operations on lists such as calculating sum and maximum/minimum, finding second largest element, reversing list, checking presence and counting occurrences, removing duplicates, creating new lists with cubes/duplicates, separating even and odd elements, removing occurrences of an element, finding numbers divisible by two numbers, and creating lists of original, squares and cubes of elements.
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/ 2

Piyush Deshmukh

keyboard_arrow_down Assignment List 1


1 numbers=[1,2,3,4,5,6,13,14,7,8,9,10,12,15,16,17,18]

1 s=(0)
2 for i in numbers:
3 s+=i
4 print(s)

160

1 maxim = numbers[0]
2 minim = numbers[0]
3 for i in numbers:
4 if i > maxim:
5 maxim = i
6 if i < minim:
7 minim = i
8 print("Maximum element: ", maxim)
9 print("Minimum element: ", minim)

Maximum element: 18
Minimum element: 1

keyboard_arrow_down 3. Second largest element in the list


1 sorted= sorted(numbers)
2 second_largest = sorted[-2]
3 print("Second largest element:", second_largest)

Second largest element: 17

keyboard_arrow_down 4. Reverse the list


1 reversed = []
2 for i in range(len(numbers) - 1, -1, -1):
3 reversed.append(numbers[i])
4 print("Reversed list:", reversed)

Reversed list: [18, 17, 16, 15, 12, 10, 9, 8, 7, 14, 13, 6, 5, 4, 3, 2, 1]

keyboard_arrow_down 5. Check if an element is present and count occurrences


1 user = int(input("Enter a number to check in the list: "))
2 occur = 0
3 for num in numbers:
4 if num == user:
5 occur += 1
6 print(f"{user} is present in the list {occur} times.")

Enter a number to check in the list: 5


5 is present in the list 1 times.

keyboard_arrow_down 6. Remove duplicates from the list


1 unique_list = []
2 for num in numbers:
3 if num not in unique_list:
4 unique_list.append(num)
5 print("List after removing duplicates:", unique_list)

List after removing duplicates: [1, 2, 3, 4, 5, 6, 13, 14, 7, 8, 9, 10, 12, 15, 16, 17, 18]

keyboard_arrow_down 7. Create a new list with cube of each number


1 cubed = [num ** 3 for num in numbers]
2 print("List with cubes:", cubed)

List with cubes: [1, 8, 27, 64, 125, 216, 2197, 2744, 343, 512, 729, 1000, 1728, 3375, 4096, 4913, 5832]

keyboard_arrow_down 8. Create a duplicate of the list


1 duplicate = numbers[:]
2 print("Duplicate list:", duplicate)

Duplicate list: [1, 2, 3, 4, 5, 6, 13, 14, 7, 8, 9, 10, 12, 15, 16, 17, 18]
keyboard_arrow_down 9. Separate even and odd elements into two lists
1 even = [num for num in numbers if num % 2 == 0]
2 odd = [num for num in numbers if num % 2 != 0]
3 print("Even elements:", even)
4 print("Odd elements:", odd)

Even elements: [2, 4, 6, 14, 8, 10, 12, 16, 18]


Odd elements: [1, 3, 5, 13, 7, 9, 15, 17]

keyboard_arrow_down 10. Remove all occurrences of a given element


1 element = int(input("Enter the element to remove: "))
2 numbers = [num for num in numbers if num != element]
3 print("List after removing occurrences:", numbers)

Enter the element to remove: 6


List after removing occurrences: [1, 2, 3, 4, 5, 13, 14, 7, 8, 9, 10, 12, 15, 16, 17, 18]

keyboard_arrow_down 11. Print numbers divisible by m and n


1 m = 2
2 n = 3
3 divisible = [num for num in numbers if num % m == 0 and num % n == 0]
4 print(f"Numbers divisible by {m} and {n}:", divisible)

Numbers divisible by 2 and 3: [12, 18]

keyboard_arrow_down 12. Create three lists of numbers, their squares, and cubes
1 squares = [num ** 2 for num in numbers]
2 cubes = [num ** 3 for num in numbers]
3 print("Original list:", numbers)
4 print("Squares:", squares)
5 print("Cubes:", cubes)

Original list: [1, 2, 3, 4, 5, 13, 14, 7, 8, 9, 10, 12, 15, 16, 17, 18]
Squares: [1, 4, 9, 16, 25, 169, 196, 49, 64, 81, 100, 144, 225, 256, 289, 324]
Cubes: [1, 8, 27, 64, 125, 2197, 2744, 343, 512, 729, 1000, 1728, 3375, 4096, 4913, 5832]

You might also like