SlideShare a Scribd company logo
PYTHON CODING INTERVIEW
QUESTIONS
1. Write a simple Python program to print an output.
print("Hello, World!")
2. Write a Python code to check if a number is even or odd
def is_even(num):
return num % 2 == 0
print(is_even(4)) # True
print(is_even(5)) # False
Write a Python program to count the number of vowels in a string
def count_vowels(s):
return sum(1 for char in s if char.lower() in
'aeiou')
print(count_vowels("Hello World")) # 3
Write a Python code to convert a string to an integer
str_num = "12345"
int_num = int(str_num)
print(int_num) # 12345
Write a Python code to merge two
dictionaries
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
merged = {**dict1, **dict2}
print(merged) #{'a': 1, 'b': 3, 'c': 4}
Write a Python program to find common elements in two lists
list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
common = list(set(list1) & set(list2))
print(common) #[3, 4]
Write a Python code to remove duplicates from a list
list1 = [1, 2, 2, 3, 4, 4]
unique_list1 = list(set(list1))
print(unique_list1) #[1, 2, 3, 4]
Write a Python program to find the longest word in a sentence
def longest_word(sentence):
words = sentence.split()
return max(words, key=len)
print(longest_word("The fox jumps over the
lazy dog")) # jumps
Write a Python code to find the first non-repeating character in a
string
def first_non_repeating_char(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
print(first_non_repeating_char("nxtwave")) # n
Write a Python code to implement a binary search algorithm
def binary_search(arr, target):
low, high = 0, len(arr) – 1
while low <= high:
mid = (low + high) // 2
if arr[mid] < target:
low = mid + 1
elif arr[mid] > target:
high = mid – 1
else:
return mid
return -1
print(binary_search([1, 2, 3, 4, 5], 3)) # 2
Write a Python code to implement a function to flatten a nested
list
def flatten(nested_list):
flat_list = []
for item in nested_list:
if isinstance(item, list):
flat_list.extend(flatten(item))
else:
flat_list.append(item)
return flat_list
print(flatten([1, [2, [3, 4], 5], 6])) # [1, 2, 3, 4, 5, 6]
Write a Python code to find the maximum subarray sum
def max_subarray_sum(arr):
max_sum = current_sum = arr[0]
for num in arr[1:]:
current_sum = max(num, current_sum + num)
max_sum = max(max_sum, current_sum)
return max_sum
print(max_subarray_sum([-2, 1, -3, 4, -1, 2, 1, -5,
4])) # 6
Write a Python program to find the intersection of two sets
def intersection(set1, set2):
return set1 & set2
print(intersection({1, 2, 3}, {2, 3, 4})) # {2, 3}
Write a Python code to implement a simple calculator
def calculator(a, b, operation):
if operation == 'add':
return a + b
elif operation == 'subtract':
return a - b
elif operation == 'multiply':
return a * b
elif operation == 'divide':
return a / b if b != 0 else "Cannot divide by zero"
else:
return "Invalid operation"
print(calculator(5, 3, 'add')) # 8
Write a Python code to find the GCD of two numbers
def gcd(a, b):
while b:
a, b = b, a % b
return a
print(gcd(48, 18)) # 6
Write a Python program to find the largest
element in a list.
def find_largest(numbers):
largest = numbers[0]
for num in numbers:
if num > largest:
largest = num
return largest
# Test the function
nums = [10, 5, 8, 20, 3]
largest_num = find_largest(nums)
print(f"The largest number is {largest_num}")
Write a Python program to reverse a string.
def reverse_string(string):
return string[::-1]
# Test the function
text = "Hello, World!"
reversed_text = reverse_string(text)
print(reversed_text)
Write a Python program to count the
frequency of each element in a list.
def count_frequency(numbers):
frequency = {}
for num in numbers:
if num in frequency:
frequency[num] += 1
else:
frequency[num] = 1
return frequency
# Test the function
nums = [1, 2, 3, 2, 1, 3, 2, 4, 5, 4]
frequency_count = count_frequency(nums)
print(frequency_count)
Write a Python program to check if a
number is prime.
def is_prime(number):
if number < 2:
return False
for i in range(2, int(number**0.5) + 1):
if number % i == 0:
return False
return True
# Test the function
num = 17
if is_prime(num):
print(f"{num} is a prime number")
else:
print(f"{num} is not a prime number")
Write a Python program to find the common
elements between two lists.
def find_common_elements(list1, list2):
common_elements = []
for item in list1:
if item in list2:
common_elements.append(item)
return common_elements
# Test the function
list_a = [1, 2, 3, 4, 5]
list_b = [4, 5, 6, 7, 8]
common = find_common_elements(list_a, list_b)
print(common)
Write a Python program to sort a list of
elements using the bubble sort algorithm.
def bubble_sort(elements):
n = len(elements)
for i in range(n - 1):
for j in range(n - i - 1):
if elements[j] > elements[j + 1]:
elements[j], elements[j + 1] = elements[j + 1], elements[j]
# Test the function
nums = [5, 2, 8, 1, 9]
bubble_sort(nums)
print(nums)
Write a Python program to find the second
largest number in a list.
def find_second_largest(numbers):
largest = float('-inf')
second_largest = float('-inf')
for num in numbers:
if num > largest:
second_largest = largest
largest = num
elif num > second_largest and num != largest:
second_largest = num
return second_largest
# Test the function
nums = [10, 5, 8, 20, 3]
second_largest_num = find_second_largest(nums)
print(f"The second largest number is {second_largest_num}")
Write a Python program to remove
duplicates from a list.
def remove_duplicates(numbers):
unique_numbers = []
for num in numbers:
if num not in unique_numbers:
unique_numbers.append(num)
return unique_numbers
# Test the function
nums = [1, 2, 3, 2, 1, 3, 2, 4, 5, 4]
unique_nums = remove_duplicates(nums)
print(unique_nums)
Program to find the sum of digits of a
given number in Python
def sum_digits(num):
Sum = 0
for digit in str(num):
sum = sum + int(digit)
return "Sum of digits :",sum
sum_digits(int(input("Enter Number :")))
Tell the output of the following
code:
import array
a = [4, 6, 8, 3, 1, 7]
print(a[-3])
print(a[-5])
print(a[-1])
Output:
3 6 7
How do you print the summation of all the numbers from 1 to
101?
print(sum(range(1, 102)))
Output:
5151

More Related Content

DOCX
CBSE Class 12 Computer practical Python Programs and MYSQL
PDF
python practicals-solution-2019-20-class-xii.pdf
PDF
solution-of-practicals-class-xii-comp.-sci.-083-2021-22 (1).pdf
PPTX
PRACTICAL FILE(COMP SC).pptx
DOCX
ECE-PYTHON.docx
PDF
Python Lab Manual
PDF
xii cs practicals class 12 computer science.pdf
DOCX
Python lab manual all the experiments are available
CBSE Class 12 Computer practical Python Programs and MYSQL
python practicals-solution-2019-20-class-xii.pdf
solution-of-practicals-class-xii-comp.-sci.-083-2021-22 (1).pdf
PRACTICAL FILE(COMP SC).pptx
ECE-PYTHON.docx
Python Lab Manual
xii cs practicals class 12 computer science.pdf
Python lab manual all the experiments are available

Similar to PYTHON PROGRAMMING INTERVIEW QUESTIONS.pptx (20)

DOCX
cs class 12 project computer science .docx
PDF
python_lab_manual_final (1).pdf
PDF
Sample Program file class 11.pdf
PDF
Python for High School Programmers
PDF
Master Python in 15 Days.pdf
PDF
Xi CBSE Computer Science lab programs
PDF
datastructure-1 lab manual journals practical
PPTX
ANSHUL RANA - PROGRAM FILE.pptx
PPTX
python_1.pptx
PPTX
Python.pptx
PPTX
Python Workshop - Learn Python the Hard Way
PDF
PyLecture4 -Python Basics2-
PDF
Andrii Soldatenko "Competitive programming using Python"
PDF
xii cs practicals
PDF
III MCS python lab (1).pdf
DOCX
Basic python laboratoty_ PSPP Manual .docx
PDF
Sasin nisar
PPTX
Python..._Practical_.goated programs.pptx
ODP
Python quickstart for programmers: Python Kung Fu
PDF
140+ Basic Python Programs This resource can assist you in preparing for your...
cs class 12 project computer science .docx
python_lab_manual_final (1).pdf
Sample Program file class 11.pdf
Python for High School Programmers
Master Python in 15 Days.pdf
Xi CBSE Computer Science lab programs
datastructure-1 lab manual journals practical
ANSHUL RANA - PROGRAM FILE.pptx
python_1.pptx
Python.pptx
Python Workshop - Learn Python the Hard Way
PyLecture4 -Python Basics2-
Andrii Soldatenko "Competitive programming using Python"
xii cs practicals
III MCS python lab (1).pdf
Basic python laboratoty_ PSPP Manual .docx
Sasin nisar
Python..._Practical_.goated programs.pptx
Python quickstart for programmers: Python Kung Fu
140+ Basic Python Programs This resource can assist you in preparing for your...
Ad

Recently uploaded (20)

PDF
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PDF
737-MAX_SRG.pdf student reference guides
PDF
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
PPTX
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
PPTX
introduction to high performance computing
PPTX
communication and presentation skills 01
PPTX
Fundamentals of Mechanical Engineering.pptx
PDF
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PDF
Categorization of Factors Affecting Classification Algorithms Selection
PPTX
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
PPT
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
PPTX
Software Engineering and software moduleing
PPTX
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PPTX
Information Storage and Retrieval Techniques Unit III
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PDF
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
PDF
August 2025 - Top 10 Read Articles in Network Security & Its Applications
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
737-MAX_SRG.pdf student reference guides
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
introduction to high performance computing
communication and presentation skills 01
Fundamentals of Mechanical Engineering.pptx
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
Categorization of Factors Affecting Classification Algorithms Selection
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
Software Engineering and software moduleing
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
Information Storage and Retrieval Techniques Unit III
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
August 2025 - Top 10 Read Articles in Network Security & Its Applications
Ad

PYTHON PROGRAMMING INTERVIEW QUESTIONS.pptx

  • 2. 1. Write a simple Python program to print an output. print("Hello, World!")
  • 3. 2. Write a Python code to check if a number is even or odd def is_even(num): return num % 2 == 0 print(is_even(4)) # True print(is_even(5)) # False
  • 4. Write a Python program to count the number of vowels in a string def count_vowels(s): return sum(1 for char in s if char.lower() in 'aeiou') print(count_vowels("Hello World")) # 3
  • 5. Write a Python code to convert a string to an integer str_num = "12345" int_num = int(str_num) print(int_num) # 12345
  • 6. Write a Python code to merge two dictionaries dict1 = {'a': 1, 'b': 2} dict2 = {'b': 3, 'c': 4} merged = {**dict1, **dict2} print(merged) #{'a': 1, 'b': 3, 'c': 4}
  • 7. Write a Python program to find common elements in two lists list1 = [1, 2, 3, 4] list2 = [3, 4, 5, 6] common = list(set(list1) & set(list2)) print(common) #[3, 4]
  • 8. Write a Python code to remove duplicates from a list list1 = [1, 2, 2, 3, 4, 4] unique_list1 = list(set(list1)) print(unique_list1) #[1, 2, 3, 4]
  • 9. Write a Python program to find the longest word in a sentence def longest_word(sentence): words = sentence.split() return max(words, key=len) print(longest_word("The fox jumps over the lazy dog")) # jumps
  • 10. Write a Python code to find the first non-repeating character in a string def first_non_repeating_char(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 print(first_non_repeating_char("nxtwave")) # n
  • 11. Write a Python code to implement a binary search algorithm def binary_search(arr, target): low, high = 0, len(arr) – 1 while low <= high: mid = (low + high) // 2 if arr[mid] < target: low = mid + 1 elif arr[mid] > target: high = mid – 1 else: return mid return -1 print(binary_search([1, 2, 3, 4, 5], 3)) # 2
  • 12. Write a Python code to implement a function to flatten a nested list def flatten(nested_list): flat_list = [] for item in nested_list: if isinstance(item, list): flat_list.extend(flatten(item)) else: flat_list.append(item) return flat_list print(flatten([1, [2, [3, 4], 5], 6])) # [1, 2, 3, 4, 5, 6]
  • 13. Write a Python code to find the maximum subarray sum def max_subarray_sum(arr): max_sum = current_sum = arr[0] for num in arr[1:]: current_sum = max(num, current_sum + num) max_sum = max(max_sum, current_sum) return max_sum print(max_subarray_sum([-2, 1, -3, 4, -1, 2, 1, -5, 4])) # 6
  • 14. Write a Python program to find the intersection of two sets def intersection(set1, set2): return set1 & set2 print(intersection({1, 2, 3}, {2, 3, 4})) # {2, 3}
  • 15. Write a Python code to implement a simple calculator def calculator(a, b, operation): if operation == 'add': return a + b elif operation == 'subtract': return a - b elif operation == 'multiply': return a * b elif operation == 'divide': return a / b if b != 0 else "Cannot divide by zero" else: return "Invalid operation" print(calculator(5, 3, 'add')) # 8
  • 16. Write a Python code to find the GCD of two numbers def gcd(a, b): while b: a, b = b, a % b return a print(gcd(48, 18)) # 6
  • 17. Write a Python program to find the largest element in a list. def find_largest(numbers): largest = numbers[0] for num in numbers: if num > largest: largest = num return largest # Test the function nums = [10, 5, 8, 20, 3] largest_num = find_largest(nums) print(f"The largest number is {largest_num}")
  • 18. Write a Python program to reverse a string. def reverse_string(string): return string[::-1] # Test the function text = "Hello, World!" reversed_text = reverse_string(text) print(reversed_text)
  • 19. Write a Python program to count the frequency of each element in a list. def count_frequency(numbers): frequency = {} for num in numbers: if num in frequency: frequency[num] += 1 else: frequency[num] = 1 return frequency # Test the function nums = [1, 2, 3, 2, 1, 3, 2, 4, 5, 4] frequency_count = count_frequency(nums) print(frequency_count)
  • 20. Write a Python program to check if a number is prime. def is_prime(number): if number < 2: return False for i in range(2, int(number**0.5) + 1): if number % i == 0: return False return True # Test the function num = 17 if is_prime(num): print(f"{num} is a prime number") else: print(f"{num} is not a prime number")
  • 21. Write a Python program to find the common elements between two lists. def find_common_elements(list1, list2): common_elements = [] for item in list1: if item in list2: common_elements.append(item) return common_elements # Test the function list_a = [1, 2, 3, 4, 5] list_b = [4, 5, 6, 7, 8] common = find_common_elements(list_a, list_b) print(common)
  • 22. Write a Python program to sort a list of elements using the bubble sort algorithm. def bubble_sort(elements): n = len(elements) for i in range(n - 1): for j in range(n - i - 1): if elements[j] > elements[j + 1]: elements[j], elements[j + 1] = elements[j + 1], elements[j] # Test the function nums = [5, 2, 8, 1, 9] bubble_sort(nums) print(nums)
  • 23. Write a Python program to find the second largest number in a list. def find_second_largest(numbers): largest = float('-inf') second_largest = float('-inf') for num in numbers: if num > largest: second_largest = largest largest = num elif num > second_largest and num != largest: second_largest = num return second_largest # Test the function nums = [10, 5, 8, 20, 3] second_largest_num = find_second_largest(nums) print(f"The second largest number is {second_largest_num}")
  • 24. Write a Python program to remove duplicates from a list. def remove_duplicates(numbers): unique_numbers = [] for num in numbers: if num not in unique_numbers: unique_numbers.append(num) return unique_numbers # Test the function nums = [1, 2, 3, 2, 1, 3, 2, 4, 5, 4] unique_nums = remove_duplicates(nums) print(unique_nums)
  • 25. Program to find the sum of digits of a given number in Python def sum_digits(num): Sum = 0 for digit in str(num): sum = sum + int(digit) return "Sum of digits :",sum sum_digits(int(input("Enter Number :")))
  • 26. Tell the output of the following code: import array a = [4, 6, 8, 3, 1, 7] print(a[-3]) print(a[-5]) print(a[-1]) Output: 3 6 7
  • 27. How do you print the summation of all the numbers from 1 to 101? print(sum(range(1, 102))) Output: 5151