Python Interview Questions & Answers - Part 6

  • Team myQ

    21) How can you copy an object in Python?
    To copy an object in Python, you can try copy.copy () or copy.deepcopy() for the general case. You cannot copy all objects but most of them.

    22) What is negative index in Python?
    Python sequences can be index in positive and negative numbers. For positive index, 0 is the first index, 1 is the second index and so forth. For negative index, (-1) is the last index and (-2) is the second last index and so forth.

    23) How you can convert a number to a string?
    In order to convert a number into a string, use the inbuilt function str(). If you want a octal or hexadecimal representation, use the inbuilt function oct() or hex().

    24) What is the difference between Xrange and range?
    Xrange returns the xrange object while range returns the list, and uses the same memory and no matter what the range size is.