About Lesson
-
Implement a Program to Manage a List of Students
- Objective: Write a Python program to manage a list of students, allowing adding, removing, and displaying student names.
- Steps:
- Create an empty list to store student names.
- Define functions to add, remove, and display students.
- Use a loop to provide a menu for user interaction.
python
students = [] def add_student(name): students.append(name) def remove_student(name): if name in students: students.remove(name) def display_students(): for student in students: print(student) while True: print("1. Add Student") print("2. Remove Student") print("3. Display Students") print("4. Exit") choice = input("Enter your choice: ") if choice == '1': name = input("Enter student name: ") add_student(name) elif choice == '2': name = input("Enter student name: ") remove_student(name) elif choice == '3': display_students() elif choice == '4': break else: print("Invalid choice. Please try again.")
Write a Program to Count the Frequency of Words in a Text
- Objective: Write a Python program to count the frequency of each word in a given text.
- Steps:
- Prompt the user to enter a text.
- Split the text into words.
- Use a dictionary to count the frequency of each word.
- Display the word frequencies.
python
text = input("Enter a text: ") words = text.split() word_count = {} for word in words: if word in word_count: word_count[word] += 1 else: word_count[word] = 1 for word, count in word_count.items(): print(f"{word}: {count}")