Python Interview Questions and Answers
Python Interview Questions and Answers
Answers
Subjective Questions:-
13. What is the difference between `append()` and `extend()` methods in Python lists?
- `append()` is used to add a single element to the end of a list, while `extend()` is used
to add multiple elements (such as elements of another list) to the end of a list.
14. Explain the use of `*args` and `kwargs` in Python function definitions.
- `*args` is used to pass a variable number of positional arguments to a function, while
`kwargs` is used to pass a variable number of keyword arguments to a function.
15. What is a lambda function in Python? How is it different from a regular function?
- A lambda function is an anonymous function defined using the `lambda` keyword. It
can have any number of parameters but can only have one expression. Lambda functions are
typically used for short, simple operations.
20. Explain the use of the `map()` and `filter()` functions in Python.
- The `map()` function applies a given function to each item of an iterable and returns a
list of the results. The `filter()` function filters elements of an iterable based on a function that
returns `True` or `False`.
24. Explain the difference between shallow copy and deep copy in Python.
- Shallow copy creates a new object and inserts references to the original elements into
it, while deep copy creates a new object and recursively inserts copies of the original elements
into it, creating a completely independent copy.
Coding Questions:
Certainly! Here are 30 Python interview questions along with code examples:
1. How do you swap the values of two variables without using a temporary variable?
python
a=5
b = 10
a, b = b, a
print("a =", a) # Output: 10
print("b =", b) # Output: 5
8. Write a Python program to sort a list of elements using the bubble sort algorithm.
python
def bubble_sort(lst):
n = len(lst)
for i in range(n):
for j in range(0, n-i-1):
if lst[j] > lst[j+1]:
lst[j], lst[j+1] = lst[j+1], lst[j]
11. Write a Python program to find the second largest element in a list.
python
def second_largest(lst):
unique_elements = set(lst)
unique_elements.remove(max(unique_elements))
return max(unique_elements)
print(sum_of_digits(12345)) # Output: 15
15. How do you find the square root of a number in Python?
python
import math
num = 16
square_root = math.sqrt(num)
print(square_root) # Output: 4.0
18. Write a Python program to generate a random number between a given range.
python
import random
19. Write a Python program to count the occurrences of a specific character in a string.
python
def count_occurrences(string, char):
return string.count(char)
20. How do you find the maximum occurring character in a string in Python?
python
def max_occurrence(string):
char_count = {}
for char in string:
char_count[char] = char_count.get(char, 0) + 1
return max(char_count, key=char_count.get)
21. Write a Python program to find the sum of all elements in a list.
python
def sum_of_elements(lst):
return sum(lst)
23. Write a Python program to remove all occurrences of a specific element from a list.
python
def remove_all_occurrences(lst, element):
return [x for x in lst if x != element]
24. Write a Python program to find the length of the longest consecutive sequence of a given
list.
python
def longest_consecutive_sequence(lst):
longest_sequence = 0
current_sequence = 1
lst.sort()
for i in range(1, len(lst)):
if lst[i] == lst[i - 1] + 1:
current_sequence += 1
else:
longest_sequence = max(longest_sequence, current_sequence)
current_sequence = 1
return max(longest_sequence, current_sequence)
25. How do you find the most common element in a list in Python?
python
from collections import Counter
def most_common_element(lst):
counter = Counter(lst)
return counter.most_common(1)[0][0]
26. Write a Python program to find all numbers which are divisible by a given number in a list.
python
def divisible_numbers(lst, divisor):
return [x for x in lst if x % divisor == 0]
27. Write a Python program to find the missing number in a given list of numbers from 1 to n.
python
def find_missing_number(lst):
n = len(lst) + 1
total_sum = n * (n + 1) // 2
actual_sum = sum(lst)
return total_sum - actual_sum
30. Write a Python program to find the first non-repeated character in a string.
python
def first_non_repeated_character(s):
char_count = {}
for char in s:
char_count[char] = char_count.get(char, 0) + 1
for char in s:
if char_count[char] == 1:
return char
return None