Python Interview Question
Python Interview Question
1 Rohit Verma
Question 1: Write a Python function to check if a given number is even or odd.
[1]: def is_even_or_odd(num):
if num % 2 == 0:
return "Even"
else:
return "Odd"
Question 2: Implement a function to find the sum of all the elements in a list
[6]: lst=[4,5,2,3,6,4]
def sum_of_list_elements(lst):
return sum(lst)
sum_of_list_elements(lst)
[6]: 24
Question: Write a Python program to calculate the factorial of a given number using
recursion.
[9]: def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
factorial(5)
[9]: 120
Question: Write a Python function to find the Fibonacci series up to a given number.
[11]: def fibonacci(n):
fib_series = [0, 1]
while fib_series[-1] + fib_series[-2] <= n:
1
fib_series.append(fib_series[-1] + fib_series[-2])
return fib_series
fibonacci(5)
[11]: [0, 1, 1, 2, 3, 5]
def is_palindrome(string):
cleaned_string = re.sub('[^A-Za-z0-9]+', '', string.lower())
return cleaned_string == cleaned_string[::-1]
is_palindrome('hello')
[13]: False
is_prime(45)
[18]: False
Question: Write a Python program to find the square of each element in a list.
[19]: def square_list(lst):
return [x**2 for x in lst]
square_list([4,5,2,8])
Question: Implement a function to find the maximum difference between any two
2
numbers in a list.
[20]: def max_difference(lst):
return max(lst) - min(lst)
max_difference([7,8,2,3,5])
[20]: 6
13 12
13
12
13
Question: Implement a function to check if a given string is an anagram of another
string.
[28]: def is_anagram(string1, string2):
return sorted(string1) == sorted(string2)
3
is_anagram('nothing','nothing')
[28]: True
Question: Given two sorted lists, write a function to merge them into a single sorted
list.
[29]: def merge_sorted_lists(lst1, lst2):
return sorted(lst1 + lst2)
merge_sorted_lists([4,5,2,3],[5,2,3,6])
[29]: [2, 2, 3, 3, 4, 5, 5, 6]
[ ]: