Python Assignment
Python 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’
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:
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.
Names:
Roll No:
Mid Sem Marks for Python
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.