Python interview questions

Python quiz questions

  • 1.

    How To Convert A List Into Other Data Types?


    Sometimes, we don’t use lists as is. Instead, we have to convert them to other types.

  • 2.

    Why And When Do You Use Generators In Python?


    A generator in Python is a function which returns an iterable object. We can iterate on the generator object using the <yield> keyword. But we can only do that once because their values don’t persist in memory, they get the values on the fly.

    Generators give us the ability to hold the execution of a function or a step as long as we want to keep it. However, here are a few examples where it is beneficial to use generators.

    • We can replace loops with generators for efficiently calculating results involving large data sets.
    • Generators are useful when we don’t want all the results and wish to hold back for some time.
    • Instead of using a callback function, we can replace it with a generator. We can write a loop inside the function doing the same thing as the callback and turns it into a generator.
  • 3.

    List Down Some Of The PDB Commands For Debugging Python Programs?


    Here are a few PDB commands to start debugging Python code.

    • Add breakpoint – <b>
    • Resume execution – <c>
    • Step by step debugging – <s>
    • Move to next line – <n>
    • List source code – <l>
    • Print an expression – <p>
  • 4.

    How Do You Debug A Program In Python? Is It Possible To Step Through Python Code?


    Yes, we can use the Python debugger (<pdb>) to debug any Python program. And if we start a program using <pdb>, then it let us even step through the code.

  • 5.

    How do you Use To Convert A Number To A String?


    For converting a number into a string, you can use the built-in function <str()>.  If you want an octal or hexadecimal representation, use the inbuilt function <oct()> or <hex()>.

  • 6.

    What Is The Purpose Of Doc Strings In Python?


    In Python, documentation string is popularly known as doc strings. It sets a process of recording Python functions, modules, and classes.

  • 7.

    What Are Different Methods To Copy An Object In Python?


    There are two ways to copy objects in Python.

    • copy.copy() function
      • It makes a copy of the file from source to destination.
      • It’ll return a shallow copy of the parameter.
    • copy.deepcopy() function
      • It also produces the copy of an object from the source to destination.
      • It’ll return a deep copy of the parameter that you can pass to the function.
  • 8.

    Explain the <Self> Keyword Do?


    The <self> keyword is a variable that holds the instance of an object. In almost, all the object-oriented languages, it is passed to the methods as hidden parameter.

  • 9.

    What Are The Optional Statements That Can Be Used Inside A <Try-Except> Block In Python?


    There are two optional clauses you can use in the <try-except> block.

    • The <else> clause
      • It is useful if you want to run a piece of code when the try block doesn’t create any exception.
    • The <finally> clause
      • It is useful when you want to execute some steps which run, irrespective of whether there occurs an exception or not.
  • 10.

    Is There A Switch Or Case Statement In Python? If Not Then What Is The Reason For The Same?


    No, Python does not have a Switch statement, but you can write a Switch function and then use it.


© 2017