Saraswat World

If you have data with name of customers and their location, which data type will you use to store it in Python?

If you have data with name of customers and their location, which data type will you use to store it in Python?

PYTHON INTERVIEW QUESTIONS

In Python, we can use dict data type to store key value pairs. In this example, customer name can be the key and their location can be the value in a dict data type. Dictionary is an efficient way to store data that can be looked up based on a key.

What is the output of calling a list variable with a index range in Python?

What is the output of calling a list variable with a index range in Python?

PYTHON INTERVIEW QUESTIONS

Python code output Even though the list has only 2 elements, the call to thelist with index 3 does not give any index error.

What are the popular Python libraries used in Data analysis?

What are the popular Python libraries used in Data analysis?

PYTHON INTERVIEW QUESTIONS

Some of the popular libraries of Python used for Data analysis are: I. Pandas: Powerful Python Data Analysis Toolkit II. SciKit: This is a machine learning library in Python. III. Seaborn: This is a statistical data visualization library in Python. IV. SciPy: This is an open source system for science, mathematics and engineering implemented in […]

How will you execute a Python script in Unix?

How will you execute a Python script in Unix?

PYTHON INTERVIEW QUESTIONS

To execute a Python script in Unix, we need to have Python executor in Unix environment. In addition to that we have to add following line as the first line in a Python script file. #!/usr/local/bin/python This will tell Unix to use Python interpreter to execute the script.

What is the improvement in enumerate() function of Python?

What is the improvement in enumerate() function of Python?

PYTHON INTERVIEW QUESTIONS

In Python, enumerate() function is an improvement over regular iteration. The enumerate() function returns an iterator that gives (0, item[0]). E.g. >>> thelist=[‘a’,’b’] >>> for i,j in enumerate(thelist): print i,j 0 a 1 b

How can we do Functional programming in Python?

How can we do Functional programming in Python?

PYTHON INTERVIEW QUESTIONS

In Functional Programming, we decompose a program into functions. These functions take input and after processing give an output. The function does not maintain any state. Python provides built-in functions that can be used for Functional programming. Some of these functions are: I. Map() II. reduce() III. filter() Event iterators and generators can be used […]

How will you share variables across modules in Python?

How will you share variables across modules in Python?

PYTHON INTERVIEW QUESTIONS

We can create a common module with variables that we want to share. This common module can be imported in all the modules in which we want to share the variables. In this way, all the shared variables will be in one module and available for sharing with any new module as well.

What is the difference between โ€˜isโ€™ and โ€˜==โ€™ in Python?

What is the difference between โ€˜isโ€™ and โ€˜==โ€™ in Python?

PYTHON INTERVIEW QUESTIONS

We use โ€˜isโ€™ to check an object against its identity. We use โ€˜==โ€™ to check equality of two objects. E.g. >>> lst = [10,20, 20] >>> lst == lst[:] True >>> lst is lst[:] False

How do you profile a Python script?

How do you profile a Python script?

PYTHON INTERVIEW QUESTIONS

Python provides a profiler called cProfile that can be used for profiling Python code. We can call it from our code as well as from the interpreter. It gives use the number of function calls as well as the total time taken to run the script. We can even write the profile results to a […]

How will you debug a piece of code in Python?

How will you debug a piece of code in Python?

PYTHON INTERVIEW QUESTIONS

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?

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

PYTHON INTERVIEW QUESTIONS

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?

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

PYTHON INTERVIEW QUESTIONS

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?

How will you handle an error condition in Python code?

PYTHON INTERVIEW QUESTIONS

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?

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

PYTHON INTERVIEW QUESTIONS

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?

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

PYTHON INTERVIEW QUESTIONS

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 is an Object Oriented programming language or a functional programming language?

PYTHON INTERVIEW QUESTIONS

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?

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

PYTHON INTERVIEW QUESTIONS

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?

What is a Module in Python?

PYTHON INTERVIEW QUESTIONS

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?

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

PYTHON INTERVIEW QUESTIONS

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?

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

PYTHON INTERVIEW QUESTIONS

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: […]