Python Interview Questions 30
Python Interview Questions 30
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
lst = [1, 2, 3, 4, 2, 3, 5]
lst = list(set(lst))
print(lst)
def reverse_string(s):
return ''.join(reversed(s))
print(reverse_string('hello'))
- 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?
try:
x=1/0
except ZeroDivisionError:
print('Cannot divide by zero')
except Exception as e:
print(f'Error: {e}')
import pandas as pd
df = pd.read_csv('data.csv')
print(df.head())
df.groupby('column_name').sum()
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)
27. How do you extract data from an Excel file using Python?
import pandas as pd
df = pd.read_excel('file.xlsx')
30. How do you optimize a script that processes large CSV files?