PYTHON INTERVIEW QUESTIONS FOR TECH ROUND
1. What is Python, and what are its key features?
Answer should include Python's simplicity, readability, dynamically typed nature, support for
multiple programming paradigms, and its vast standard library.
2. Explain the difference between `==` and `is` in Python.
`==` checks for equality of values, while `is` checks for object identity.
3. How do you comment code in Python?
Use `#` for single-line comments and triple quotes `"""` or `'''` for multi-line comments or
docstrings.
4. What are mutable and immutable types in Python? Give examples.
Mutable types (like lists and dictionaries) can be modified after creation, while immutable types
(like strings, tuples, and numbers) cannot.
5. What is indentation in Python, and why is it important?
Python uses indentation (4 spaces or a tab) to define code blocks, especially in loops, functions,
and conditionals.
Data Types and Data Structures
6. What are the basic data types in Python?
Common data types include `int`, `float`, `str`, `bool`, `list`, `tuple`, `set`, and `dict`.
7. Explain the difference between lists and tuples.
Lists are mutable, while tuples are immutable.
8. How does a dictionary work in Python?
Dictionaries store key-value pairs and provide fast access by key.
9. What are sets in Python, and how are they useful?
Sets are unordered collections of unique elements, useful for removing duplicates and performing
set operations like union and intersection.
10. How do you access elements in a list?
Use zero-based indexing, e.g., `my_list[0]` for the first element, or slicing, e.g., `my_list[1:4]`.
11. What is the difference between `for` and `while` loops in Python?
A `for` loop iterates over a sequence, while a `while` loop runs as long as a condition is true.
12. How do you create a function in Python?
Use the `def` keyword, e.g., `def my_function():`.
13. What is the purpose of the `return` statement in a function?
The `return` statement ends the function and sends a result back to the caller.
14. What is a lambda function?
A lambda function is an anonymous, inline function defined with the `lambda` keyword. Example:
`lambda x: x + 1`.
15. What is a `break` statement, and how does it work in a loop?
Prepared By KSRAJU Sir
PYTHON INTERVIEW QUESTIONS FOR TECH ROUND
The `break` statement stops the loop entirely when executed.
16. What is an exception in Python?
An exception is an error that occurs during program execution, disrupting the normal flow.
17. How do you handle exceptions in Python?
Use a `try-except` block to catch and handle exceptions.
18. What is the difference between `try-except` and `try-finally`?
`try-except` handles exceptions, while `try-finally` ensures that code in the `finally` block executes
regardless of exceptions.
19. What is a class in Python?
A class is a blueprint for creating objects that encapsulate data and methods.
20. What is an object in Python?
An object is an instance of a class with specific data and behaviors.
21. Explain inheritance in Python.
Inheritance allows a class to inherit attributes and methods from another class.
22. What is polymorphism in Python?
Polymorphism allows functions or methods to operate on objects of different types in a unified
way, like using the same method name for different classes.
23. What is the purpose of the `self` keyword in class methods?
`self` refers to the instance of the class and is used to access its attributes and methods.
24. What is the `import` statement in Python, and how does it work?
The `import` statement is used to include external libraries or modules in your program.
25. What is the difference between `import module` and `from module import function`?
`import module` imports the entire module, whereas `from module import function` imports only
a specific function or variable.
26. What are some commonly used libraries in Python for data analysis?
Libraries like `numpy`, `pandas`, `matplotlib`, and `scikit-learn` are widely used for data analysis.
27. How can you read and write files in Python?
Use the `open()` function with modes such as `'r'` (read), `'w'` (write), and `'a'` (append).
28. What is the difference between `map()` and `filter()` functions?
`map()` applies a function to all items in a sequence, while `filter()` applies a function to each item
and returns only those that match a condition.
29. What are list comprehensions, and why are they useful?
List comprehensions are a concise way to create lists by embedding loops and conditions in a single
line, e.g., `[x for x in range(5)]`.
30. How does the `zip()` function work?
`zip()` combines multiple iterables into tuples, pairing elements with the same index.
Prepared By KSRAJU Sir
PYTHON INTERVIEW QUESTIONS FOR TECH ROUND
31. What are decorators in Python?
Decorators are functions that modify the behavior of other functions, typically used to add
functionality without changing code structure.
32. What is a generator, and how is it different from a regular function?
Generators yield items one at a time using `yield` instead of `return`, allowing lazy evaluation of
sequences.
33. How does Python manage memory?
Python uses a memory manager with a garbage collector to handle memory allocation and
deallocation.
34. Explain the concept of `__init__` in classes.
`__init__` is a constructor method that initializes a new object when it is created.
35. What is the Global Interpreter Lock (GIL)?
The GIL is a mutex that protects access to Python objects, preventing multiple threads from
executing Python bytecode at the same time in CPython.
LOGICAL CODING
1. Reverse a String
- Question: Write a function that takes a string as input and returns the string in reverse.
- Example:
Input: "hello"
Output: "olleh"
def reverse_string(s):
return s[::-1]
2. Check if a Number is Prime
- Question: Write a function that checks if a number is prime.
- Example:
Input: 7
Output: True
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
3. Find the Fibonacci Sequence up to `n` Terms
- Question: Write a function that returns a list containing the Fibonacci sequence up to `n` terms.
- Example:
Prepared By KSRAJU Sir
PYTHON INTERVIEW QUESTIONS FOR TECH ROUND
Input: 5
Output: [0, 1, 1, 2, 3]
def fibonacci(n):
sequence = [0, 1]
for i in range(2, n):
sequence.append(sequence[-1] + sequence[-2])
return sequence[:n]
4. Check if a String is a Palindrome
- Question: Write a function to check if a given string is a palindrome (reads the same forwards
and backwards).
- Example:
Input: "racecar"
Output: True
def is_palindrome(s):
return s == s[::-1]
5. Find the Factorial of a Number
- Question: Write a function that returns the factorial of a given number.
- Example:
Input: 5
Output: 120
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
6. Find Duplicates in a List
- Question: Write a function to find and return duplicates from a list.
- Example:
Input: [1, 2, 3, 4, 3, 2]
Output: [2, 3]
def find_duplicates(lst):
duplicates = []
seen = set()
for num in lst:
Prepared By KSRAJU Sir
PYTHON INTERVIEW QUESTIONS FOR TECH ROUND
if num in seen:
duplicates.append(num)
else:
seen.add(num)
return list(set(duplicates))
7. FizzBuzz
- Question: Write a program that prints numbers from 1 to 100, but:
- For multiples of 3, print "Fizz" instead of the number.
- For multiples of 5, print "Buzz".
- For multiples of both 3 and 5, print "FizzBuzz".
def fizz_buzz(n=100):
for i in range(1, n + 1):
if i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)
8. Find the Missing Number in an Array
- Question: Given a list of consecutive numbers from 1 to `n`, with one number missing, write a
function to find the missing number.
- Example:
Input: [1, 2, 3, 5]
Output: 4
def find_missing_number(arr, n):
expected_sum = n * (n + 1) // 2
actual_sum = sum(arr)
return expected_sum - actual_sum
9. Count Vowels in a String
- Question: Write a function that counts the number of vowels (a, e, i, o, u) in a string.
- Example:
Input: "hello world"
Output: 3
def count_vowels(s):
Prepared By KSRAJU Sir
PYTHON INTERVIEW QUESTIONS FOR TECH ROUND
vowels = 'aeiouAEIOU'
return sum(1 for char in s if char in vowels)
10. Check if Two Strings are Anagrams
- Question: Write a function to check if two strings are anagrams (contain the same letters in a
different order).
- Example:
Input: "listen", "silent"
Output: True
def are_anagrams(s1, s2):
return sorted(s1) == sorted(s2)
11. Generate Prime Numbers up to `n`
- Question: Write a function that generates all prime numbers up to a given number `n`.
- Example:
Input: 10
Output: [2, 3, 5, 7]
def generate_primes(n):
primes = []
for num in range(2, n + 1):
is_prime = all(num % i != 0 for i in range(2, int(num 0.5) + 1))
if is_prime:
primes.append(num)
return primes
12. Find the Longest Word in a Sentence
- Question: Write a function to find the longest word in a given sentence.
- Example:
Input: "I love programming"
Output: "programming"
def longest_word(sentence):
words = sentence.split()
return max(words, key=len)
Prepared By KSRAJU Sir