Advertisement
brandblox

python lab (05/03/2024)

Mar 5th, 2024
814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.28 KB | None | 0 0
  1. # Wap to print sum of list
  2. l1 = [12+13+14+15+56]
  3. sum = 0
  4.  
  5. for i  in l1:
  6.     sum += i
  7.  
  8. print(sum
  9.  
  10. #output
  11. 110
  12.  
  13. #Wap to print mal element
  14. ls=[]
  15. print("Enter Elements in array")
  16. for i in range(5):
  17.     el=int(input())
  18.     ls.append(el)
  19.    
  20. maxel=ls[0]
  21.  
  22. mi=0
  23. for i in range(1,len(ls)):
  24.     if ls[i]>maxel:
  25.         maxel=ls[i]
  26.         mi = i
  27.        
  28. print("Max element is: ",maxel)
  29.  
  30. #output
  31. Enter Elements in array
  32. 13
  33. 67
  34. 34
  35. 99
  36. 12
  37. Max element is:  99
  38.  
  39. #wap to print common elements
  40. def compare_lists(list1, list2):
  41.     common_elements = set(list1) & set(list2)
  42.     return list(common_elements)
  43.  
  44.  
  45. list1_input = input("Enter elements of the first list separated by spaces: ")
  46. list1 = list(map(str, list1_input.split()))
  47.  
  48.  
  49. list2_input = input("Enter elements of the second list separated by spaces: ")
  50. list2 = list(map(str, list2_input.split()))
  51.  
  52. common_elements = compare_lists(list1, list2)
  53.  
  54. if common_elements:
  55.     print("Common elements: ", common_elements)
  56. else:
  57.     print("No common elements found.")
  58.  
  59. #output
  60. Enter elements of the first list separated by spaces: 12 13 45 67 89
  61. Enter elements of the second list separated by spaces: 12 45 67 89
  62. Common elements:  ['12', '45', '67', '89']
  63.  
  64. #Wap to remove even numbers
  65. def remove_even_numbers(input_list):
  66.     return [num for num in input_list if int(num) % 2 != 0]
  67.  
  68. user_input = input("Enter a list of numbers separated by spaces: ")
  69. numbers_list = user_input.split()
  70.  
  71. filtered_list = remove_even_numbers(numbers_list)
  72.  
  73. print("List after removing even numbers:", filtered_list)
  74.  
  75.  
  76. #output
  77. Enter a list of numbers separated by spaces: 12 34 56 77 43 31
  78. List after removing even numbers: ['77', '43', '31']
  79.  
  80. #Wap to print number of occurance
  81. def count_occurrences(array, target_element):
  82.     return array.count(target_element)
  83.  
  84. user_input = input("Enter a list of numbers separated by spaces: ")
  85. numbers_list = user_input.split()
  86.  
  87.  
  88. target_element = input("Enter the element to count: ")
  89.  
  90. occurrences = count_occurrences(numbers_list, target_element)
  91.  
  92. print(f"The number of occurrences of {target_element} in the list is: {occurrences}")
  93.  
  94. #output
  95. Enter a list of numbers separated by spaces: 12 33 33 45 67 54
  96. Enter the element to count: 33
  97. The number of occurrences of 33 in the list is: 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement