ASSIGNMENT - 2 , GO_STP_8113

GO_STP_8113

ASSIGNMENT -2

https://www.goeduhub.com/11513/question-exercise-practice-solutions-science-machine-learning


#OnlineSummerTraining #machinelearning #datascience #PYTHONNUMPY #pandas #pythonprogramming #goeduhub


  • Is a list mutable? 

          Yes list is mutable.
  • Does a list need to be homogeneous?

          List is both homogeneous and heterogeneous.
  • What is the difference between a list and a tuple.

          List is mutable and tuple is immutable.
  • How to find the number of elements in the list?
          len() function is used to find the number of elements in the list.
  • How to check whether the list is empty or not?
          if len(list)=0 is used to check whether a list is empty or not.
  • How to find the first and last element of the list?
          First element list[0], Last element list[-1].
  • How to find the largest and lowest value in the list?
          min(ls) #largest value
          max(ls)#lowest value
  • How to access elements of the list?
          Indexing and slicing operations are used to access the elements of the list.
  • Remove elements in a list before a specific index

           pop() method is used to remove elements in a list before a specific index
  • Remove elements in a list between 2 indices

        del is used remove elements in a list between 2 indices
        ls=[1,2,3,4,5]
        del ls[1:4]
  • Return every 2nd element in a list between 2 indices

        list[start:stop:step]
        ls = [1,2,3]
        print(ls[::2])
  • Get the first element from each nested list in a list

        ls=[[1,2],[3,4],[5,6]]
        for i in ls:
        print((i[0]))
  • How to modify elements of the list?
        By indexing we can modify elements in list.
  • How to concatenate two lists?
        list.append() function is used to concatenate two lists.
  • How to add two lists element-wise in python?
         list1 = [1, 2, 3]
         list2 = [4, 5, 6]
        sum_list = [] .
        for (item1, item2) in zip(list1, list2):
        sum_list. append(item1+item2)
        print(sum_list) [(1 + 4), (2 + 5), (3 + 6)]

  • Difference between del and clear?
        del keyword is used to delete a particular element. The clear( ) function is used                             to delete all the elements in a dictionary.
  • Difference between remove and pop?
            The pop() function takes either no parameter or the index value as its parameter.                           When no parameter is given, this function pops() the last element and returns it.                               The remove() function, on the other hand, is used to remove the value where we do not                need the removed value to be returned.
  • Difference between append and extend?
        Python append() method adds an element to a list, and the extend() method                                concatenates the first list with another list 
  • Difference between indexing and Slicing?
        Indexing means referring to an element of an iterable by its position within the                                 iterable. Slicing means getting a subset of elements from an iterable based on                                 their indices.
  • Difference between sort and sorted?
        sort() function will modify the list it is called on. The sorted() function will create a                        new list containing a sorted version of the list it is given
  • Difference between reverse and reversed?
        reverse() actually reverses the elements in the container. reversed() doesn't                                 actually reverse anything, it merely returns an object that can be used to iterate                             over the container's elements in reverse order.
  • Difference between copy and deepcopy?
            A copy constructs a new compound object and then inserts references into it to                              the objects found in the original. A deep copy constructs a new compound                                      object and then, recursively, inserts copies into it of the objects found in                                      the original.
  • How to remove duplicate elements in the list?
        mylist = ["a""b""a""c""c"]
    mylist = list(dict.fromkeys(mylist))
    print(mylist)

  • How to find an index of an element in the python list?
        index() is used to find an index of an element in the python list.
  • How to find the occurrences of an element in the python list?
            count() and counter() methods are used to find occurrences of an element in the python list
  • How to insert an item at a given position?
        insert() is an inbuilt function in Python that inserts a given element at a given                         index in a list.
  • How to check if an item is in the list?
        To check if the item exists in the list, use Python “in operator”. 
  • How to flatten a list in python?
        We can flatten a list in python using sum().
  • How to convert python list to other data structures like set, tuple, dictionary?
        tuple(list_name) #list to tuple
        set(list_name) #list to set
        To convert list to dictionary using dictionary comprehension.
  • How to apply a function to all items in the list?
        map() is used to apply a function to all items in the list
  • How to filter the elements based on a function in a python list?
        The filter() function iterates over the elements of the list and applies the                                              fn() function to each element.
  • How python lists are stored in memory?
          Python has the variable refer to the value. Python keeps an internal counter on how many               references an object has.

Comments

Popular posts from this blog

Data Science Matplotlib Library Data Visualization, ASSIGNMENT - 6, GO_STP_8113