Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Python 1

Uploaded by

Prashant Raut
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python 1

Uploaded by

Prashant Raut
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Work:

"Hi, my name is [Your Name], and I recently graduated with a degree in AI and Data
Science from [University Name]. During my internship at Maxgen Technologies, I
contributed to multiple data-driven projects where I helped analyze and interpret
large datasets to support business decisions."

Achievement:
"During my internship, I developed a machine learning model that improved the
prediction accuracy of customer behaviour by 15%. This project enhanced the
company’s decision-making process in customer engagement strategies."

Strengths:
"I excel at problem-solving and have a keen eye for detail. My ability to break down
complex data and derive actionable insights has been recognized during my
internship and academic projects."

Projects:
"I’ve worked on various data science projects, including a Credit Risk Prediction
model and a Restaurant Insights Analysis project, where I was able to demonstrate
my skills in data exploration, machine learning, and predictive modelling."
What are the basic data types in Python?

Response: Python has several built-in data types, including:

• int: Represents integers, e.g., 5, -10.

• float: Represents floating-point numbers, e.g., 3.14, -2.5.

• str: Represents strings, e.g., 'hello', "world".

• bool: Represents Boolean values, True or False.

• list: An ordered, mutable collection, e.g., [1, 2, 3].

• tuple: An ordered, immutable collection, e.g., (1, 2, 3).

• set: An unordered collection of unique elements, e.g., {1, 2, 3}.

• dict: A collection of key-value pairs, e.g., {'a': 1, 'b': 2}.

2. What is a list in Python and how do you declare it?

Response: A list in Python is a mutable, ordered collection of elements that can store items of
different data types. You can declare a list using square brackets:

python

Copy code

my_list = [1, 'apple', 3.5, True]

3. How do dictionaries differ from lists in Python?

Response:

• Lists are ordered collections of elements accessed by index, e.g., my_list[0].

• Dictionaries are unordered collections of key-value pairs accessed by keys, e.g.,


my_dict['key'].

Example of a dictionary:

python

Copy code

my_dict = {'name': 'Alice', 'age': 25}

4. Explain the usage of if-elif-else control structure.

Response: The if-elif-else structure is used for conditional branching. The if block runs if the
condition is true, the elif block runs if the previous conditions were false but this one is true, and the
else block runs if all previous conditions are false.

python

Copy code

x = 10
if x > 10:

print("Greater than 10")

elif x == 10:

print("Equal to 10")

else:

print("Less than 10")

5. What are loops in Python? Give an example of a for loop.

Response: Loops allow repetitive execution of a block of code. Python supports for and while loops.
Example of a for loop:

python

Copy code

for i in range(3):

print(i) # Output: 0 1 2

6. How do you define a function in Python?

Response: A function in Python is defined using the def keyword, followed by the function name,
parentheses, and a colon. The function body is indented.

python

Copy code

def greet(name):

return f"Hello, {name}!"

7. How to reverse a string in Python?

Response: You can reverse a string using slicing:

python

Copy code

my_string = "Python"

reversed_string = my_string[::-1]

print(reversed_string) # Output: nohtyP

8. How to find the largest number in a list?

Response: You can use the built-in max() function:

python

Copy code

numbers = [1, 5, 8, 3]
largest = max(numbers)

print(largest) # Output: 8

9. How to remove duplicates from a list in Python?

Response: You can convert the list to a set, which automatically removes duplicates:

python

Copy code

my_list = [1, 2, 2, 3, 4, 4]

unique_list = list(set(my_list))

print(unique_list) # Output: [1, 2, 3, 4]

10. How do you count the occurrences of each element in a list?

Response: You can use the collections.Counter class:

python

Copy code

from collections import Counter

my_list = [1, 2, 2, 3, 3, 3]

count = Counter(my_list)

print(count) # Output: Counter({3: 3, 2: 2, 1: 1})

11. How do you check if a string is a palindrome in Python?

Response: A palindrome is a string that reads the same forward and backward. You can check this by
reversing the string and comparing it with the original:

python

Copy code

def is_palindrome(s):

return s == s[::-1]

print(is_palindrome("madam")) # Output: True

12. What are list comprehensions in Python?

Response: List comprehensions provide a concise way to create lists. It consists of an expression
followed by a for statement inside square brackets. Example:

python

Copy code

squares = [x**2 for x in range(5)]

print(squares) # Output: [0, 1, 4, 9, 16]


13. How do you use a while loop in Python?

Response: A while loop continues to execute as long as the condition is true:

python

Copy code

i=1

while i <= 3:

print(i) # Output: 1 2 3

i += 1

14. How do you merge two dictionaries in Python?

Response: In Python 3.9+, you can use the union (|) operator:

python

Copy code

dict1 = {'a': 1}

dict2 = {'b': 2}

merged_dict = dict1 | dict2

print(merged_dict) # Output: {'a': 1, 'b': 2}

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

Response:

• break: Exits the loop entirely.

• continue: Skips the current iteration and moves to the next.

• pass: Does nothing, acts as a placeholder.

Example:

python

Copy code

for i in range(5):

if i == 3:

break # Loop exits when i is 3

print(i)

for i in range(5):

if i == 3:
continue # Skips printing 3

print(i)

Pandas:

1. What are the primary data structures in Pandas?

o Response:
Pandas has two primary data structures:

▪ Series: A one-dimensional labeled array capable of holding any data type.

▪ DataFrame: A two-dimensional, size-mutable, and heterogeneous tabular


data structure with labeled axes (rows and columns).

2. How do you create a DataFrame in Pandas?

o Response:
You can create a DataFrame using dictionaries, lists, NumPy arrays, or even another
DataFrame. Here's an example using a dictionary:

python

Copy code

data = {'Name': ['John', 'Anna', 'Peter'],

'Age': [28, 24, 35]}

df = pd.DataFrame(data)

3. How can you filter rows in a DataFrame?

o Response:
You can filter rows using boolean indexing. For example, to filter rows where the
'Age' column is greater than 30:

python

Copy code

df_filtered = df[df['Age'] > 30]

4. What is the difference between loc and iloc in Pandas?

o Response:

o loc is label-based, meaning you access rows and columns by labels or boolean
conditions.

o iloc is index-based, meaning you access rows and columns by their index positions.

python

Copy code

df.loc[0, 'Name'] # label-based access


df.iloc[0, 0] # index-based access

5. How do you group and aggregate data in Pandas?

o Response:
You can group data using the groupby() function and then apply aggregate functions
like sum(), mean(), count(), etc. For example:

python

Copy code

df.groupby('Category')['Sales'].sum()

6. How do you handle missing values in Pandas?

o Response:
You can handle missing values by using functions like fillna() to replace them, or
dropna() to remove rows/columns with missing values. Example:

python

Copy code

df.fillna(0) # Replaces NaN with 0

df.dropna() # Drops rows with any missing value

7. How can you merge two DataFrames in Pandas?

o Response:
You can merge DataFrames using the merge() function, similar to SQL joins. For
example:

python

Copy code

pd.merge(df1, df2, on='ID', how='inner') # Inner join on 'ID' column

8. How do you add and remove columns in a DataFrame?

o Response:

o To add a column, simply assign values to a new column name:

python

Copy code

df['New_Column'] = [value1, value2, value3]

o To remove a column, use the drop() method:

python

Copy code

df.drop('Column_Name', axis=1, inplace=True)


Exploratory Data Analysis (EDA):

9. What are descriptive statistics, and how do you calculate them in Pandas?

o Response:
Descriptive statistics summarize the central tendency, dispersion, and shape of a
dataset’s distribution. You can use describe() in Pandas to get a quick summary:

python

Copy code

df.describe()

10. How do you visualize data in Pandas using line plots, bar plots, and histograms?

• Response:
You can use the Pandas built-in plotting functions to visualize data. Examples:

python

Copy code

df.plot.line() # Line plot

df.plot.bar() # Bar plot

df['column'].plot.hist() # Histogram

11. What is correlation, and how do you calculate it in Pandas?

• Response:
Correlation measures the strength of the relationship between two variables. You can
calculate it using corr():

python

Copy code

df.corr()

This will return a correlation matrix for the numerical columns.

12. How do you handle duplicate rows in a DataFrame?

• Response:
You can handle duplicates by using drop_duplicates(). For example:

python

Copy code

df.drop_duplicates(inplace=True)

13. What is data transformation, and how can it be done in Pandas?

• Response:
Data transformation involves converting data into a suitable format for analysis. In Pandas,
you can apply transformations using apply(), map(), replace(), etc. For example, to normalize
a column:

python

Copy code

df['Normalized'] = (df['Value'] - df['Value'].min()) / (df['Value'].max() - df['Value'].min())

14. How do you calculate covariance in Pandas?

• Response:
Covariance measures the directional relationship between two variables. You can calculate it
using the cov() function:

python

Copy code

df.cov()

15. What strategies can you use to handle missing data in EDA?

• Response:
You can either:

• Remove missing data using dropna().

• Impute missing data using fillna(), replacing NaN with the mean, median, or mode of the
column.

• Use advanced imputation techniques such as K-Nearest Neighbors (KNN) or regression


imputation depending on the complexity of the data.

NumPy:

1. What is a NumPy array, and how is it different from a Python list? Answer:
A NumPy array is a multi-dimensional, homogeneous array (meaning all elements are of the
same data type) used for numerical computations. It is different from a Python list because:

o It supports vectorized operations, making it much faster than lists for large datasets.

o Arrays have fixed size and consume less memory due to contiguous allocation.

o Operations on arrays are more efficient as they are performed element-wise in


compiled C code.

2. How do you create a NumPy array? Answer:


You can create a NumPy array using:

o np.array() from a list:

python

Copy code
import numpy as np

arr = np.array([1, 2, 3])

o np.zeros(), np.ones(), or np.random() for arrays filled with zeros, ones, or random
values:

python

Copy code

zeros_arr = np.zeros((2, 3)) # 2x3 array of zeros

ones_arr = np.ones((2, 3)) # 2x3 array of ones

random_arr = np.random.rand(3, 3) # 3x3 array of random values

3. What are slicing and indexing in NumPy arrays? Answer:


Slicing and indexing in NumPy arrays allow you to access specific elements or subsets of the
array. Slicing syntax is start:stop:step.
Example:

python

Copy code

arr = np.array([1, 2, 3, 4, 5])

print(arr[1:4]) # Output: [2 3 4]

You can also slice multi-dimensional arrays:

python

Copy code

arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

print(arr_2d[:, 1]) # Second column [2, 5, 8]

4. How are arithmetic operations performed on NumPy arrays? Answer:


Arithmetic operations are element-wise, meaning operations are performed for
corresponding elements.
Example:

python

Copy code

a = np.array([1, 2, 3])

b = np.array([4, 5, 6])

print(a + b) # Output: [5, 7, 9]


print(a * b) # Output: [4, 10, 18]

Integration with Other Libraries:

5. How can you visualize a line plot using Pandas? Answer:


Pandas provides easy integration with Matplotlib for visualizations. To create a line plot:

python

Copy code

import pandas as pd

import matplotlib.pyplot as plt

data = {'x': [1, 2, 3], 'y': [2, 4, 6]}

df = pd.DataFrame(data)

df.plot(x='x', y='y', kind='line')

plt.show()

6. How do you create a bar plot using Pandas? Answer:


Similar to line plots, you can use the plot() method for bar plots:

python

Copy code

df = pd.DataFrame({'A': [3, 8, 1], 'B': [4, 7, 2]})

df.plot(kind='bar')

plt.show()

Key Concepts to Revise:

7. How do you handle missing values in a Pandas DataFrame? Answer:


You can handle missing values using:

o df.isnull() to detect missing values.

o df.fillna() to fill missing values with a specific value or method.

o df.dropna() to remove rows/columns with missing values.

python

Copy code

df.fillna(0) # Replace NaN with 0


df.dropna() # Drop rows with any NaN values

8. How do you handle duplicate values in a dataset using Pandas? Answer:

o Use df.duplicated() to find duplicates.

o Use df.drop_duplicates() to remove duplicate rows.

python

Copy code

df.drop_duplicates(inplace=True)

9. How do you read and write CSV files in Pandas? Answer:

o To read a CSV file:

python

Copy code

df = pd.read_csv('file.csv')

o To write a DataFrame to CSV:

python

Copy code

df.to_csv('output.csv', index=False)

10. What is data transformation, and how is it done using Pandas or NumPy? Answer:
Data transformation involves altering the data format or scale for analysis. Common
transformations include:

• Normalization: Scaling data to a range of [0, 1].

• Standardization: Scaling data to have mean 0 and standard deviation 1.

python

Copy code

from sklearn.preprocessing import MinMaxScaler, StandardScaler

scaler = MinMaxScaler()

scaled_data = scaler.fit_transform(df[['column']])
11. What is data aggregation, and how do you perform it with Pandas? Answer:
Aggregation involves summarizing data using functions like sum(), mean(), etc. In Pandas,
you can use:

python

Copy code

df.groupby('column').agg({'value_column': ['sum', 'mean']})

12. How do you merge two DataFrames in Pandas? Answer:


You can merge DataFrames using pd.merge().
Example:

python

Copy code

pd.merge(df1, df2, on='common_column', how='inner')

13. How do you concatenate DataFrames in Pandas? Answer:


You can concatenate DataFrames using pd.concat().
Example:

python

Copy code

pd.concat([df1, df2], axis=0) # Vertically concatenate

pd.concat([df1, df2], axis=1) # Horizontally concatenate

14. How can you scale numerical data using Pandas? Answer:
You can use sklearn’s MinMaxScaler or StandardScaler to scale numerical data.
Example:

python

Copy code

from sklearn.preprocessing import MinMaxScaler

scaler = MinMaxScaler()

df[['scaled_column']] = scaler.fit_transform(df[['column']])

15. How do you handle JSON files using Pandas? Answer:


Pandas provides built-in support for reading and writing JSON files:

• To read a JSON file:


python

Copy code

df = pd.read_json('file.json')

• To write a DataFrame to a JSON file:

python

Copy code

df.to_json('output.json')

You might also like