Python Coding Questions and Answers
-
Write a Python function to reverse a string.
Use the following Python code:
def reverse_string(s): return s[::-1] print(reverse_string("hello")) # Output: "olleh"
-
Write a Python function to check if a number is prime.
Use the following Python code:
def is_prime(n): if n <= 1: return False for i in range(2, int(n ** 0.5) + 1): if n % i == 0: return False return True print(is_prime(7)) # Output: True
-
Write a Python function to find the factorial of a number.
Use the following Python code:
def factorial(n): if n == 0 or n == 1: return 1 return n * factorial(n - 1) print(factorial(5)) # Output: 120
-
Write a Python function to flatten a nested list.
Use the following Python code:
def flatten_list(lst): flat_list = [] for item in lst: if isinstance(item, list): flat_list.extend(flatten_list(item)) else: flat_list.append(item) return flat_list print(flatten_list([1, [2, [3, [4]]]])) # Output: [1, 2, 3, 4]
-
Write a Python function to sort a list of dictionaries by a specific key.
Use the following Python code:
def sort_by_key(lst, key): return sorted(lst, key=lambda x: x[key]) people = [{'name': 'John', 'age': 30}, {'name': 'Jane', 'age': 25}] print(sort_by_key(people, 'age'))
-
Write a Python function to find the largest number in a list.
Use the following Python code:
def find_largest(lst): return max(lst) print(find_largest([3, 5, 7, 2, 8])) # Output: 8
-
Write a Python function to check if a string is a palindrome.
Use the following Python code:
def is_palindrome(s): return s == s[::-1] print(is_palindrome("radar")) # Output: True
-
Write a Python function to count the number of occurrences of a specific element in a list.
Use the following Python code:
def count_occurrences(lst, element): return lst.count(element) print(count_occurrences([1, 2, 3, 2, 4, 2], 2)) # Output: 3
-
Write a Python function to remove duplicates from a list.
Use the following Python code:
def remove_duplicates(lst): return list(set(lst)) print(remove_duplicates([1, 2, 2, 3, 4, 4, 5])) # Output: [1, 2, 3, 4, 5]
-
Write a Python function to merge two sorted lists into a single sorted list.
Use the following Python code:
def merge_sorted_lists(lst1, lst2): return sorted(lst1 + lst2) print(merge_sorted_lists([1, 3, 5], [2, 4, 6])) # Output: [1, 2, 3, 4, 5, 6]
-
Write a Python function to find the intersection of two lists.
Use the following Python code:
def intersection(lst1, lst2): return list(set(lst1) & set(lst2)) print(intersection([1, 2, 3], [2, 3, 4])) # Output: [2, 3]
-
Write a Python function to generate a Fibonacci sequence up to a specified number of terms.
Use the following Python code:
def fibonacci(n): sequence = [0, 1] while len(sequence) < n: sequence.append(sequence[-1] + sequence[-2]) return sequence print(fibonacci(5)) # Output: [0, 1, 1, 2, 3]
-
Write a Python function to find the common elements between two lists.
Use the following Python code:
def common_elements(lst1, lst2): return list(set(lst1) & set(lst2)) print(common_elements([1, 2, 3], [3, 4, 5])) # Output: [3]
-
Write a Python function to check if a list is sorted.
Use the following Python code:
def is_sorted(lst): return lst == sorted(lst) print(is_sorted([1, 2, 3, 4])) # Output: True print(is_sorted([1, 3, 2, 4])) # Output: False
-
Write a Python function to find the longest word in a sentence.
Use the following Python code:
def longest_word(sentence): words = sentence.split() return max(words, key=len) print(longest_word("The quick brown fox jumps over the lazy dog")) # Output: "jumps"
-
Write a Python function to find the sum of digits of a number.
Use the following Python code:
def sum_of_digits(n): return sum(int(digit) for digit in str(n)) print(sum_of_digits(123)) # Output: 6
-
Write a Python function to calculate the power of a number.
Use the following Python code:
def power(base, exponent): return base ** exponent print(power(2, 3)) # Output: 8
-
Write a Python function to count the number of unique characters in a string.
Use the following Python code:
def count_unique_chars(s): return len(set(s)) print(count_unique_chars("hello")) # Output: 4
-
Write a Python function to find the minimum and maximum values in a list.
Use the following Python code:
def find_min_max(lst): return min(lst), max(lst) print(find_min_max([3, 5, 7, 2, 8])) # Output: (2, 8)
-
Write a Python function to remove all occurrences of a specific value from a list.
Use the following Python code:
def remove_value(lst, value): return [item for item in lst if item != value] print(remove_value([1, 2, 3, 2, 4], 2)) # Output: [1, 3, 4]