Page 28 - ipp11
P. 28

47.  What is the purpose of using range() method?
             Ans.  The range() method generates an iterable sequence using the arguments start, stop and step. It is very
                 flexible, common, easy and useful method used with for loops. For example,
                 for i in range(5):
                     print(i)
              48.  What is the difference between ‘=’ and ‘==’ operators?
             Ans.  ‘=’ is an assignment operator while ‘==’ is a comparison operator (equality).
                  For example,
                 x=90                              #Assignment operator
                 if c==7                           #Comparison operator
              49.  What is the purpose of break statement?
             Ans.  The break statement is used to forcefully terminate the loop execution where it has been given.
                  For example,
                 if a==4:
                   print(a*a)
                 else:
                   break
              50.  Define membership operators in Python with examples.
             Ans.  We use membership operators to check whether a value or variable exists in a sequence (string, list,
                 tuples, sets, dictionary) or not.
                  For example, the statements—
                 >>> x = "Hello, World!"
                 >>>print("hello" in x)
                  shall return False as the output.
              51.  What is an exception in Python?
             Ans.  An error that occurs during the execution of the source code/program is termed as an exception.
                  For example, syntax errors, runtime errors, like division by zero, are examples of exceptions in Python.
              52.  Define a list in Python. Why is it mutable and dynamic in nature?
             Ans.  A list in Python is an ordered collection of heterogeneous items, i.e., of different data types. A list is a
                 dynamic, mutable type as you can add or delete elements from the list at any time.
              53.  How does the len() method work?
             Ans.  len() returns the length (number of elements) of a sequence like a string or list.
              54.  What happens when an element gets deleted from a list?
             Ans.  When we remove an element from a list, the size or length of the list decreases by one (1).
              55.  What does append() function do?
             Ans.  It simply adds/appends an element to the end of the existing list.
              56.  How are elements within a list accessed using indexing?
             Ans.  Elements in a list are quickly accessed using square [ ] brackets and an index number of the element to
                 be accessed.
              57.  Give one difference between insert() and append() methods.
             Ans.  insert() method adds an element at the specified index of the list whereas append() method adds
                 a single element at the end of the list.
              58.  Which method is used to sort the list elements?
             Ans.  sort() in ascending order and sort(reverse=True) in descending order.
              59.  Which operators can be used with integers as well as with strings?
             Ans.  The addition (‘+’) operator, when used with integers, adds numbers whereas when used with string, it
                 concatenates the strings.
                  The multiplication (‘*’) operator, when used with integers, multiplies the numbers whereas when used
                 with string, it replicates the strings.



                                                           V.4
   23   24   25   26   27   28   29   30   31   32   33