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

Python Interview Questions 30

The document provides a comprehensive list of Python interview questions and answers covering key features, data types, and programming concepts. It includes explanations of functions, methods, and libraries, as well as practical coding examples. Topics range from basic syntax to advanced concepts like multiple inheritance and data manipulation with Pandas.

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)
3 views

Python Interview Questions 30

The document provides a comprehensive list of Python interview questions and answers covering key features, data types, and programming concepts. It includes explanations of functions, methods, and libraries, as well as practical coding examples. Topics range from basic syntax to advanced concepts like multiple inheritance and data manipulation with Pandas.

Uploaded by

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

Python Interview Questions and Answers

1. What are the key features of Python?

- Easy to learn and use


- Interpreted and dynamically typed
- Object-oriented
- Extensive standard libraries
- Cross-platform compatibility

2. What is the difference between list and tuple?

- List is mutable (modifiable), tuple is immutable.


- Lists use more memory, while tuples are memory efficient.
- Lists have more built-in functions compared to tuples.

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

- 'is' compares object identity (memory location).


- '==' compares values.

4. Explain *args and **kwargs in Python.

- *args allows passing multiple positional arguments.


- **kwargs allows passing multiple keyword arguments.

5. What are Python's mutable and immutable data types?

- Mutable: list, set, dictionary


- Immutable: int, float, string, tuple

6. How do you implement a stack using a list?

class Stack:
def __init__(self):
self.stack = []
def push(self, item):
self.stack.append(item)
def pop(self):
return self.stack.pop() if self.stack else None

7. How do you find the second largest number in a list?

nums = [10, 20, 4, 45, 99]


nums.remove(max(nums))
print(max(nums))

8. Write a Python function to check if two strings are anagrams.

def is_anagram(str1, str2):


return sorted(str1) == sorted(str2)
print(is_anagram('listen', 'silent')) # True

9. Write a Python program to remove duplicates from a list.

lst = [1, 2, 3, 4, 2, 3, 5]
lst = list(set(lst))
print(lst)

10. How do you reverse a string without using [::-1]?

def reverse_string(s):
return ''.join(reversed(s))
print(reverse_string('hello'))

11. What is method overloading and method overriding?

- Method Overloading: Same method name but different parameters (not directly supported in
Python).
- Method Overriding: Child class redefines a method from the parent class.

12. What is the difference between class variables and instance variables?

- Class variables are shared across instances.


- Instance variables are unique to each object.

13. What is multiple inheritance?

- A class can inherit from more than one parent class.

14. What is the use of the super() function?

- Used to call a parent class method inside a child class.

15. Explain encapsulation in Python.

- Encapsulation restricts access to certain attributes using private/protected members.

16. How to read and write a file in Python?

with open('file.txt', 'r') as f:


content = f.read()
with open('file.txt', 'w') as f:
f.write('Hello World')

17. What is the difference between raise and assert?

- 'raise' is used to throw exceptions manually.


- 'assert' is used for debugging to check conditions.

18. What are try, except, and finally used for?

- Used for handling exceptions in Python.

19. How to open a file in different modes in Python?

- 'r' (read), 'w' (write), 'a' (append), 'r+' (read/write)

20. How to handle multiple exceptions in Python?

try:
x=1/0
except ZeroDivisionError:
print('Cannot divide by zero')
except Exception as e:
print(f'Error: {e}')

21. How to read a CSV file using Pandas?

import pandas as pd
df = pd.read_csv('data.csv')
print(df.head())

22. How do you filter rows based on a condition in Pandas?

df_filtered = df[df['column_name'] > 10]

23. How to group data in Pandas?

df.groupby('column_name').sum()

24. How to remove missing values in Pandas?

df.dropna()

25. How do you convert SQL query results into a Pandas DataFrame?

import pandas as pd
import sqlite3
conn = sqlite3.connect('database.db')
df = pd.read_sql_query('SELECT * FROM table_name', conn)

26. How to schedule a Python script to run automatically?

- Use Task Scheduler (Windows) or Cron Jobs (Linux).

27. How do you extract data from an Excel file using Python?

import pandas as pd
df = pd.read_excel('file.xlsx')

28. What are lambda functions in Python?

- Anonymous functions written using the `lambda` keyword.

29. What is list comprehension in Python?

squared = [x**2 for x in range(10)]

30. How do you optimize a script that processes large CSV files?

- Use `chunksize` in Pandas, Dask, or multiprocessing.

You might also like