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

Assignment-Python

Contains tasks and code regarding electrical engineering

Uploaded by

Faizan Ali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Assignment-Python

Contains tasks and code regarding electrical engineering

Uploaded by

Faizan Ali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Important Instructions:

Submit a .py file: Write and test your Python code for each question. Submit your solutions as
a .py file (Python script). Ensure your code is well-commented, clear, and follows Pythonic
standards.

Submit a Report: Along with the .py file, you must also submit a written report. The report
should explain:
 The logic behind your code.
 Step-by-step working of the code for each question.
 How the code solves the problem.
 Any assumptions or observations you made while solving the question.
 If you encountered any challenges, how you overcame them.
 The report can be submitted as a PDF File

Code Structure:
 Ensure your code is clean and follows proper indentation.
 Avoid hardcoding values unless required. Make your code dynamic and reusable.
 Use appropriate variable names that are meaningful and self-descriptive.

Comments and Documentation:


 Write comments in your code to explain what each part of the code is doing.
 Include a brief comment at the top of each Python file explaining its purpose and any
important details.
 The report should complement the code by explaining the flow of the program.

Naming Conventions:
 Name your Python files according to the question they correspond to (e.g.,
question_1.py, question_2.py, etc.).
 The report file should be named like roll_number_report.docx (e.g., 123_report.docx).

Strict Warnings
Late Submission: Any assignment submitted after the deadline will receive a penalty of 10% per
day. Submissions later than 3 days past the due date will not be accepted under any
circumstances.

Plagiarism: Any form of plagiarism or copying from another student, the internet, or any other
sources will result in an automatic zero for the assignment. We have plagiarism detection
systems in place. If you are unsure about the guidelines, please ask for clarification before
submitting.

Code Accuracy: The code must run without errors. Submitting code that does not execute or
throws errors will result in zero marks for that question. Ensure you test your code thoroughly
before submission.

Adherence to Instructions: Ensure you follow all instructions carefully. Failure to follow the
instructions, including not submitting both the Python file and the report, will lead to a reduced
grade.

Independent Work: This assignment is to be done individually. Collaboration or sharing code is


strictly prohibited. You are expected to submit your own work.

Version Control: Before submitting, make sure your code is in its final version. Submitting a
draft or incomplete code will result in a loss of marks. Review your code and report carefully.

External Libraries: If you use external libraries (like numpy, matplotlib, pandas, etc.), make sure
to mention them in your report and install them using pip in the Python environment. Do not
include unnecessary or unused libraries.
How to Submit
Step 1: Write your Python code for each question in separate .py files.
Step 2: Write a report as described above.
Step 3: Compress your .py files and report into a single ZIP file for submission.
Step 4: Submit the ZIP file via the assigned submission portal.
Please note that any deviation from the above instructions will impact your grade. Pay close
attention to detail and follow the rules strictly.

Question 1: Variables and Data Types


Define a variable x using your roll number. Write a Python program to determine whether x is odd or
even. If your roll number ends in an odd digit, add 5 to x before checking. If it ends in an even digit,
subtract 5 from x before checking.

Question 2: Conditional Statements


 Write a Python program to check if your roll number is divisible by:
 3 (if your roll number ends in 0, 1, or 2),
 5 (if it ends in 3, 4, or 5),
 or both (if it ends in 6, 7, 8, or 9).

Question 3: Loops
Write a Python program that prints all prime numbers from 1 to your roll number. If your roll number is
odd, use a while loop. If it’s even, use a for loop.

Question 4: Functions
Define a function factorial(n) to calculate the factorial of a number. Use this function to calculate the
factorial of the last digit of your roll number.

Question 5: Lists

Create a list of the first 10 multiples of your roll number. Write a program to:

 Reverse the list if the roll number is even.


 Sort the list in ascending order if the roll number is odd.

Question 6: Dictionaries
Create a dictionary where keys are digits from 1 to 5, and values are their cubes. Write a program to:

Add a key-value pair where the key is the last digit of your roll number, and the value is its square.

Question 7: Strings
Write a Python program to generate a string by repeating your roll number’s digits (as characters) 5
times. Then:

 Count the occurrence of each digit in the string if your roll number is odd.
 Reverse the string if your roll number is even.

Question 8: File Handling


Write a Python program that:

 Creates a file named Roll_<YourRollNumber>.txt.


 Writes your roll number and its square into the file.
 Reads and prints the content of the file.

Question 9: Classes and Objects


Define a class Student with:

 Attributes: roll_number (int), name (str).


 A method is_prime() to check if the roll_number is a prime number. Instantiate the class with
your roll number and name and call is_prime().

Question 10: Exception Handling


Write a Python program that:

 Accepts two numbers as input: the first two digits of your roll number and the last digit.
 Divides the first number by the second.
 Handles any exceptions if the second digit is 0.

Question 11: Object-Oriented Programming - Class and Object Creation


Write a Python class Rectangle that has:

 A constructor to initialize length and width of the rectangle.


 A method area() that returns the area of the rectangle.
 A method perimeter() that returns the perimeter of the rectangle.
 If your roll number ends in an odd digit, create a rectangle where the length is the last digit of
your roll number and the width is the sum of the digits of your roll number. Otherwise, use the
last two digits of your roll number for length and width.

Question 12: Object-Oriented Programming - Inheritance


Create a class Person with attributes name and age. Then, create a subclass Employee that inherits from
Person and has an additional attribute employee_id. Write methods to display the name, age, and
employee ID.

If your roll number ends in an odd digit, display all details for an employee with the name John, age 30,
and employee ID as the first digit of your roll number. If the roll number ends in an even digit, use the
last two digits as employee ID.

Question 13: Plotting Data in Python - Basic Plot


Using the matplotlib library, plot a simple line graph of the following:

 The X-axis: A list of numbers from 1 to your roll number.


 The Y-axis: The square of each number on the X-axis.
 If your roll number ends in an odd digit, use a dashed line. If it ends in an even digit, use a solid
line.

Question 14: Plotting Data in Python - Scatter Plot


Generate a scatter plot using matplotlib where:

 X values are the first n integers (n being your roll number).


 Y values are the corresponding squares of these integers.
 Add labels for both axes and a title. If your roll number ends in an odd digit, choose red as the
color for the points, and if it ends in an even digit, use blue.

Question 15: Data Science - Numpy Operations


Write a Python program using numpy to perform the following:

 Create an array of n random integers (where n is your roll number).


 Compute and display the mean, median, and standard deviation of the array.
 If your roll number ends in an odd digit, round the values of mean and median to two decimal
places.

Question 16: Data Science - Data Cleaning


 Write a Python program using pandas to clean a sample dataset.
 Create a DataFrame with missing values (e.g., a column with NaN values).
 Use an appropriate method to fill or drop the missing values.
 If your roll number ends in an odd digit, fill missing values with the mean of the column;
otherwise, drop the rows containing NaN.

Question 17: Signal Manipulation - Fourier Transform


 Write a Python program that uses the numpy library to generate a simple signal (e.g., a sine
wave), then compute and plot its Fast Fourier Transform (FFT) using matplotlib.
 The frequency of the sine wave should be equal to the last digit of your roll number. If your roll
number ends in an odd digit, use a sine wave with amplitude 1; if even, use amplitude 0.5.

Question 18: Signal Processing - Filtering a Signal


 Generate a noisy sine wave signal and apply a low-pass filter to it. The scipy library can be used
for the filtering.
 Use a cutoff frequency based on the last two digits of your roll number.
 Plot both the noisy signal and the filtered signal.
 If your roll number ends in an even digit, use a Gaussian filter; if odd, use a median filter.

Question 19: Signal Manipulation - Sampling Rate


 Create a Python program that generates a sine wave and simulates downsampling by reducing
its sampling rate by half. Plot both the original and downsampled signals using matplotlib.
 Use the last digit of your roll number to define the frequency of the sine wave. If your roll
number ends in an odd digit, use a frequency of 100 Hz; otherwise, use 50 Hz.

Question 20: Data Science - Linear Regression


 Using the scikit-learn library, implement a simple linear regression model.
 Use a dataset with n data points, where n is your roll number.
 Perform a linear regression on the dataset and plot the best fit line.
 If your roll number ends in an odd digit, generate random data for X and Y; if it ends in an even
digit, use a basic arithmetic progression for X and generate corresponding Y values with a linear
relationship.

Question 21: Data Manipulation with Pandas


You are given a dataset data.csv with columns: Name, Age, Height, Weight. Load the dataset using
pandas, and write a Python program to:

 Filter the data to show only individuals older than 30.


 Calculate the average age, height, and weight of the dataset.
 If your roll number ends in an even digit, also include a step to sort the data by Height in
descending order.

Question 22: Data Visualization - Histogram


 Using matplotlib, create a histogram of the Age column from the dataset provided in Question
21.
 Set the number of bins to 10.
 If your roll number ends in an odd digit, change the color of the histogram to blue. If it ends in
an even digit, make the histogram color red.

Question 23: Data Cleaning with Pandas


Write a Python function that performs the following tasks on a dataset:

 Removes any rows with missing values.


 Converts the Age column to integers if it's stored as floats.
 Use a dataset with columns: ID, Age, Name. If your roll number ends in 0, 1, or 2, add an
additional column called Salary with some NaN values and clean it as well.

Question 24: Correlation in Data Science


 Given a dataset with columns Height, Weight, and Age, calculate the Pearson correlation
coefficient between Height and Weight.
 If your roll number ends in an even digit, also calculate the correlation between Age and Weight.

Question 25: Time Series Analysis


You are given a time series dataset temperature_data.csv with columns Date and Temperature. Perform
the following tasks:

 Convert the Date column to datetime format.


 Plot the temperature trend over time.
 If your roll number ends in an odd digit, perform a simple moving average (SMA) calculation
with a window size of 7.

You might also like