Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Wap to print sum of list
- l1 = [12+13+14+15+56]
- sum = 0
- for i in l1:
- sum += i
- print(sum
- #output
- 110
- #Wap to print mal element
- ls=[]
- print("Enter Elements in array")
- for i in range(5):
- el=int(input())
- ls.append(el)
- maxel=ls[0]
- mi=0
- for i in range(1,len(ls)):
- if ls[i]>maxel:
- maxel=ls[i]
- mi = i
- print("Max element is: ",maxel)
- #output
- Enter Elements in array
- 13
- 67
- 34
- 99
- 12
- Max element is: 99
- #wap to print common elements
- def compare_lists(list1, list2):
- common_elements = set(list1) & set(list2)
- return list(common_elements)
- list1_input = input("Enter elements of the first list separated by spaces: ")
- list1 = list(map(str, list1_input.split()))
- list2_input = input("Enter elements of the second list separated by spaces: ")
- list2 = list(map(str, list2_input.split()))
- common_elements = compare_lists(list1, list2)
- if common_elements:
- print("Common elements: ", common_elements)
- else:
- print("No common elements found.")
- #output
- Enter elements of the first list separated by spaces: 12 13 45 67 89
- Enter elements of the second list separated by spaces: 12 45 67 89
- Common elements: ['12', '45', '67', '89']
- #Wap to remove even numbers
- def remove_even_numbers(input_list):
- return [num for num in input_list if int(num) % 2 != 0]
- user_input = input("Enter a list of numbers separated by spaces: ")
- numbers_list = user_input.split()
- filtered_list = remove_even_numbers(numbers_list)
- print("List after removing even numbers:", filtered_list)
- #output
- Enter a list of numbers separated by spaces: 12 34 56 77 43 31
- List after removing even numbers: ['77', '43', '31']
- #Wap to print number of occurance
- def count_occurrences(array, target_element):
- return array.count(target_element)
- user_input = input("Enter a list of numbers separated by spaces: ")
- numbers_list = user_input.split()
- target_element = input("Enter the element to count: ")
- occurrences = count_occurrences(numbers_list, target_element)
- print(f"The number of occurrences of {target_element} in the list is: {occurrences}")
- #output
- Enter a list of numbers separated by spaces: 12 33 33 45 67 54
- Enter the element to count: 33
- The number of occurrences of 33 in the list is: 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement