Q1: What is Python and what are its key features?
Answer: Python is a high-level, interpreted, general-purpose programming language created by Guido van Rossum in 1991. Key features:
- Simple and easy to learn
- Interpreted
- Dynamically typed
- Object-Oriented & Procedural
- Large standard library
- Cross-platform
- Open-source
Q2: What are Python’s data types?
Answer: Python has several built-in data types:
- Numeric types:
int,float,complex - Sequence types:
list,tuple,range - Text type:
str - Mapping type:
dict - Set types:
set,frozenset - Boolean type:
bool - Binary types:
bytes,bytearray,memoryview
Q3: What is the difference between list and tuple?
Answer:
- List: mutable, allows modification
- Tuple: immutable, cannot be modified
- Example:
Q4: What are mutable and immutable objects in Python?
Answer:
- Mutable: Objects that can change (
list,dict,set) - Immutable: Objects that cannot change (
int,float,str,tuple)
Q5: What is Python’s difference between shallow copy and deep copy?
Answer:
- Shallow copy: copies the object but nested objects are shared
- Deep copy: copies the object and all nested objects
- Example:
Q6: Difference between Python 2 and Python 3?
Answer:
- Print statement: Python 2 →
print "Hi", Python 3 →print("Hi") - Integer division: Python 2 truncates by default, Python 3 returns float
- Unicode: Python 3 uses Unicode strings by default
Q7: Explain Python memory management.
Answer: Python uses private heap space for memory allocation and has a garbage collector to free unreferenced memory.
Q8: What are Python decorators?
Answer: Functions that modify the behavior of another function.
Example:
Q9: What is GIL (Global Interpreter Lock)?
Answer: A mutex in Python that allows only one thread to execute at a time. Limits CPU-bound multi-threading.
Q10: Explain *args and **kwargs.
Answer:
*args→ collects positional arguments as a tuple**kwargs→ collects keyword arguments as a dictionary- Example:
Q11: What are Python modules and packages?
Answer:
- Module: A
.pyfile containing Python code - Package: A folder with
__init__.pycontaining modules
Q12: What is Python’s difference between is and ==?
Answer:
is→ checks object identity (same memory location)==→ checks value equality
Q13: What are Python’s numbers types?
Answer: int, float, complex.
Example:
Q14: Difference between append() and extend() in list?
Answer:
append()→ adds single elementextend()→ adds multiple elements from iterable
Q15: How to handle exceptions in Python?
Answer: Use try-except blocks.
Q16: What are Python’s string methods?
Answer: Common methods: lower(), upper(), split(), replace(), strip(), join()
Q17: What is Python’s difference between remove(), pop() and del?
Answer:
remove(value)→ removes first occurrencepop(index)→ removes element at index and returns itdel→ deletes element or variable entirely
Q18: What are Python’s list comprehensions?
Answer: A concise way to create lists.
Q19: Difference between range() and xrange()?
Answer:
- Python 2:
range()returns list,xrange()returns generator - Python 3:
range()behaves likexrange()
Q20: Explain Python’s pass statement.
Answer: pass is a null operation, used as a placeholder.
Q21: What are Python’s boolean operators?
Answer: and, or, not
Q22: What are Python’s file handling modes?
Answer:
r→ readw→ writea→ appendrb→ read binarywb→ write binary
Q23: How to open and read a file in Python?
Q24: What is Python’s difference between is and ==?
Answer:
is→ object identity==→ value equality
Q25: How to convert string to int in Python?
Q26: What are Python’s functions?
Answer: Functions are reusable blocks of code that perform a specific task.
Q27: Explain Python’s lambda functions.
Answer: Anonymous, single-line functions defined using lambda.
Q28: What is Python’s map() function?
Answer: Applies a function to all items in an iterable.
Q29: What is Python’s filter() function?
Answer: Filters items based on a function returning True or False.
Q30: What is Python’s reduce() function?
Answer: Reduces an iterable to a single value using a function.
Q31: Explain Python’s generators.
Answer: Functions that yield values one at a time using yield instead of return.
Q32: What are Python’s iterators?
Answer: Objects that can be iterated using next().
Q33: What is Python’s difference between deep copy and shallow copy?
Answer: Already explained in Q5. Shallow copies only top-level object; deep copies all nested objects.
Q34: What are Python’s OOP concepts?
Answer:
- Class & Object
- Inheritance
- Polymorphism
- Encapsulation
- Abstraction
Q35: How to create a class and object in Python?
Q36: What is Python’s inheritance?
Answer: Mechanism to derive a class from another class.
Q37: What is Python’s polymorphism?
Answer: Ability to take many forms; same function name behaves differently.
Q38: What is Python’s encapsulation?
Answer: Restrict access to methods and variables using private attributes (_var, __var).
Q39: What is Python’s abstraction?
Answer: Hiding complex implementation using abstract classes and methods.
Q40: Difference between Python class method, instance method, and static method?
Answer:
- Instance method: operates on object instance
- Class method: operates on class (
@classmethod) - Static method: independent, no
selforcls
Q41: Explain Python’s with statement.
Answer: Simplifies resource management; automatically closes resources.
Q42: What is Python’s __init__ method?
Answer: Constructor method called when an object is created.
Q43: What are Python’s modules and packages?
Answer: Already explained in Q11. Modules = .py files, Packages = folders with __init__.py.
Q44: Difference between del and remove()?
Answer:
del→ deletes variable or element by indexremove()→ deletes element by value
Q45: What are Python’s built-in data structures?
Answer: list, tuple, dict, set, frozenset, deque
Q46: Difference between set and frozenset?
Answer:
set→ mutablefrozenset→ immutable
Q47: Difference between is and ==?
Answer: Already explained in Q12.
Q48: How to sort a list in Python?
Q49: How to reverse a list in Python?
Q50: How to merge two dictionaries in Python?
Q51: What are Python decorators and their use cases?
Answer: Decorators are functions that modify the behavior of another function or method. They are commonly used for logging, authentication, or validation.
Q52: Explain the difference between @staticmethod and @classmethod.
Answer:
@staticmethoddoes not access the class or instance. It behaves like a regular function inside a class.@classmethodreceives the class as the first argument (cls) and can access class variables.
Q53: What are Python generators and why are they useful?
Answer: Generators are functions that yield values one at a time using the yield keyword. They are memory-efficient for large datasets.
Q54: How do coroutines work in Python?
Answer: Coroutines are asynchronous functions defined with async def. They allow cooperative multitasking with await.
Q55: What is the difference between multithreading and multiprocessing in Python?
Answer:
- Multithreading: Multiple threads share the same memory space; limited by GIL; good for I/O-bound tasks.
- Multiprocessing: Separate memory space; true parallelism; good for CPU-bound tasks.
Q56: What is Python’s Global Interpreter Lock (GIL)?
Answer: GIL is a mutex that allows only one thread to execute Python bytecode at a time, which prevents true parallel execution of threads for CPU-bound tasks.
Q57: How do you handle exceptions in Python?
Answer: Use try-except blocks, optionally else for code that runs if no exception occurs, and finally for cleanup.
Q58: How to profile Python code?
Answer: Use the cProfile module to measure execution time of functions.
Q59: What is Python’s difference between deep copy and shallow copy?
Answer:
- Shallow copy: copies the outer object only; nested objects are shared.
- Deep copy: copies the object and all nested objects recursively.
Q60: How to implement a singleton in Python?
Answer: Ensure only one instance exists.
Q61: How to merge two dictionaries in Python?
Q62: How to remove duplicates from a list in Python?
Q63: How to reverse a string in Python?
Q64: How to sort a dictionary by value?
Q65: How do Python context managers work?
Answer: Context managers manage resources and guarantee cleanup with __enter__ and __exit__.
Q66: How to use Python’s zip() function?
Q67: How to use Python’s enumerate() function?
Q68: How to apply a function to a Pandas DataFrame column?
Q69: How to handle missing values in Pandas?
Q70: How to merge two Pandas DataFrames?
Q71: How do you use Python’s Counter from collections?
Q72: How to implement Python multithreading?
Q73: How to implement Python multiprocessing?
Q74: How to measure execution time of a Python function?
Q75: How to read a large file efficiently in Python?
Q76: How do you use Python regular expressions?
Q77: How to handle JSON in Python?
Q78: How to use Python’s __str__ and __repr__ methods?
Q79: How to create a Python virtual environment?
Q80: How to implement Python property decorators?
Q81: How to reverse a list in Python?
Q82: How to remove an element from a list by index?
Q83: How to remove an element from a list by value?
Q84: How to create a Python module?
Answer: Create a .py file and import it in another script.
Q85: How to handle multiple exceptions in Python?
Q86: How to implement Python logging?
Q87: How to check Python version in code?
Q88: How to find all unique elements in a list?
Q89: How to merge two lists in Python?
Q90: How to find the length of a list, string, or dictionary?
Q91: How to use Python’s any() and all()?
Q92: How to use Python’s map() function?
Q93: How to use Python’s filter() function?
Q94: How to use Python’s reduce() function?
Q95: How to implement Python recursion?
Q96: How to check if a string is a palindrome?
Q97: How to flatten a nested list in Python?
Q98: How to copy a list in Python?
Q99: How to check if a key exists in a dictionary?
Q100: How to sort a list of dictionaries by a key?