Q-1. Write a Python program to sum all the items in a List.
Input: [1,2,3,0,79,7]
Output: 92 (Sum of elements)
Python Code:
#sum of all elements in list def sum_num(num_list): sum_list=0 #Iterating each element in List for ele in num_list: sum_list += ele return sum_list print(sum_num([1,2,3,0,79,7]))
Q-2. Write a Python program to get the largest number from a List.
Input:[1,8,3,9,4]
Output: 9(largest element in list)
Python Code:
#finding max element def max_num(num_list): max=num_list[0] #Iterating the collection for ele in num_list: if ele>max: max=ele return max print(max_num([1,8,3,9,4]))
Q-3. Write a Python program to get the smallest number from a List.
Input:[1,8,3,9,4]
Output: 1(smallest element in list)
Python Code:
#finding min element def min_num(num_list): #here we took min as first element of the list. min=num_list[0] for ele in num_list: if ele<min: min=ele return min print(min_num([1,8,3,9,4]))
Q-4. Write a Python program to clone or copy a List..
Input:[7,88,5,0,2]
Output: the new list : [7, 88, 5, 0, 2]
Python Code:
#cloning of list original_list=[7,88,5,0,2] new_list=list(original_list) print("the new list :",new_list)
Q-5. Write a python program to reverse the List.
Input:[1,2,3]
Output: [3, 2, 1]
Python Code:
#reversing the elements of list def reverse_lst(list1): n=len(list1) rev=[] for i in range(n-1,-1,-1): rev.append(list1[i]) i-=1 return rev list1=[1,2,3] print(reverse_lst(list1))
Alternative way of reversing the list
#another way of reversing the elements of list item = [10, 11, 12, 13, 14, 15] new_list = item[::-1] print(new_list)
0 Comments