Basic Python Interview Questions with Answers
Q: What is Python?
A: Python is a high-level, interpreted, general-purpose programming language known for its
readability and ease of use.
Q: What are the key features of Python?
A: Easy to learn and use, Interpreted language, Dynamically typed, Extensive standard libraries,
Supports OOP and functional programming, Open-source.
Q: How is Python different from Java or C++?
A: Python is dynamically typed, concise, and has simpler syntax compared to statically typed
languages like Java and C++.
Q: Is Python interpreted or compiled?
A: Python is an interpreted language. Code is executed line by line using the Python interpreter.
Q: What are Python's basic data types?
A: int, float, complex, str, bool, list, tuple, set, dict, NoneType.
Q: What is PEP 8?
A: PEP 8 is Pythons style guide it recommends best practices for writing clean and readable code.
Q: Difference between list, tuple, set, and dictionary?
A: List: Ordered, mutable, duplicates; Tuple: Ordered, immutable, duplicates; Set: Unordered,
mutable, no duplicates; Dictionary: Key-value pairs, mutable.
Q: Mutable vs Immutable types?
A: Mutable: Can be changed (e.g., list, dict); Immutable: Cannot be changed (e.g., int, str, tuple).
Q: How is memory managed in Python?
A: Python uses automatic garbage collection and reference counting.
Q: What are *args and **kwargs?
A: *args: Non-keyword variable arguments; **kwargs: Keyword variable arguments.
Q: Difference between break, continue, and pass?
A: break: Exits loop; continue: Skips current iteration; pass: Does nothing (placeholder).
Q: How do for and while loops work?
A: for: Iterates over items; while: Runs while a condition is true.
Q: What is a list comprehension?
A: A concise way to create lists: [x*x for x in range(5)]
Q: How do you define a function in Python?
A: def greet(name): return 'Hello ' + name
Q: Function vs Method?
A: Function: Defined independently; Method: Function tied to an object.
Q: What is a lambda function?
A: An anonymous function: lambda x: x*x
Q: What is recursion?
A: A function calling itself: def factorial(n): return 1 if n == 0 else n * factorial(n-1)
Q: What are classes and objects?
A: Class: Blueprint; Object: Instance of a class.
Q: Four principles of OOP?
A: Encapsulation, Abstraction, Inheritance, Polymorphism.
Q: What is inheritance?
A: A class inherits properties/methods from another: class Dog(Animal): pass
Q: __init__ vs __str__ methods?
A: __init__: Constructor; __str__: String representation.
Q: How to open a file?
A: file = open('data.txt', 'r'); content = file.read(); file.close()
Q: What is 'with' in file handling?
A: Ensures file is properly closed: with open('data.txt') as f: data = f.read()
Q: How does exception handling work?
A: try: ... except: ... finally: ...
Q: Built-in exceptions?
A: ZeroDivisionError, TypeError, ValueError, KeyError, IndexError, etc.
Q: What is a module/package?
A: Module: Python file; Package: Directory with __init__.py
Q: Difference between is and ==?
A: ==: Compares values; is: Compares memory location.
Q: What is a decorator?
A: A function that modifies another function's behavior.
Q: What is a generator?
A: Function that yields values lazily using 'yield'.
Q: Python 2 vs Python 3?
A: Python 3 has better Unicode support, different print syntax, integer division, and is the current
standard.