Python Interview Q_A & Case studies
Python Interview Q_A & Case studies
Below Playlist Contains more than 20 Python EDA Case studies videos-
- https://youtube.com/playlist?list=PL_1pt6K-CLoDMEbYy2PcZuITWEjqMfyoA
Solution:
With Indexing:
def reverse_string(s):
return s[::-1]
Example usage
Output:
Original string: Hello, World!
Without Indexing:
def reverse_string(s):
reversed_str = ""
for char in s:
reversed_str = char + reversed_str
return reversed_str
Example usage
Output:
Solution:
For String:
def is_palindrome(s):
s = s.replace(" ", "").lower()
return s == s[::-1]
Example usage
Output:
For Number:
def is_palindrome(number):
num_str = str(number)
return num_str == num_str[::-1]
Example usage
input_number = 12321
if is_palindrome(input_number):
print("The number is a palindrome.")
else:
print("The number is not a palindrome.")
Output:
Solution:
def count_vowels(s):
vowels = "aeiouAEIOU"
count = 0
for char in s:
if char in vowels:
count += 1
return count
Example usage
Output:
Solution:
With Function:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
Example usage
number = 5
result = factorial(number)
print("Factorial of", number, "is", result)
Output:
Factorial of 5 is 120
Without Function:
number = 5
factorial = 1
Output:
Factorial of 5 is 120
Solution:
def fibonacci(n):
fib_sequence = [0, 1]
for i in range(2, n):
next_term = fib_sequence[-1] + fib_sequence[-2]
fib_sequence.append(next_term)
return fib_sequence
Example usage
num_terms = 10
fib_sequence = fibonacci(num_terms)
print("Fibonacci sequence up to", num_terms, "terms:", fib_sequence)
Output:
Solution:
Output:
Example usage
my_list = [10, 23, 45, 67, 12, 89, 34]
max_element = find_max_element(my_list)
print("Maximum element in the list:", max_element)
Output:
Here is the continuation of the Python interview questions and answers in the
requested format:
Solution:
def is_anagram(str1, str2):
str1 = str1.replace(" ", "").lower()
str2 = str2.replace(" ", "").lower()
return sorted(str1) == sorted(str2)
Example usage
string1 = "listen"
string2 = "silent"
if is_anagram(string1, string2):
print(f"'{string1}' and '{string2}' are anagrams.")
else:
print(f"'{string1}' and '{string2}' are not anagrams.")
Output:
Solution:
def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True
Example usage
start_range = 1
end_range = 50
prime_numbers = find_primes(start_range, end_range)
print("Prime numbers between", start_range, "and", end_range, "are:",
prime_numbers)
Output:
Prime numbers between 1 and 50 are: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
41, 43, 47]
def is_pangram(sentence):
sentence = sentence.lower()
unique_chars = set(sentence)
unique_chars.difference_update(set(string.punctuation + " "))
return len(unique_chars) == 26
Example usage
input_sentence = "The quick brown fox jumps over the lazy dog"
if is_pangram(input_sentence):
print("The sentence is a pangram.")
else:
print("The sentence is not a pangram.")
Output:
Question 10: Write a Python program for basic Data Structure Operations
(e.g., list manipulation, string manipulation).
Solution:
List Manipulation
my_list = [1, 2, 3, 4, 5]
my_list.append(6)
print("After appending 6:", my_list)
Remove an element from the list
my_list.remove(3)
print("After removing 3:", my_list)
String Manipulation
words = my_string.split()
print("Split string into words:", words)
new_string = "-".join(words)
print("Joined words with '-':", new_string)
upper_string = my_string.upper()
print("Uppercase string:", upper_string)
Replace a substring
Output:
After appending 6: [1, 2, 3, 4, 5, 6]
Element at index 2: 4
Question 11: Write a Python program to find the Minimum Element in a List.
Solution:
Example usage
my_list = [10, 23, 45, 67, 12, 89, 34]
min_element = find_min_element(my_list)
print("Minimum element in the list:", min_element)
Output:
Output:
Solution:
def sum_of_digits(number):
num_str = str(number)
digit_sum = 0
for digit in num_str:
digit_sum += int(digit)
return digit_sum
Example usage
input_number = 12345
result = sum_of_digits(input_number)
print("Sum of digits in", input_number, "is", result)
Output:
Solution:
def is_armstrong(number):
num_str = str(number)
num_digits = len(num_str)
armstrong_sum = 0
for digit in num_str:
armstrong_sum += int(digit) ** num_digits
return armstrong_sum == number
Example usage
input_number = 153
if is_armstrong(input_number):
print(input_number, "is an Armstrong number.")
else:
print(input_number, "is not an Armstrong number.")
Output:
Solution:
def is_leap_year(year):
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
return True
else:
return False
Example usage
input_year = 2024
if is_leap_year(input_year):
print(input_year, "is a leap year.")
else:
print(input_year, "is not a leap year.")
Output:
Solution:
def factorial(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
Example usage
number = 5
result = factorial(number)
print("Factorial of", number, "is", result)
Output:
Factorial of 5 is 120
Solution:
def find_average(numbers):
if not numbers:
return None
total = sum(numbers)
average = total / len(numbers)
return average
Example usage
Output:
Solution:
def merge_sorted_lists(list1, list2):
merged_list = []
i=j=0
while i < len(list1) and j < len(list2):
if list1[i] < list2[j]:
merged_list.append(list1[i])
i += 1
else:
merged_list.append(list2[j])
j += 1
return merged_list
Example usage
Output:
Solution:
def remove_duplicates(input_string):
unique_chars = set()
result = ""
for char in input_string:
if char not in unique_chars:
result += char
unique_chars.add(char)
return result
Example usage
Output:
Solution:
def is_perfect_number(number):
if number <= 0:
return False
divisor_sum = 0
for i in range(1, number):
if number % i == 0:
divisor_sum += i
return divisor_sum == number
Example usage
input_number = 28
if is_perfect_number(input_number):
print(input_number, "is a perfect number.")
else:
print(input_number, "is not a perfect number.")
Output:
28 is a perfect number.
Solution:
def max_difference(nums):
if len(nums) < 2:
return None
min_element = float('inf')
max_difference = float('-inf')
for num in nums:
min_element = min(min_element, num)
max_difference = max(max_difference, num - min_element)
return max_difference
Example usage
Solution:
Example usage
input_number = 7
result = check_even_odd(input_number)
print(input_number, "is", result)
Output:
7 is Odd
Without Function:
number = 7
if number % 2 == 0:
print(number, "is Even")
else:
print(number, "is Odd")
Output:
7 is Odd
Solution:
def count_words(sentence):
words = sentence.split()
return len(words)
Example usage
Output:
Output:
Solution:
def decimal_to_binary(decimal):
binary = ""
quotient = decimal
while quotient > 0:
remainder = quotient % 2
binary = str(remainder) + binary
quotient //= 2
return binary
Example usage
decimal_number = 10
binary_number = decimal_to_binary(decimal_number)
print("Binary representation of", decimal_number, "is", binary_number)
Output:
Question 24: Write a Python program to Find the Second Largest Element in
a List.
Solution:
def second_largest(nums):
if len(nums) < 2:
return None
sorted_nums = sorted(nums, reverse=True)
return sorted_nums[1]
Example usage
Output:
Solution:
def reverse_words(input_string):
words = input_string.split()
reversed_words = words[::-1]
reversed_string = " ".join(reversed_words)
return reversed_string
Example usage
Output:
Solution:
def is_prime_factor(number, potential_factor):
if number <= 1 or potential_factor <= 1:
return False
return number % potential_factor == 0
Example usage
number = 15
potential_factor = 3
if is_prime_factor(number, potential_factor):
print(potential_factor, "is a prime factor of", number)
else:
print(potential_factor, "is not a prime factor of", number)
Output:
3 is a prime factor of 15
Solution:
def is_power_of_two(number):
if number <= 0:
return False
while number > 1:
if number % 2 != 0:
return False
number //= 2
return True
Example usage
number = 16
if is_power_of_two(number):
print(number, "is a power of two.")
else:
print(number, "is not a power of two.")
Output:
16 is a power of two.
Solution:
def celsius_to_fahrenheit(celsius):
fahrenheit = (celsius * 9/5) + 32
return fahrenheit
Example usage
celsius_temperature = 25
fahrenheit_temperature = celsius_to_fahrenheit(celsius_temperature)
print("Celsius:", celsius_temperature, "Fahrenheit:", fahrenheit_temperature)
Output:
Question 29: Write a Python program to calculate the LCM (Least Common
Multiple) of Two Numbers.
Solution:
import math
Example usage
num1 = 12
num2 = 18
result = lcm(num1, num2)
print("LCM of", num1, "and", num2, "is", result)
Output:
LCM of 12 and 18 is 36
Question 30: Write a Python program to Find the GCD (Greatest Common
Divisor) of Two Numbers.
Solution:
import math
Example usage
num1 = 56
num2 = 98
result = gcd(num1, num2)
print("GCD of", num1, "and", num2, "is", result)
Output:
GCD of 56 and 98 is 14