Python Viva Questions & Answers
Q: What is Python?
A: Python is a high-level, interpreted programming language known for its simplicity and readability.
Q: What are the key features of Python?
A: Easy to learn, interpreted, dynamically typed, supports OOP, extensive standard libraries, and
portable.
Q: What is the difference between list and tuple?
A: Lists are mutable and use square brackets [], while tuples are immutable and use parentheses ().
Q: What is a dictionary in Python?
A: A collection of key-value pairs, defined using curly braces { }.
Q: What are sets in Python?
A: Unordered collections of unique elements.
Q: What is the use of 'if __name__ == "__main__"'?
A: It ensures that code runs only when the script is executed directly, not when imported.
Q: What is the difference between 'is' and '=='?
A: '==' checks for value equality, 'is' checks for identity (same memory location).
Q: How do you handle exceptions in Python?
A: Using try-except blocks.
Q: What is a function?
A: A reusable block of code defined using the def keyword.
Q: What is lambda in Python?
A: An anonymous, one-line function using the lambda keyword.
Q: What is list comprehension?
A: A concise way to create lists using a single line: [x for x in range(5)]
Q: What is a module and a package?
A: A module is a Python file; a package is a collection of modules with __init__.py.
Q: How do you open and read a file in Python?
A: Using open('filename', 'r') and then read(), readline(), or readlines().
Q: What is the difference between append() and extend()?
A: append() adds a single element, extend() adds multiple elements from an iterable.
Q: What is a class in Python?
A: A blueprint for creating objects, defined using the class keyword.
Q: What are magic methods in Python?
A: Special methods with double underscores like __init__, __str__, __len__, etc.
Q: What is inheritance in Python?
A: A feature that allows a class to derive properties and methods from another class.
Q: What is the difference between deep copy and shallow copy?
A: Shallow copy copies references, deep copy copies everything recursively.
Q: What is the use of the pass statement?
A: A placeholder statement that does nothing (used to define empty blocks).
Q: What is the purpose of the 'with' statement?
A: Simplifies file handling by automatically closing the file.