1️⃣ What is the difference between a list and a tuple?
⦁ List: mutable, defined with
[]⦁ Tuple: immutable, defined with
()lst = [1, 2, 3]
tpl = (1, 2, 3)
2️⃣ How to reverse a string in Python?
s = "Hello"
rev = s[::-1] # 'olleH'
3️⃣ Write a function to find factorial using recursion.
def factorial(n):
return 1 if n == 0 else n * factorial(n-1)
4️⃣ How do you handle exceptions?
⦁ Use
try and except blocks.try:
x = 1 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
5️⃣ Difference between
== and is?⦁
== compares values⦁
is compares identities (memory locations)6️⃣ How to check if a number is prime?
def is_prime(n):
if n < 2:
return False
for i in range(2,int(n**0.5)+1):
if n % i == 0:
return False
return True
7️⃣ What are list comprehensions? Give example.
⦁ Compact way to create lists
squares = [x*x for x in range(5)]
8️⃣ How to merge two dictionaries?
⦁ Python 3.9+
d1 = {'a':1}
d2 = {'b':2}
merged = d1 | d29️⃣ Explain
*args and **kwargs.⦁
*args: variable number of positional arguments⦁
**kwargs: variable number of keyword arguments10️⃣ How do you read a file in Python?
with open('file.txt', 'r') as f:
data = f.read()Python Interview Resources: https://whatsapp.com/channel/0029VaiM08SDuMRaGKd9Wv0L
Tap ❤️ for more