Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Course Code: DECAP776

Course Name: PROGRAMMING IN PYTHON

Important instruction:

1. The question paper contains 5 questions out of which only one question shall be
allocated by the neutral examiner. The candidate should attempt only the allocated
questions out of the 5 given questions as per the allocation key. The same should be
cross verified by a neutral examiner.

Q 1: When was the python language created, who created it and why?

Python was created by Guido van Rossum, a Dutch programmer, and its development began in
the late 1980s. Guido van Rossum released the first version of Python, Python 0.9.0, in February
1991.
The creation of Python was motivated by Guido van Rossum's desire for a language that was
easy to read and write, emphasizing code readability and developer productivity. He aimed to
create a language with a clear and straightforward syntax that would facilitate the creation of
high-level software, allowing developers to express concepts in fewer lines of code than
languages like C++ or Java. Guido van Rossum's background included working on the ABC
language at Centrum Wiskunde & Informatica (CWI) in the Netherlands, and Python was
influenced by ABC's simplicity and readability.
Over the years, Python has grown in popularity and has become one of the most widely used
programming languages, known for its versatility, readability, and extensive community
support.

Q 2: Give meaning and examples of any five inbuilt functions related to sequence handling?
Certainly! Sequence handling functions are commonly used in programming to manipulate
sequences of data, such as strings, lists, tuples, or other iterable objects. Here are five inbuilt
functions related to sequence handling in Python:
1. len() - Length Function:
 Meaning: Returns the number of elements in a sequence.
 Example:
my_list = [1, 2, 3, 4, 5]
length = len(my_list)
print(length) # Output: 5

2. max() - Maximum Function:


 Meaning: Returns the largest element in a sequence.
 Example:
my_tuple = (10, 5, 8, 20, 15)
maximum_value = max(my_tuple)
print(maximum_value) # Output: 20

3. min() - Minimum Function:


Meaning: Returns the smallest element in a sequence.
Example:
my_string = "python"
minimum_value = min(my_string)
print(minimum_value) # Output: 'h'

4. sum() - Summation Function:

Meaning: Returns the sum of all elements in a numeric sequence.


Example:
my_list = [1, 2, 3, 4, 5]
total_sum = sum(my_list)
print(total_sum) # Output: 15

5. sorted() - Sorting Function:

Meaning: Returns a new sorted list from the elements of any iterable.
Example:
my_list = [5, 2, 8, 1, 3]
sorted_list = sorted(my_list)
print(sorted_list) # Output: [1, 2, 3, 5, 8]

These functions provide essential tools for working with sequences in Python and can be
applied to various types of iterable objects.

Q 3: Explain the concept of exception handling in Python and provide an example where a
‘try’ and ‘except’ block is used to catch a specific type of exception.

Exception handling in Python is a mechanism that allows you to gracefully handle runtime
errors or exceptional situations in your code. The primary components of exception handling in
Python are the try, except, else, and finally blocks.

Here's a basic structure of a try and except block:


try:
# Code that might raise an exception
# ...
except SomeExceptionType as e:
# Code to handle the exception
# ...
else:
# Code to execute if no exception occurred in the try block
# ...
finally:
# Code that will be executed no matter what, whether an exception occurred or not
# ...

Here's a simple example where a try and except block is used to catch a specific type of
exception, the ZeroDivisionError:
# Example: Handling division by zero exception

try:
numerator = int(input("Enter the numerator: "))
denominator = int(input("Enter the denominator: "))

result = numerator / denominator

except ZeroDivisionError as e:
print(f"Error: {e}. You cannot divide by zero.")
except ValueError as e:
print(f"Error: {e}. Please enter valid integers.")
else:
print(f"The result of {numerator} divided by {denominator} is: {result}")
finally:
print("This block always executes, regardless of whether an exception occurred or not.")

In this example, the user is prompted to enter a numerator and denominator. The try block
attempts to perform the division operation. If the user enters 0 as the denominator, a
ZeroDivisionError will occur, and the control will transfer to the corresponding except block. If
the user enters a non-integer value, a ValueError will occur, and the corresponding except block
for ValueError will be executed.

The else block will be executed if no exceptions occurred in the try block, and the finally block
will be executed regardless of whether an exception occurred or not.

Q 4: Compare and contrast NumPy arrays with Python lists. Discuss at least three advantages
of using NumPy arrays over lists in numerical computations.

NumPy arrays and Python lists are both used to store and manipulate collections of data, but
they have several key differences. Here are three advantages of using NumPy arrays over
Python lists in numerical computations:

Performance:
NumPy arrays are implemented in C and Fortran, which allows for highly efficient computation.
In contrast, Python lists are more flexible but are not optimized for numerical operations.
NumPy's underlying C implementation enables it to perform operations on large datasets much
faster than equivalent operations in pure Python.
NumPy uses contiguous blocks of memory and avoids the overhead associated with Python
lists, resulting in better cache locality and improved performance.

Conciseness and Readability:

NumPy provides a concise and expressive syntax for numerical operations. Operations on entire
arrays can be performed with a single line of code, making the code more readable and closer
to mathematical notation.
For example, in NumPy, you can perform element-wise multiplication of two arrays with a
simple * operator:
import numpy as np

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


arr2 = np.array([4, 5, 6])
result = arr1 * arr2

The equivalent operation using Python lists would require a loop or a list comprehension,
making the code less concise.
Broadcasting:

NumPy supports broadcasting, which is a powerful mechanism that allows operations between
arrays of different shapes and sizes. When performing operations on arrays with different
shapes, NumPy automatically broadcasts the smaller array to match the shape of the larger
one, eliminating the need for explicit looping.
This feature simplifies code and makes it more readable. In contrast, performing similar
operations with Python lists would require explicit loops or list comprehensions.
In summary, NumPy arrays offer superior performance, concise and readable syntax for
numerical operations, and powerful broadcasting capabilities compared to Python lists. These
advantages make NumPy the preferred choice for numerical computations and data
manipulation in scientific computing, machine learning, and other domains where efficiency is
crucial.

Q 5: What is a regression problem? Write the steps to fit linear regression model to a dataset.
Also mention the data cleaning steps?
A regression problem involves predicting a continuous output variable (also called the
dependent variable) based on one or more input variables (independent variables). The goal is
to establish a relationship between the input variables and the continuous output variable.
Linear regression is a common method used for regression analysis.
Here are the steps to fit a linear regression model to a dataset:
1. Understand the Problem:
 Clearly define the problem and the variables involved.
 Determine whether a regression analysis is appropriate for the given problem.
2. Collect and Explore Data:
 Collect relevant data for the analysis.
 Explore the dataset to understand its structure, identify missing values, and
examine the relationships between variables.
3. Preprocess Data:
 Handle missing data: Impute missing values or remove rows/columns with
missing data.
 Encode categorical variables: Convert categorical variables into numerical
representations, if necessary.
 Handle outliers: Identify and address outliers that might affect the model's
performance.
 Scale features: Normalize or standardize numerical features to ensure they have
similar scales.
4. Split the Data:
 Divide the dataset into training and testing sets. The training set is used to train
the model, while the testing set is used to evaluate its performance.
5. Choose a Model:
 Select the appropriate regression model. For linear regression, the goal is to fit a
linear equation to the data.
6. Train the Model:
 Use the training dataset to train the linear regression model.
 The model learns the coefficients that define the linear relationship between the
input variables and the output variable.
7. Evaluate the Model:
 Use the testing dataset to evaluate the model's performance.
 Common evaluation metrics for regression include mean squared error (MSE),
root mean squared error (RMSE), and R-squared.
8. Tune Hyperparameters (if necessary):
 If the model has hyperparameters, tune them to improve performance. For
linear regression, there are usually not many hyperparameters to tune.
9. Make Predictions:
 Use the trained model to make predictions on new or unseen data.
10. Interpret the Results:
 Analyze the coefficients and their significance to understand the impact of each variable
on the predicted outcome.
Data cleaning steps may include:
 Handling Missing Values:
 Remove or impute missing values in the dataset.
 Encoding Categorical Variables:
 Convert categorical variables into a numerical format using techniques like one-
hot encoding.
 Outlier Detection and Removal:
 Identify and handle outliers that could skew the model's predictions.
 Scaling Features:
 Normalize or standardize numerical features to ensure they have similar scales.
 Dealing with Duplicates:
 Identify and remove any duplicate records in the dataset.
 Handling Skewed Data:
 Address skewed distributions through techniques like log transformation.
 Addressing Data Integrity Issues:
 Check for data integrity issues, such as typos or inconsistent entries.
Remember that the specific steps may vary depending on the characteristics of the dataset and
the requirements of the problem at hand.

You might also like