0% found this document useful (0 votes)
0 views4 pages

Assignments 2

The document outlines a series of Python programming assignments designed to enhance coding skills through practical tasks. Assignments include creating functions for word frequency counting, managing a contact book, simulating a bank account, and generating Fibonacci sequences, among others. Each task emphasizes the use of dictionaries, error handling, and basic data manipulation techniques.

Uploaded by

cokobo6915
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)
0 views4 pages

Assignments 2

The document outlines a series of Python programming assignments designed to enhance coding skills through practical tasks. Assignments include creating functions for word frequency counting, managing a contact book, simulating a bank account, and generating Fibonacci sequences, among others. Each task emphasizes the use of dictionaries, error handling, and basic data manipulation techniques.

Uploaded by

cokobo6915
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
You are on page 1/ 4

Ye thode hard level ke hai esmei pahle logic laga apne brain use karke agar net pe search

karna hai to inbuilt functions ke baare me kar sakta.


Inbuilt functions programming ko asan karte hai.

Python Assignments

1. Word Frequency Counter:

o Write a function word_frequency(text) that takes a string text as input.

o The function should return a dictionary where keys are unique words (case-
insensitive) from the text, and values are their respective frequencies.

o Ignore punctuation (e.g., periods, commas).

o Example: word_frequency("Hello world, hello Python!") might return {'hello':


2, 'world': 1, 'python': 1}.

2. Contact Book Manager:

o Create a program that allows a user to manage a simple contact book.

o Use a dictionary where keys are contact names (strings) and values are
dictionaries containing "phone" (string) and "email" (string).

o Implement functions for:

▪ add_contact(name, phone, email)

▪ view_contact(name) (displays details or "Contact not found")

▪ list_all_contacts() (displays all contacts in a readable format)

▪ delete_contact(name)

o Provide a simple menu-driven interface for the user.

3. Inventory Management System:

o Develop a basic inventory system. Use a dictionary where keys are item
names (strings) and values are dictionaries containing "quantity" (integer) and
"price" (float).

o Implement functions for:

▪ add_item(name, quantity, price) (adds a new item or updates an


existing one)

▪ update_quantity(name, new_quantity)
▪ calculate_total_value() (sums up the value of all items in inventory:
quantity * price)

▪ display_inventory()

o Include error handling (e.g., trying to update a non-existent item).

4. Simple Bank Account Simulation:

o Create a class (or functions operating on a dictionary) to simulate a bank


account.

o The account should have an account_number (string), balance (float), and


owner_name (string).

o Implement functions for:

▪ deposit(amount)

▪ withdraw(amount) (handle insufficient funds)

▪ get_balance()

▪ get_account_details()

o Ensure that amounts are positive.

5. Palindrome Checker:

o Write a function is_palindrome(text) that takes a string and returns True if it's
a palindrome (reads the same forwards and backward, ignoring case and
spaces), False otherwise.

o Example: is_palindrome("Racecar") should return True, is_palindrome("A


man a plan a canal Panama") should return True.

6. Fibonacci Sequence Generator:

o Write a function generate_fibonacci(n) that generates the first n Fibonacci


numbers.

o Return them as a list.

o The Fibonacci sequence starts with 0 and 1, and each subsequent number is
the sum of the two preceding ones (0, 1, 1, 2, 3, 5, 8...).

o Handle cases where n is 0 or 1.

7. List of Dictionaries Sorting:

o Given a list of dictionaries, where each dictionary represents a person with


keys like name, age, and city.
o Write a function sort_people(people_list, key_to_sort_by) that sorts the list
of dictionaries based on the value of a specified key (e.g., 'age' or 'name').

o Example input: [{'name': 'Alice', 'age': 30}, {'name': 'Bob', 'age': 25}]

8. Prime Number Checker and Generator:

o Write two functions:

▪ is_prime(number): Returns True if the number is prime, False


otherwise.

▪ generate_primes_up_to(limit): Returns a list of all prime numbers up


to a given limit.

9. Basic Data Analysis - Average and Max/Min:

o Given a list of numbers, write functions to calculate:

▪ calculate_average(numbers)

▪ find_max(numbers)

▪ find_min(numbers)

o Handle empty lists appropriately (e.g., return None or raise an error).

10. Tuple of Student Grades:

o Represent student data as a list of tuples, where each tuple is (student_name,


[list_of_grades]).

o Write functions to:

▪ calculate_student_average(student_data) (returns a dictionary:


{'student_name': average_grade})

▪ find_top_student(student_data) (returns the name of the student


with the highest average grade)

11. Dictionary Merger:

o Write a function merge_dictionaries(dict1, dict2) that takes two dictionaries


and merges them into a new dictionary.

o If a key exists in both dictionaries, the value from dict2 should override the
value from dict1.

o Example: merge_dictionaries({'a': 1, 'b': 2}, {'b': 3, 'c': 4}) should return {'a': 1,
'b': 3, 'c': 4}.
12. Remove Duplicates from a List (Preserving Order):

o Write a function remove_duplicates(input_list) that takes a list and returns a


new list with all duplicate elements removed, while preserving the original
order of the remaining elements.

o Hint: Consider using a set for efficient tracking of seen elements.

13. Simple Tic-Tac-Toe Board (Representation & Display):

o Represent a Tic-Tac-Toe board using a 2D list (a list of lists) of 3x3. Initially, it


should contain empty strings or spaces.

o Write a function display_board(board) that prints the current state of the


board in a user-friendly format (e.g., using lines to separate cells).

o Write a function place_marker(board, player_marker, row, col) that places an


'X' or 'O' at a specified row and col if the spot is empty. Return True on
success, False otherwise.

14. Character Counter (Excluding Spaces/Punctuation):

o Write a function count_characters(text) that takes a string and returns a


dictionary.

o Keys should be characters (case-insensitive), and values should be their


counts.

o Ignore spaces, digits, and punctuation. Only count alphabetic characters.

o Example: count_characters("Hello World!") might return {'h': 1, 'e': 1, 'l': 3,


'o': 2, 'w': 1, 'r': 1, 'd': 1}.

15. Basic Password Generator:

o Write a function generate_password(length, include_digits=True,


include_symbols=True) that generates a random password.

o The password should be of the specified length.

o Allow optional inclusion of digits and symbols. By default, it should include


letters (uppercase and lowercase).

o Use the random module.

You might also like