April 2023

  • How will you debug a piece of code in Python?

    In Python, we can use the debugger pdb for debugging the code. To start debugging we have to enter following lines on the top of a Python script. import pdb pdb.set_trace() After adding these lines, our code runs in debug mode. Now we can use commands like breakpoint, step through, step into etc for debugging.

  • How will you check in Python, if a class is subclass of another class?

    Python provides a useful method issubclass(a,b) to check whether class a is a subclass of b. E.g. int is not a subclass of long >>> issubclass(int,long) False bool is a subclass of int >>> issubclass(bool,int) True

  • What is the difference between split() and slicing in Python?

    Both split() function and slicing work on a String object. By using split() function, we can get the list of words from a String. E.g. ‘a b c ‘.split() returns [‘a’, ‘b’, ‘c’] Slicing is a way of getting substring from a String. It returns another String. E.g. ‘a b c'[2:3] returns b

  • How will you handle an error condition in Python code?

    We can implement exception handling to handle error conditions in Python code. If we are expecting an error condition that we cannot handle, we can raise an error with appropriate message. E.g. >>> if student_score < 0: raise ValueError(“Score can not be negative”) If we do not want to stop the program, we can just…

  • What is the difference between append() and extend() functions of a list in Python?

    In Python, we get a built-in sequence called list. We can call standard functions like append() and extend() on a list. We call append() method to add an item to the end of a list. We call extend() method to add another list to the end of a list. In append() we have to add…

  • How can we retrieve data from a MySQL database in a Python script?

    To retrieve data from a database we have to make use of the module available for that database. For MySQL database, we import MySQLdb module in our Python script. We have to first connect to a specific database by passing URL, username, password and the name of database. Once we establish the connection, we can…

  • Python is an Object Oriented programming language or a functional programming language?

    Python uses most of the Object Oriented programming concepts. But we can also do functional programming in Python. As per the opinion of experts, Python is a multi-paradigm programming language. We can do functional, procedural, object-oriented and imperative programming with the help of Python.

  • How can we create a dictionary with ordered set of keys in Python?

    In a normal dictionary in Python, there is no order maintained between keys. To solve this problem, we can use OrderDict class in Python. This class is available for use since version 2.7. It is similar to a dictionary in Python, but it maintains the insertion order of keys in the dictionary collection.

  • What is a Module in Python?

    A Module is a script written in Python with import statements, classes, functions etc. We can use a module in another Python script by importing it or by giving the complete namespace. With Modules, we can divide the functionality of our application in smaller chunks that can be easily managed.

  • What is the use of double forward slash // operator in Python?

    Python provides // operator to perform floor division of a number by another. The result of // operator is a whole number (without decimal part) quotient that we get by dividing left number with right number. It can also be used floordiv(a,b). E.g. 10// 4 = 2 -10//4 = -3

  • What is the use of zip() function in Python?

    In Python, we have a built-in function zip() that can be used to aggregate all the Iterable objects of an Iterator. We can use it to aggregate Iterable objects from two iterators as well. E.g. list_1 = [‘a’, ‘b’, ‘c’] list_2 = [‘1’, ‘2’, ‘3’] for a, b in zip(list_1, list_2): print a, b Output:…

  • What is None in Python?

    None is a reserved keyword used in Python for null objects. It is neither a null value nor a null pointer. It is an actual object in Python. But there is only one instance of None in a Python environment. We can use None as a default argument in a function. During comparison we have…

  • What is Python Flask?

    Python Flask is a micro-framework based on Python to develop a web application. It is a very simple application framework that has many extensions to build an enterprise level application. Flask does not provide a data abstraction layer or form validation by default. We can use external libraries on top of Flask to perform such…

  • What is the use of frozenset in Python?

    A frozenset is a collection of unique values in Python. In addition to all the properties of set, a frozenset is immutable and hashable. Once we have set the values in a frozenset, we cannot change. So we cannot use and update methods from set on frozenset. Being hashable, we can use the objects in…

  • What is a metaclass in Python?

    A metaclass in Python is also known as class of a class. A class defines the behavior of an instance. A metaclass defines the behavior of a class. One of the most common metaclass in Python is type. We can subclass type to create our own metaclass. We can use metaclass as a class-factory to…

  • What are the main benefits of using Python?

    Some of the main benefits of using Python are as follows: I. Easy to learn: Python is simple language. It is easy to learn for a new programmer. II. Large library: There is a large library for utilities in Python that can be used for different kinds of applications. III. Readability: Python has a variety…

  • How will you copy an object in Python?

    In Python we have two options to copy an object. It is similar to cloning an object in Java. I. Shallow Copy: To create a shallow copy we call copy.copy(x). In a shallow copy, Python creates a new compound object based on the original object. And it tries to put references from the original object…

  • What is lambda expression in Python?

    A lambda expression in Python is used for creating an anonymous function. Wherever we need a function, we can also use a lambda expression. We have to use lambda keyword for creating a lambda expression. Syntax of lambda function is as follows: lambda argumentList: expression E.g. >>>lambda a,b: a+b The above mentioned lambda expression takes…

  • What is the difference between xrange and range in Python?

    In Python, we use range(0,10) to create a list in memory for 10 numbers. Python provides another function xrange() that is similar to range() but xrange() returns a sequence object instead of list object. In xrange() all the values are not stored simultaneously in memory. It is a lazy loading based function. But as per…

  • What is the significance of functions that start and end with underscore_ symbol in Python?

    Python provides many built-in functions that are surrounded by _ symbol at the start and end of the function name. As per Python documentation, double _ symbol is used for reserved names of functions. These are also known as System-defined names. Some of the important functions are: Object._new_ Object._init_ Object._del_