Python Interview Questions

30 Questions

Python Basics

Q1

What are the key features of Python?

Python is a high-level, interpreted programming language known for its simple syntax and readability. It supports object-oriented programming, dynamic typing, large libraries, and cross-platform compatibility. Python is widely used in web development, automation, AI, machine learning, and data science.

Q2

What is the difference between a list and a tuple in Python?

Lists are mutable, meaning their values can be changed after creation. Tuples are immutable and cannot be modified once created. Lists use square brackets [], while tuples use parentheses (). Tuples are generally faster and used for fixed data.

Q3

What is the difference between deep copy and shallow copy?

A shallow copy copies only the reference of nested objects, so changes in nested objects affect both copies. A deep copy creates completely independent copies of all nested objects. Python provides copy() for shallow copy and deepcopy() from the copy module for deep copy.

Q4

What are Python decorators?

Decorators are functions that modify the behavior of another function without changing its actual code. They are commonly used for logging, authentication, timing, and access control. Decorators use the @ symbol before the function definition.

Q5

What is a lambda function in Python?

A lambda function is a small anonymous function written in a single line using the lambda keyword. It is mainly used for short operations inside functions like map(), filter(), and sorted().

Q6

What is list comprehension in Python?

List comprehension provides a compact way to create lists using a single line of code. It improves readability and performance compared to traditional loops. Example: squares = [x*x for x in range(5)]

Q7

What is the difference between == and is in Python?

== checks whether two objects have equal values, while is checks whether two variables point to the same memory location. == compares content, whereas is compares object identity.

Q8

What are Python generators?

Generators are functions that return values one at a time using the yield keyword instead of return. They are memory efficient because they generate items only when needed instead of storing all values in memory.

Q9

What is the difference between append() and extend()?

append() adds a single element to the end of a list, while extend() adds multiple elements from another iterable to the list. Example: list1.append([4,5]) list1.extend([4,5])

Q10

What is exception handling in Python?

Exception handling prevents program crashes by handling runtime errors using try, except, finally, and raise blocks. It helps maintain program flow even when errors occur.

NumPy & Pandas

Q11

What is NumPy and why is it used?

NumPy is a Python library used for numerical computing. It provides fast multidimensional arrays and mathematical operations. NumPy is highly optimized and widely used in machine learning and data analysis.

Q12

What is the difference between NumPy arrays and Python lists?

NumPy arrays are faster, consume less memory, and support vectorized operations. Python lists are more flexible but slower for numerical computations. NumPy arrays are mainly used in scientific computing and machine learning.

Q13

What is Pandas?

Pandas is a Python library used for data manipulation and analysis. It provides powerful data structures like DataFrame and Series for handling structured data efficiently.

Q14

What is a DataFrame in Pandas?

A DataFrame is a two-dimensional tabular data structure in Pandas with rows and columns. It is similar to an Excel sheet or SQL table and is widely used for data analysis.

Q15

What is the difference between loc and iloc in Pandas?

loc accesses data using row and column labels, while iloc accesses data using integer index positions. Example: df.loc[0,'Name'] df.iloc[0,1]

Q16

How do you handle missing values in Pandas?

Missing values can be handled using methods like dropna(), fillna(), forward fill, backward fill, or replacing with mean, median, or mode values depending on the dataset.

Q17

What is groupby() in Pandas?

groupby() is used to split data into groups based on specific columns and perform aggregate operations like sum, average, count, or maximum values.

Q18

What is the difference between merge() and concat() in Pandas?

merge() combines DataFrames based on common columns similar to SQL joins, while concat() combines DataFrames vertically or horizontally without matching keys.

Q19

What is vectorization in NumPy and Pandas?

Vectorization performs operations on entire arrays or columns at once instead of using loops. It improves performance and reduces execution time significantly.

Q20

What is pivot_table() in Pandas?

pivot_table() is used to summarize and aggregate data in a tabular format similar to Excel pivot tables. It helps analyze grouped data efficiently.

Python for Data Science

Q21

Why is Python popular in Data Science?

Python is popular in Data Science because of its simple syntax, large ecosystem of libraries, strong community support, and powerful frameworks like NumPy, Pandas, Scikit-learn, TensorFlow, and PyTorch.

Q22

What libraries are commonly used in Data Science?

Popular Data Science libraries include NumPy for numerical computing, Pandas for data analysis, Matplotlib and Seaborn for visualization, Scikit-learn for machine learning, and TensorFlow/PyTorch for deep learning.

Q23

What is Scikit-learn?

Scikit-learn is a machine learning library in Python that provides tools for classification, regression, clustering, preprocessing, model evaluation, and feature selection.

Q24

How do you read CSV files in Python?

CSV files are commonly read using Pandas with the read_csv() function. Example: import pandas as pd df = pd.read_csv('data.csv')

Q25

What is data preprocessing?

Data preprocessing is the process of cleaning and transforming raw data before training machine learning models. It includes handling missing values, encoding, scaling, and removing duplicates.

Q26

What is feature scaling?

Feature scaling standardizes numerical data to ensure all features contribute equally to machine learning models. Common methods include normalization and standardization.

Q27

What is one-hot encoding?

One-hot encoding converts categorical variables into binary columns so machine learning models can process non-numeric data efficiently.

Q28

How do you visualize data in Python?

Data visualization is commonly done using Matplotlib and Seaborn libraries. They help create plots like bar charts, histograms, scatter plots, and heatmaps for better data understanding.

Q29

What is the difference between supervised and unsupervised learning?

Supervised learning uses labeled data to predict outputs, while unsupervised learning finds hidden patterns in unlabeled data. Examples include regression for supervised learning and clustering for unsupervised learning.

Q30

Explain a Data Science project using Python.

A Data Science project usually includes problem understanding, data collection, preprocessing, exploratory data analysis, feature engineering, model building, evaluation, and deployment. Python libraries like Pandas, NumPy, and Scikit-learn are commonly used throughout the workflow.