0% found this document useful (0 votes)
2 views

Python Interview Questions

This document provides a comprehensive list of Python interview questions and answers covering key features, data types, memory management, and various programming concepts such as loops, functions, and classes. It highlights important distinctions between Python elements, such as mutable vs immutable types and the differences between 'is' and '=='. Additionally, it includes practical examples for recursion, file handling, and list comprehensions.

Uploaded by

mail4mob.ashu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Interview Questions

This document provides a comprehensive list of Python interview questions and answers covering key features, data types, memory management, and various programming concepts such as loops, functions, and classes. It highlights important distinctions between Python elements, such as mutable vs immutable types and the differences between 'is' and '=='. Additionally, it includes practical examples for recursion, file handling, and list comprehensions.

Uploaded by

mail4mob.ashu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Interview Questions & Answers

1. What are the key features of Python?


- Easy to learn and read
- Interpreted and dynamically typed
- Object-oriented
- Supports multiple paradigms
- Extensive libraries and frameworks

2. How is Python an interpreted language?


Python executes code line by line at runtime, unlike compiled languages like C or Java.

3. What is PEP 8? Why is it important?


PEP 8 is a style guide for writing readable Python code. It improves code consistency and
maintainability.

4. What are Python's data types?


- Numeric: int, float, complex
- Sequence: list, tuple, range
- Text: str
- Set: set, frozenset
- Mapping: dict
- Boolean: bool
- Binary: bytes, bytearray, memoryview

5. What is the difference between a list and a tuple?


- List: Mutable, slower, uses more memory
- Tuple: Immutable, faster, uses less memory

6. How is memory managed in Python?


Python uses automatic garbage collection and a reference counting system to manage memory.

7. What are mutable and immutable types in Python?


- Mutable: Can be changed (list, dict, set)
- Immutable: Cannot be changed (int, float, tuple, str)

8. What is the difference between 'is' and '=='?


- 'is' checks object identity (memory location)
- '==' checks for value equality
9. How do you swap two variables without using a third variable?
a, b = b, a # Python tuple swapping

10. What are Python's loop types?


- for loop: Iterates over sequences
- while loop: Runs as long as a condition is true

11. What is the difference between break, continue, and pass?


- break: Exits the loop
- continue: Skips current iteration
- pass: Does nothing, used as a placeholder

12. How does Python handle switch-case statements?


Python doesn't have switch-case. Instead, it uses dictionaries or if-elif-else statements.

13. What is the difference between *args and **kwargs?


- *args: Takes multiple positional arguments
- **kwargs: Takes multiple keyword arguments

14. What is the difference between a function and a lambda function?


- Function: Defined using 'def', has multiple expressions
- Lambda: Anonymous function with a single expression

15. Explain recursion with an example.


Recursion is when a function calls itself:

def factorial(n):
if n == 0: return 1
return n * factorial(n - 1)

16. What are classes and objects in Python?


- Class: A blueprint for objects
- Object: An instance of a class

17. What is the difference between @staticmethod, @classmethod, and instance methods?
- Instance method: Works on instance variables
- Class method: Works on class variables
- Static method: Independent of instance/class

18. What are inheritance and polymorphism in Python?


- Inheritance: A child class inherits properties from a parent class
- Polymorphism: Allows the same function to have different behavior

19. How do you handle exceptions in Python?


Using try-except:
try:
x=1/0
except ZeroDivisionError:
print('Cannot divide by zero')

20. How do you read and write files in Python?


Using the with statement:
with open('file.txt', 'w') as f:
f.write('Hello, World!')

21. How do sets work in Python?


Sets store unique, unordered elements:
s = {1, 2, 2, 3}
print(s) # {1, 2, 3}

22. What are list comprehensions?


List comprehensions allow concise list creation:
squares = [x*x for x in range(5)]
print(squares) # [0, 1, 4, 9, 16]

23. What is the __init__ method?


__init__ initializes object attributes when an instance is created:
class Car:
def __init__(self, brand):
self.brand = brand

You might also like