1. Explain list. How it is different from tuple.
A list in Python is a mutable, ordered collection of elements that can hold different data types. Lists are
defined using square brackets [].
List: Mutable (can be modified) Tuple:Immutable (cannot be modified)
list Requires more memory tuple Requires less memory
lists are Used when data needs to be modified tuples are Used for fixed data ([Link], config
lists are Slower due to dynamic nature settings)
tuples are Faster due to immutability
Defined using [] Defined using ()
2. How lambda function is different from normal function.
* A lambda function is an anonymous, single-line function defined using the lambda keyword. It can take
any number of arguments but must have only one expression.
* It is inline and concise.
* lambda functions are Used in functional programming (e.g., with map(), filter(), sorted()).
* lambda functions Cannot contain multiple statements or assignments.
* Best for short, throwaway functions
2. Normal Function (Using def)
* A normal function is defined using the def keyword and can have multiple statements.
* Can contain multiple lines of code.
* Supports loops, conditions, and multiple expressions.
* Best for reusable, complex logic
3. What is inheritance? Explain use of super() method
Inheritance is a fundamental OOP (Object-Oriented Programming) concept where a class (child class)
derives properties and behavior from another class (parent class). This promotes code reusability and
hierarchical relationships.
The super() method is used to call the parent class constructor (__init__()) or methods inside the child
class. This is useful when extending functionality without completely overriding it.
* Avoids code duplication.
* Ensures parent class is properly initialized.
* Useful in multiple inheritance scenarios.
4. How to create custom exception in python?
In Python, we can create a custom exception by defining a new class that inherits from the built-in
Exception class. This allows us to create meaningful error messages for specific scenarios.
To do this, we follow three steps:
* Create a new class that extends Exception.
* Define an __init__ method if we want to pass custom error messages.
* Use the raise keyword to trigger the exception when needed.
5. What are membership operators in python?
Membership operators in Python are used to check whether a value is present in a sequence like a list,
tuple, string, or dictionary. There are two membership operators:
in Operator – Returns True if the value is found in the sequence.
not in Operator – Returns True if the value is not found in the sequence.