Thursday, May 2, 2024
BCA PART B 11. Create DataFramefrom Excel sheet using Pandas and Perform Operations on DataFrames
BCA PART B 10. Create Array using NumPy and Perform Operations on Array
BCA PART B 9. Drawing Histogram and Pie chart using Matplotlib
BCA PART B 8. Demonstrate usage of advance regular expression
BCA PART B 7. Demonstrate Exceptions in Python
a=int(input('Enter value of a: '))
BCA PART B 6. Create a GUI using Tkinter module
BCA PART B 5. Create SQLite Database and Perform Operations on Tables
BCA PART B 4. Demonstrate use of Dictionary
BCA PART B 3. Demonstrate use of List
BCA PART B 2. Demonstrate usage of advance regular expression
BCA PART B 1. Demonstrate usage of basic regular expression
Sunday, April 14, 2024
BCA PART A 10. Read and write into a file
In this program, two functions are defined:
write_to_file:- Takes a filename and data as input.
- Opens the file in write mode (
'w'), which either creates the file if it doesn't exist or truncates the file if it already exists. - Writes the data to the file.
- Closes the file automatically when the
withblock exits.
read_from_file:- Takes a filename as input.
- Opens the file in read mode (
'r'). - Reads the data from the file.
- Closes the file automatically when the
withblock exits. - Returns the data read from the file.
In the example usage section of the program:
- The user is prompted to enter the filename.
- The user chooses to either write to the file or read from it.
- Depending on the user's choice:
- For the write operation, the program prompts the user to enter the data to write to the file and then calls the
write_to_filefunction. - For the read operation, the program calls the
read_from_filefunction and prints the data read from the file.
- For the write operation, the program prompts the user to enter the data to write to the file and then calls the
BCA PART A 9. Implement Stack
A stack is a data structure that follows the Last-In-First-Out (LIFO) principle. You can implement a stack in Python without using the stack module by using a list to store the stack elements. Below is a program that demonstrates how to create a stack class, implement its methods, and interact with the stack:
The program provided implements a stack data structure in Python using a list to store stack elements. Let's go through each part of the program to understand how it works:
Stack Class
The Stack class defines the stack data structure and its methods.
__init__: This is the class constructor method. It initializes an empty liststackthat will hold the stack elements.is_empty: This method returnsTrueif the stack is empty (i.e., the list has length zero) andFalseotherwise.push: This method takes an item as an argument and adds it to the top of the stack using theappendmethod of the list. It also prints a message indicating that the item was pushed onto the stack.pop: This method removes and returns the item from the top of the stack. If the stack is empty, it prints a message indicating that the stack is empty and returnsNone. Otherwise, it uses thepopmethod of the list to remove the last element and prints a message showing which item was popped.peek: This method returns the item from the top of the stack without removing it. If the stack is empty, it prints a message indicating that the stack is empty and returnsNone. Otherwise, it returns the last element in the list.display: This method displays the current contents of the stack by printing the list.
Example Usage
The example usage section of the program demonstrates how to use the Stack class:
It first creates an instance of the
Stackclass calledmy_stack.It then enters an infinite loop that provides a menu of stack operations that the user can perform: push, pop, peek, display, and exit.
Depending on the user's choice, the program performs the corresponding operation on the stack.
Push: The program prompts the user to enter an item and then pushes that item onto the stack using the
pushmethod.Pop: The program calls the
popmethod to remove the top item from the stack and prints a message indicating which item was popped.Peek: The program calls the
peekmethod to view the top item of the stack and prints it.Display: The program calls the
displaymethod to show the current contents of the stack.Exit: The program breaks out of the loop and ends the program.
The program continues to loop, allowing the user to perform multiple operations on the stack, until the user chooses to exit the program.