Python Interview Coding Questions-Ad
Python Interview Coding Questions-Ad
------------------------------------------------------------------------------------------------------------------------------------------
def reverse_string(s):
return s[::-1]
------------------------------------------------------------------------------------------------------------------------------------------
Solution -1:
def is_prime(n):
if n <= 1:
return False
if n % i == 0:
return False
return True
Solution-2:
if num == 0 or num == 1:
for i in range(2,num):
if (num % i) == 0:
print(i,"times",num//i,"is",num)
break
else:
else:
Solution-3 :
if num > 1:
if num % i == 0:
break
else:
else:
------------------------------------------------------------------------------------------------------------------------------------------
If n==1 or n==0:
return 1
return n*fact(n-1)
n=int(input(“Enter a number:”))
print(fact(n))
------------------------------------------------------------------------------------------------------------------------------------------
A.
count = 0
if char in vowels:
count += 1
------------------------------------------------------------------------------------------------------------------------------------
6. How do you remove duplicates from a list?
A.
my_list = [1, 2, 2, 3, 4, 4, 5]
unique_list = list(set(my_list))
(OR)
my_list = [1, 2, 2, 3, 4, 4, 5]
unique_list = []
------------------------------------------------------------------------------------------------------------------------------------------
7. Write a program to find the common elements in two lists.
A.
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
(OR)
# Example usage
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
------------------------------------------------------------------------------------------------------------------------------------------
(OR)
dict1.update(dict2)
------------------------------------------------------------------------------------------------------------------------------------------
9. Write a program to sort a list of numbers.
A.
def sort_numbers(numbers):
return sorted(numbers)
------------------------------------------------------------------------------------------------------------------------------------------
10. How can you count the occurrences of each character in a string?
A.
print(char_count) # Output: Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1})
print(char_count) # Output: {'h': 1, 'e': 1, 'l': 3, 'o': 2, ' ': 1, 'w': 1, 'r': 1, 'd': 1}
------------------------------------------------------------------------------------------------------------------------------------------