0% found this document useful (0 votes)
8 views2 pages

Week 4 Python Assignment

The document outlines a Python lab assignment for students, requiring them to write programs for various functions including arithmetic operations, palindrome checking, prime number generation, Fibonacci calculation, matrix multiplication, anagram checking, custom sorting, right triangle printing, inventory management, and a student grade management system. Students must save their work with specific naming conventions and upload it as instructed. Additionally, they are encouraged to use the virtual lab at vlab.co.in for practice and coding exercises.

Uploaded by

rishabh.negi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Week 4 Python Assignment

The document outlines a Python lab assignment for students, requiring them to write programs for various functions including arithmetic operations, palindrome checking, prime number generation, Fibonacci calculation, matrix multiplication, anagram checking, custom sorting, right triangle printing, inventory management, and a student grade management system. Students must save their work with specific naming conventions and upload it as instructed. Additionally, they are encouraged to use the virtual lab at vlab.co.in for practice and coding exercises.

Uploaded by

rishabh.negi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON-1 Lab (23B65CA126)

EVEN 2025
Week 4_Assignment 4
Practice Lab
[CO: CBAC156 .2]
Instructions:
1. All students must save all their programs with the nomenclature
(Enroll No_W1_QuestionNo.cpp). Also, store the Output screenshots in a document
as well.
2. Upload them as per the instructions given by your lab faculty.
3. Students are requested to write codes for all questions so that practice for all
related topics can be done.

Note: Please explore the virtual lab at vlab.co.in for interactive programming and
technical exercises. It's a great resource to enhance your understanding and skills.
Concepts: Functions

Lab Questions:

1. Write a Python program that defines separate functions for addition, subtraction,
multiplication, and division. Allow the user to choose an operation and input two
numbers to perform the operation. Use python virtual lab for this question at:
vlab.co.in.
Hint: Use if-elif to match the user’s choice with the corresponding function.

2. Define a function is_palindrome(string) that checks if a given string is a palindrome


(ignoring spaces, case, and punctuation). Test the function with various inputs. Use
python virtual lab for this question at: vlab.co.in.
Hint: Normalize the string using .lower() and replace(). Compare the string with its
reverse using slicing.

3. Write a function generate_primes(n) that returns a list of all prime numbers up to n.


Use helper functions as needed.
Hint: Use a helper function is_prime(num) and loop from 2 to n to filter primes.

4. Implement a function fibonacci(n) that calculates the nth Fibonacci number using
memoization.
Hint: Use a dictionary to store previously calculated Fibonacci values.

5. Write a function matrix_multiply(matrix1, matrix2) that multiplies two 2D matrices.


Include input validation to ensure matrix dimensions are compatible.
Hint: Use nested loops and ensure the number of columns in matrix1 matches the
number of rows in matrix2.

6. Create a function are_anagrams(word1, word2) that checks if two words are


anagrams. Extend the function to handle phrases by ignoring spaces and punctuation.
Hint: Use sorted() on normalized strings (convert to lowercase and remove
spaces/punctuation).

7. Write a function custom_sort(data, key_function) that takes a list of data and a


custom key function, then sorts the list based on the key function. Use this to sort a
list of tuples by the second element in each tuple.
Hint: Use Python’s sorted() with the key argument set to key_function.
8. Write a Python function print_right_triangle(n) that prints a right-angled triangle
pattern of * with n row.
*
**
***
****
*****

Hint:

• Use a for loop to iterate through numbers from 1 to n.


• For each iteration, multiply the * character by the current loop value i.
• Print the result in each iteration to form the triangle pattern.

9. Design a system where you can add, update, delete, and search for items in an
inventory. Use separate functions for each operation. Each item should have
attributes like name, quantity, and price.
Hint: Use a dictionary with the item name as the key and attributes stored in nested
dictionaries or lists.

10. Write a Python program for a Student Grade Management System. Use the
following functions:

1. add_student(name): Adds a new student with an empty list of grades.

o If the student already exists, display a message: "Student already exists!"

2. add_grade(name, grade): Adds a grade to the student’s list.

o If the student doesn’t exist, display a message: "Student not found!"

3. calculate_average(name): Calculates and returns the average grade of the student.

o If the student doesn’t exist, display a message: "Student not found!"

4. display_students(): Displays all students and their grades in a readable format.

o If no students are in the system, display: "No students available!"

Hints:

• Use a dictionary to store student names and their corresponding grades.


• Use a list to store grades for each student.
• Handle edge cases like missing students or empty lists gracefully.

You might also like