Python Technical Interview Questions and Answers - Set 1 (50 Questions)
Prepare for your Python interviews with these 50 technical questions and answers. Includes simple explanations and code examples.
Basics
1. What is Python?
Python is a high-level, interpreted programming language used for web development, data analysis, AI, and automation.
2. Is Python case-sensitive?
a = 5 A = 10 print(a, A) # Output: 5 10
3. What are Python data types?
Common data types: int, float, str, list, tuple, set, dict
4. How do you check a variable’s type?
x = 10 print(type(x)) #
5. What is Python’s memory management?
Python uses automatic memory management via reference counting and garbage collection.
Control Flow
6. What is an if statement?
x = 10
if x > 5:
print("x is greater than 5")
7. Difference between for and while loops?
for i in range(3): print(i) i = 0 while i < 3: print(i); i += 1
8. What is break?
for i in range(5):
if i == 3: break
print(i)
9. What is continue?
for i in range(5):
if i == 2: continue
print(i)
Functions
10. What is a Python function?
def greet():
print("Hello")
greet()
11. What are *args and **kwargs?
def test(*args, **kwargs):
print(args, kwargs)
test(1,2,3, name="Python")
12. What is a lambda function?
square = lambda x: x*x print(square(5)) # Output: 25
13. What is recursion?
def factorial(n):
return 1 if n==1 else n*factorial(n-1)
print(factorial(5)) # 120
14. What is a decorator?
def decorator(func):
def wrapper():
print("Before")
func()
return wrapper
@decorator
def hello():
print("Hello")
hello()
Data Structures
15. Difference between list and tuple?
List is mutable; tuple is immutable.
16. What is a set?
s = {1,2,2,3}
print(s) # {1,2,3}
17. What is a dictionary?
d = {"name":"Alice", "age":25}
print(d["name"])
18. How to iterate a dictionary?
for key, value in d.items():
print(key, value)
19. What is list comprehension?
squares = [x*x for x in range(5)] print(squares)
20. Difference between shallow and deep copy?
import copy lst = [[1,2],[3,4]] shallow = copy.copy(lst) deep = copy.deepcopy(lst)
OOP
21. What is a class and object?
class Student:
pass
s = Student()
22. What is inheritance?
class A: pass class B(A): pass
23. What is polymorphism?
Polymorphism allows the same method name to behave differently in different classes.
24. What is encapsulation?
Encapsulation restricts access to variables using private attributes like _var or __var.
25. What is the __init__ method?
class A:
def __init__(self, name):
self.name = name
a = A("Python")
Exceptions & File Handling
26. What is exception handling?
try:
x = 10/0
except ZeroDivisionError:
print("Cannot divide by zero")
27. What is finally?
try:
x = 10
finally:
print("Done")
28. How to read a file?
with open("test.txt","r") as f:
print(f.read())
29. How to write a file?
with open("test.txt","w") as f:
f.write("Hello")
30. How to handle multiple exceptions?
try:
x = int("abc")
except (ValueError, TypeError) as e:
print("Error:", e)
Libraries & Modules
31. What is NumPy?
import numpy as np arr = np.array([1,2,3])
32. What is Pandas?
import pandas as pd
df = pd.DataFrame({"A":[1,2],"B":[3,4]})
33. What is Matplotlib?
import matplotlib.pyplot as plt plt.plot([1,2,3],[4,5,6]) plt.show()
34. What is JSON module?
import json
data = '{"name": "Alice"}'
parsed = json.loads(data)
35. What is OS module?
import os print(os.getcwd())
Coding Questions
36. Reverse a string
s = "Python" print(s[::-1])
37. Check palindrome
s = "madam" print(s == s[::-1])
38. Fibonacci series
a,b=0,1
for i in range(5):
print(a)
a,b = b,a+b
39. Count vowels in a string
text = "Python" print(sum(1 for c in text if c in "aeiouAEIOU"))
40. Find max in a list
nums = [1,5,3] print(max(nums))
41. Merge two lists
a = [1,2] b = [3,4] c = a+b
42. Remove duplicates from list
lst = [1,2,2,3] print(list(set(lst)))
43. Sort a dictionary by value
d = {'a':3,'b':1}
print(dict(sorted(d.items(), key=lambda x: x[1])))
44. Check prime number
n=7 print(all(n%i!=0 for i in range(2,n)))
45. Find factorial
import math print(math.factorial(5))
46. Read CSV file
import csv
with open("data.csv") as f:
reader = csv.reader(f)
for row in reader: print(row)
47. Write CSV file
with open("out.csv","w",newline="") as f:
writer = csv.writer(f)
writer.writerow([1,2,3])
48. HTTP request using requests
import requests
r = requests.get("https://api.github.com")
print(r.status_code)
49. Random number generation
import random print(random.randint(1,10))
50. Check type of variable
x = [1,2,3] print(isinstance(x,list)) # True
Comments
Post a Comment