Core Python Concepts & Fundamentals
**Explain the difference between a list and a tuple in Python.
List: It's a container where you can change, add, or remove items after you've created it.
You use square brackets [ ] to make it.
Tuple: It's a container where, once you've put the items in, you cannot change, add, or
remove them. It's fixed. You use parentheses ( ) to make it.
List: Changeable (mutable). Use when your collection needs to grow or shrink, or its
contents might change.
Tuple: Unchangeable (immutable). Use when your collection is fixed and you want to ensure
its contents won't accidentally be altered.
What is the difference between == and is in Python?
== (Double equals) checks if what's inside the boxes is the [Link] compares the values of
the objects.
is (Is keyword) checks if the two boxes are actually the exact same [Link] compares the
identity (where they are stored in memory).
1. Explain mutable vs. immutable objects in Python. Give examples of each.
Mutable Objects: Objects whose state (content) can be changed after they are created.
Operations on them modify the object in place.
Examples: Lists, dictionaries, sets, byte arrays.
Immutable Objects: Objects whose state cannot be changed after they are created. Any
operation that seems to "modify" an immutable object actually creates a new object.
Examples: Integers, floats, strings, tuples, frozen sets..
2. What is a Python decorator? Provide a simple use case.
a Python decorator is like a fancy wrapper or an extra layer you can put around an existing
function to add new features or modify its behavior, without actually touching the original
function's code.
Simple Use Case: Timing a Function
Let's say you want to know how long a specific function takes to run, but you don't want to
add start_time and end_time code inside every function. A decorator is perfect for this..
9. Explain classes, objects, inheritance, and polymorphism in Python.
Class: A class is like a blueprint or a design. It describes what a certain type of "thing" will
look like and what actions it can perform, but it's not the actual "thing" itself.
Object: An object is an actual instance of a class. It's a real "thing" created from that
blueprint. You can have many objects from the same class, each existing independently.
Inheritance is when you create a new blueprint (a new class) that automatically gets all the
features and abilities from an existing, older blueprint. You can then add new unique
features to the new blueprint, or change how some inherited features work.
Polymorphism means "many forms." It's the idea that different types of "things" (objects)
can all understand and respond to the same instruction or command, but they each carry
out that command in their own unique way.
What is the difference between shallow copy and deep copy?
Shallow copy creates a new object but references the same elements (nested objects
are not copied).
Deep copy creates a completely independent copy of both the object and its nested
objects.
What is exception handling in Python?
Exception handling allows your code to deal with runtime errors gracefully using try-except
blocks.