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

Python Assignment

Uploaded by

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

Python Assignment

Uploaded by

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

PYTHON Programming Assignment

1. You are managing a student club and need to keep track of members and their activities.
The user will input the details of each member, including their name, age, and the
activity they participate in (e.g., Music, Sports, Art). You need to:
a. Create a set of unique activities.
b. Count how many students are enrolled in each activity.
c. Display the names of students grouped by their activity.
d. Allow the user to enter member details until they decide to stop.

2. The following table shows the sale details of various laptop computers over a week @
‘Ranchi Computers’

Company Monday Tuesday Wednesday Thursday Friday


HP 2 1 0 3 4
Lenovo 3 4 5 0 2
Asus 4 3 2 1 0
Dell 1 0 3 4 2

Prices of the laptops are 40000, 45000, 60000 and 35000 respectively
Write a python script to:
a. Construct a NumPy array of the given data
b. Determine the total sale of computers on each day and append it to the array
c. Find the revenue received for each manufacturer and append it to the array.
d. Construct a data frame of the final NumPy array with proper row index and
column label

3. Use the random module of numpy to generate the sales value randomly between 0 to 6
for four companies in the ques 2 (HP, Lenovo, Asus, Dell) for 5 weekdays and perform
following tasks:

a. Create Pandas DataFrame


b. Calculate the total sales for each day and add it as a new row in the DataFrame
c. Calculate the total revenue for each manufacturer (using respective prices:
₹40,000, ₹45,000, ₹55,000, and ₹35,000) and add it as a new column.
d. Save the DataFrame to a CSV file, read it back, and display the name of the
company with the best sales performance [use of idxmax() is prohibited] [Hint:]
Select the req column by label and convert to list using tolist() method.

4. Construct two numpy 2D arrays A1 and A2 of any shape set by the user at run time and
the elements are randomly generated by the system. Perform the following steps
a. Construct an array A11 by selecting all the elements of the first array (A1) that
are divisible by 3 but not divisible by 4
b. Construct another array A22 by selecting all the elements of the second array
(A2) that are divisible by 2 and 3
c. Check whether A11 and A22 are empty. Continue the process if anyone array is
empty
d. Join 1 and 2 to form a single dimensional array and construct the largest possible
square matrix from it
5. Write a Python program that takes a start and end number as input and creates a NumPy
array of numbers in this range incremented by 1. Form a square matrix B from this
array. From B, extract two new arrays, B1 and B2, where B1 contains all prime numbers
and B2 contains non-prime numbers. Finally, create two diagonal matrices B11 and
B22, where the elements of B1 and B2 are placed on the diagonals, respectively, and
the other elements are set to zero.

6. Write a python script to create a NumPy array, receiving start and end numbers from
user. The array should contain elements from start to two times end, incremented by 2.
Create a square matrix M from this input array. From M, using conditional logic, create
two new arrays A1 and A2, consisting of elements which are divisible by a non-zero
number ‘n’ (take input from user), in the first array and not divisible by ‘n’ in the second
array. Create two arrays A11 and A22 where the elements of A1 and A2 respectively
will be the diagonal elements and the remaining array elements will be zeros.

7. Write a program to construct a user defined function sort_list() to sort the strings present
in the list based on their length.

8. Write a program to construct a user-defined function unique_elements() that takes two


sets as input and returns a new set containing elements that are unique to each set (i.e.,
elements that are only in one of the sets and not in both).

9. Create a module with name my_module to incorporate the following functions.


(minimum use of built-in functions)
a. My_fact(): To find the factorial of all numbers that are passed in the function
call. Function shall return a list of integers. (Number of elements that are passed
must be of variable length)
b. My_psq(): To find the perfect square numbers that are passed in the function
call. The function shall return a list of integers. (Number of elements that are
passed must be of variable length)
c. Call both the functions from the main program and try the function call for list,
set or tuple as its argument

10. Create three lists using list comprehension for 10 students

Names:
Roll No:
Mid Sem Marks for Python

Perform following tasks:

a) Construct a list of tuples by defining your own function my_zip


b) Function shall check for homogeneity of all lists.
c) Define a function my_sort for sorting the list of tuples
d) Function shall provide keyword argument to specify parameter of sorting and
to specify the type of sorting (ascending or descending)
e) Create a data frame of the given details,
f) Add two new columns Assignment and End Sem Marks. (Generate random
marks)
g) Find the total of mid sem, end sem and assignment
h) Add the total as a new column
i) Convert the data frame into a csv file

Theoretical Assignment
1. What is a lambda function in Python? How does it differ from a regular function?
Provide an example.
2. What is the purpose of the filter() function in Python? Can filter() and map() be used
together? Illustrate with an example.
3. What are shallow copy and deep copy in Python? Explain with examples.
4. Discuss the significance of file handling in Python. Differentiate between text files and
binary files, highlighting their use cases. Explain the ways how you can make python
auto manage resources related to file handling.
5. A novice Python developer has created a program that involves repeatedly using the
same set of Python statements in multiple parts of the code. As a Python developer with
six months of experience, how would you improve the program's structure to make it
more organized and reusable? Describe the approach you would take to achieve
reusability and explain the key components of this technique. Additionally, demonstrate
how to pass values (arguments) to the reusable code and return results for use in the rest
of the program. Provide examples to support your explanation.
6. Which NumPy method would you prefer to generate fixed amount of evenly spaced
data points between two numbers: arrange or linspace? Why and How?
7. Write a python function that accepts variable number of positional and keyword
arguments and returns the total number of positional and keyword arguments received.

8. Python allows certain built-in operators like +, -, *, and / to behave differently


depending on the type of the operands. For instance, the + operator can add numbers or
concatenate strings. How can you make such operators work in a specific way for your
custom objects? Explain the process with an example of implementing the * operator
for objects representing fractions.
9. Why NumPy?
10. Consider dictionaries with scalar values, container-type values, and nested dictionaries.
How are these different types handled when converted into data frames?

You might also like