REVIEWED STARTER COLLECTION

Python Interview Questions

Short answer se start karo, phir interview-level explanation, small Python example aur common follow-up revise karo. Ye collection canonical Python tutorial ko duplicate ya replace nahi karta.

1. What is the difference between mutable and immutable objects in Python?

Short Answer

Mutable objects can be changed after creation; immutable objects cannot be changed in place. Lists and dictionaries are mutable, while strings, numbers and tuples are common immutable types.

Detailed Explanation

Python names reference objects. Mutating a shared mutable object can therefore be visible through multiple names that reference the same object. With immutable objects, an apparent “change” creates or references a different object instead.

a = [1, 2]
b = a
b.append(3)

print(a)  # [1, 2, 3]
Learn data types →Learn lists →

2. What is the difference between a list and a tuple?

Short Answer

A list is an ordered mutable sequence. A tuple is an ordered immutable sequence.

Detailed Explanation

Use lists when the collection needs to grow, shrink or change. Tuples are useful when the sequence itself should remain fixed. Both preserve order and can contain duplicate values.

skills = ["HTML", "CSS"]
skills.append("Python")

point = (10, 20)
# point[0] = 99  # TypeError
Learn lists →Learn tuples →

3. What do *args and **kwargs do?

Short Answer

*args collects extra positional arguments into a tuple, while **kwargs collects extra keyword arguments into a dictionary.

Detailed Explanation

They let a function accept a variable number of arguments. The names args and kwargs are conventional; the unpacking operators * and ** are the important part.

def show(*args, **kwargs):
    print(args)
    print(kwargs)

show("Python", "SQL", level="beginner")
Learn this concept →

4. What are self and __init__ in a Python class?

Short Answer

self refers to the current instance inside an instance method. __init__ initializes an instance after it is created.

Detailed Explanation

When you call an instance method through an object, Python passes that instance as the first argument. By convention that parameter is named self. __init__ commonly assigns initial instance attributes.

class Student:
    def __init__(self, name):
        self.name = name

    def greet(self):
        return f"Hi, {self.name}"

student = Student("Aman")
Learn this concept →

5. What is the role of try, except, else and finally?

Short Answer

try contains code that may fail, except handles matching exceptions, else runs when no exception occurs, and finally runs whether an exception happened or not.

Detailed Explanation

Specific exception handling is better than catching everything blindly. The else block keeps success-only logic separate, while finally is useful for cleanup that must happen regardless of the outcome.

try:
    value = int("42")
except ValueError:
    print("Invalid number")
else:
    print(value)
finally:
    print("Finished")
Learn this concept →