Saraswat World

  • 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_

  • What is the use of Generator in Python?

    We can use Generator to create Iterators in Python. A Generator is written like a regular function. It can make use yield statement to return data during the function call. In this way we can write complex logic that works as an Iterator. A Generator is more compact than an Iterator due to the fact…

  • What is the difference between an Iterator and Iterable in Python?

    An Iterable is an object that can be iterated by an Iterator. In Python, Iterator object provides iter() and next() methods. In Python, an Iterable object has iter function that returns an Iterator object. When we work on a map or a for loop in Python, we can use next() method to get an Iterable…

  • How do you perform unit testing for Python code?

    We can use the unit testing modules unittest or unittest2 to create and run unit tests for Python code. We can even do automation of tests with these modules. Some of the main components of unittest are as follows: I. Test fixture: We use test fixture to create preparation methods required to run a test.…

  • What is the difference between Docstring in Python and Javadoc in Java?

    A Docstring in Python is a string used for adding comments or summarizing a piece of code in Python. The main difference between Javadoc and Docstring is that docstring is available during runtime as well. Whereas, Javadoc is removed from the Bytecode and it is not present in .class file. We can even use Docstring…