Python Interview Questions – Part 1

1. What is the difference between indexing and slicing?

Indexing is the extracting or lookup one or particular values in a data structure, whereas slicing retrieves a sequence of elements.

2. What is the lambda function?

  • Lambda functions are an anonymous or nameless function.
  • These functions are called anonymous because they are not declared in the standard manner by using the def keyword. It doesn’t require the return keyword as well. These are implicit in the function.
  • The function can have any number of parameters but can have just one statement and return just one value in the form of an expression. They cannot contain commands or multiple expressions.
  • An anonymous function cannot be a direct call to print because lambda requires an expression.
  • Lambda functions have their own local namespace and cannot access variables other than those in their parameter list and those in the global namespace.

Example:

X = lambda I, j: I + j

Print (x(4, 6))

Output: 10

3. Explain zip() and enumerate() function.

The zip() function takes multiple lists as input and creates those into a single list of tuples. It does so by taking the corresponding elements of each of the lists as a parameter. It continues this process until it finds the pairs of the tuples.

Example:

We have two lists:

list1 = [‘A’, ‘B’, ‘C’, ‘D’] and list2 = [50, 100, 150, 200].

zip(list1, list2)

output:

A list of four tuples: [(‘A’, 50), (‘B’, 100), (‘C’,150), (‘D’, 200)]

In case the length of the lists is not the same, then the zip() function will not generate the tuple once the list with the shooter length ends.

The enumerate() function also takes a list as input and creates a list of tuples. However, its output: the first element of the tuple is the position of that element in the list and the second element of the tuple is the actual value of the element in the list.

In short, enumerate() function assigns an index to each item in an iterable object that can be used to reference the item later. It makes it easier to keep track of the content of an iterable object. It return (position, value). It can only take one list at a time as an input as it takes the position of all the elements.

Example:

list2 = [‘apple’, ‘ball’, ‘mango’]

e1 = enumerate(list2)

print(e1)

output: [(0, ‘apple’), (1, ‘ball’), (2, ‘mango’)]

4. How to map, reduce and filter function work?

Map – function applies the given function to all the iterable and return a new modified list. It applies the same function to each element of a sequence.

Reduce – function applies the same operations to items of a sequence. It used the result of operations as the first param of the next operation. It returns an item and not a list.

Filter – function filters an item out of a sequence. It is used to filter the given iterable (list, sets, tuple) with the help of another function passed as an argument to test all the elements to be true of false. Its output is a filtered list.

5. What is the difference between del(), clear(), remove(), and pop()?

del() – deletes the with respect to the position of the value. It does not return which value is deleted. It also changes the index towards the right by decreasing one value. It can also be used to delete the entire data structure.

clear() – clears the list.

remove() – it deletes with respect to the value hence can be used if you know which particular value of delete.

pop() – by default removes the last element and also returns back which value is deleted. It is used extensively when we would want to create referencing. In sense, we can store this deleted return value in a variable and use in future.

Popular Posts

Spread the knowledge
 
  

Leave a Reply

Your email address will not be published. Required fields are marked *