Beginner 50 Questions • ~8 hrs study

Python Basics (Q1-50)

Core Python fundamentals asked in every interview — data types, memory, functions, decorators, generators, and more.

1

What is Python? Why is it popular for AI/ML?

Short Answer

Python is a high-level, interpreted, dynamically-typed language popular for AI/ML due to rich libraries (NumPy, PyTorch, TensorFlow), readable syntax, and large community.

Key Points

  • Interpreted: No compilation step, faster development
  • Dynamically typed: Variable types at runtime
  • Rich ecosystem: scikit-learn, PyTorch, TensorFlow, pandas, OpenCV
  • Cross-platform: Windows, Linux, macOS

💡 Memory Trick: "Python = Easy + Powerful + Libraries + Community"

Interpreted Dynamic typing GIL
2

What are Python's data types?

Short Answer

Numeric (int, float, complex), Sequence (str, list, tuple), Set (set, frozenset), Mapping (dict), Boolean (bool), None (NoneType)

Category Types Mutable?
Numericint, float, complex❌ Immutable
Sequencelist✅ Mutable
Sequencetuple, str❌ Immutable
Setset✅ Mutable
Mappingdict✅ Mutable
Booleanbool❌ Immutable

💡 Memory Trick: "Mutable = list, dict, set. Immutable = everything else"

3

What is the difference between a list and a tuple?

Short Answer

Lists are mutable (can be changed), tuples are immutable (cannot be changed). Tuples are faster and can be used as dictionary keys.

Code Example

# List — mutable
my_list = [1, 2, 3]
my_list[0] = 10  # Works!

# Tuple — immutable
my_tuple = (1, 2, 3)
my_tuple[0] = 10  # TypeError!

💡 Memory Trick: "Tuple = Trustworthy (never changes) | List = Loose (can change)"

Follow-up Questions

  • When would you choose a tuple over a list?
  • What is namedtuple?
  • Can you have a list inside a tuple?
4

What is the difference between == and is?

Short Answer

== checks value equality. is checks identity (same object in memory).

a = [1, 2, 3]
b = [1, 2, 3]
print(a == b)  # True (same value)
print(a is b)  # False (different objects)

# Integer caching (-5 to 256)
x = 256
y = 256
print(x is y)  # True (cached!)

💡 Memory Trick: "== asks 'same value?' | is asks 'same place in memory?'"

5

Explain Python's memory management.

Short Answer

Python uses reference counting + garbage collector (generational). Objects are allocated on a private heap managed by the Python memory manager.

  • Reference Counting: Every object tracks how many references point to it
  • Garbage Collection: Handles circular references using generational GC (3 generations)
  • Memory Pool: Small objects allocated from pools (pymalloc)

💡 Memory Trick: "Ref Count + GC (3 generations) + Memory Pool"

6

What are Python decorators?

Short Answer

Decorators are functions that modify the behavior of other functions/classes without changing their source code. They use the @ syntax.

def timer(func):
    import time
    def wrapper(*args, **kwargs):
        start = time.time()
        result = func(*args, **kwargs)
        print(f"{func.__name__} took {time.time()-start:.4f}s")
        return result
    return wrapper

@timer
def train_model(epochs):
    # training logic
    pass

💡 Memory Trick: "Decorator = Wrapper Gift Box — same gift inside, prettier outside"

Follow-up Questions

  • What is functools.wraps and why is it important?
  • Can you stack multiple decorators?
  • What is a class-based decorator?

Questions 7-50 continue with the same detailed format.

Topics covered: Generators, List Comprehensions, GIL, Deep/Shallow Copy, *args/**kwargs, Magic Methods, Lambda, Exception Handling, Context Managers, String Formatting, Namespaces (LEGB), @staticmethod vs @classmethod, and more.

View Full Documentation →