Professional Documents
Culture Documents
Python Assignment Report1
Python Assignment Report1
BELAGAVI
Report on:
CONTENT BEYOND SYLLABUS
SUBJECT:
APPLICATION DEVELOPMENT USING PYTHON (18CS55)
Mini Project Title
PYTHON STACK IMPLEMENTATION
Faculty In-Charge
Vinutha Prashanth
Assistant Professor,
CSE Department, RYMEC
CERTIFICATE
INTRODUCTION:
The Stack in data structure is a linear data structure that accompanies a principle
known as LIFO (Last In First Out) or FILO ( First In Last Out). Real-life examples of a stack are
a deck of cards, piles of books, piles of money, and Many more.
This example allows you to perform operations from one end only, like when you insert and
remove new books from the top of the stack. It means insertion and deletion in the stack
data structure can be done only from the top of the stack. You can access only the top of the
stack at any given point in time.
We can only see the top, i.e., the top-most book, namely 40, which is kept top of the stack.
If we want to insert a new book first, namely 50, we must update the top and then insert a
new text.
And if we want to access any other book other than the topmost book that is 40, you first
remove the topmost book from the stack, and then the top will point to the next topmost
book.
Push Operation:
Push operation involves inserting new elements in the stack. Since you have only one end to
insert a unique element on top of the stack, it inserts the new element at the top of the
stack.
Step 5: Success
Pop Operation:
Pop operation refers to removing the element from the stack again since you have only one
end to do all top of the stack. So removing an element from the top of the stack is termed
pop operation.
Step 5: Success
Peek Operation:
Peek operation refers to retrieving the topmost element in the stack without removing it
from the collections of data elements.
isFull():
isFull function is used to check whether or not a stack is empty.
isEmpty():
isEmpty function is used to check whether or not a stack is empty.
• Tkinter − Tkinter is the Python interface to the Tk GUI toolkit shipped with Python.
We would look this option in this chapter.
• wxPython − This is an open-source Python interface for
wxWindows http://wxpython.org.
• JPython − JPython is a Python port for Java which gives Python scripts seamless
access to Java class libraries on the local machine http://www.jython.org.
In this, animation of the working of Stack is achieved by using Tkinter module .
Tkinter Programming:
Tkinter is the standard GUI library for Python. Python when combined with Tkinter
provides a fast and easy way to create GUI applications. Tkinter provides a powerful
object-oriented interface to the Tk GUI toolkit.
Creating a GUI application using Tkinter is an easy task. All you need to do is perform the
following steps −
• Enter the main event loop to take action against each event triggered by the user.
Example
#!/usr/bin/python
import Tkinter
top = Tkinter.Tk()
# Code to add widgets will go here...
top.mainloop()
This would create a following window −
➢ Source code:
#Stack animation
from tkinter import *
import time
class StackVisualizer:
self.window = root
self.window.config(bg="gray")
self.last_label_value_keep = []
self.block_value_counter = []
self.block_make = 0
self.final_set_block = 0
self.extra_decrease = 0
#Label,Button,Entry
self.heading_name = None
self.sub_heading = None
self.stack_indicator = None
self.push_btn = None
self.pop_btn = None
self.element_take_entry = None
self.entry_number = None
self.element_take_label = None
self.add_btn = None
self.top_index = None
self.index_neg = None
self.index_0 = None
self.index_1 = None
self.index_2 = None
self.index_3 = None
self.index_4 = None
self.index_5 = None
self.number = IntVar()
self.value_entry = IntVar()
self.value_entry.set(" ")
#Default
self.canvas_width = 600
self.canvas_height = 500
self.number_set_x = 40
self.number_set_y = 105
self.block_left = 26
self.block_right = 72
self.block_up = 100
self.block_down = 130
self.down_achieve = 400
self.top_y = 400
self.stack_canvas.pack(fill=BOTH)
self.make_stack_container()
self.make_buttons()
self.heading_and_sub_heading()
self.set_index()
def heading_and_sub_heading(self):
self.heading_name.place(x=140,y=30)
self.sub_heading = Label(self.stack_canvas, text="Index number", bg="purple",
fg="#99ff33", font=("Helvetica",20,"bold","italic"))
self.sub_heading.place(x=20,y=300)
self.stack_indicator.place(x=180,y=450)
def make_buttons(self):
self.push_btn =
Button(self.window,text="PUSH",fg="red",bg="black",font=("Arial",15,"bold"),
relief=RAISED,bd=7,command=self.push_element)
self.push_btn.place(x=30,y=535)
self.pop_btn.place(x=500, y=535)
self.top_index.place(x=310, y=self.top_y)
#stack container
def make_stack_container(self):
self.stack_canvas.create_line(250,198,250,400,fill="yellow",width=4)
self.stack_canvas.create_line(250,400,300,400,fill="yellow",width=4)
#Index
def set_index(self):
self.index_neg = Label(self.stack_canvas,text="-
1",fg="blue",bg="purple",font=("Arial",15,"bold"))
self.index_0.place(x=220, y=365)
self.index_1 = Label(self.stack_canvas, text="1", fg="blue", bg="purple", font=("Arial",
15, "bold"))
self.index_1.place(x=220, y=332)
self.index_2.place(x=220, y=299)
self.index_3.place(x=220, y=266)
self.index_4.place(x=220, y=232)
self.index_5.place(x=220, y=199)
#Push button
def push_element(self):
if len(self.last_label_value_keep) == 6:
messagebox.showerror("Overflow","Stack is full")
else:
self.pop_btn.config(state=DISABLED)
self.push_btn.config(state=DISABLED)
bg="orange",fg="brown",font=("Arial",12,"bold"))
self.element_take_label.place(x=170,y=536)
self.element_take_entry = Entry(self.window,font=("Arial",13,"bold"),bg="white",
fg="blue",relief=SUNKEN,bd=5, textvar=self.value_entry)
self.element_take_entry.place(x=167,y=560)
self.element_take_entry.focus()
self.add_btn = Button(self.window, text="Add", font=("Arial", 10, "bold"), bg="blue",
fg="red", relief=RAISED, bd=3, padx=3, pady=3, command=lambda:
self.make_block('<Return>'))
self.add_btn.place(x=400, y=560)
self.window.bind('<Return>',self.make_block)
def make_block(self,e):
try:
self.element_take_label.place_forget()
self.element_take_entry.place_forget()
self.add_btn.place_forget()
self.entry_number.place(x=self.number_set_x, y=self.number_set_y)
if type(self.value_entry.get()) == int:
self.number.set(self.value_entry.get())
self.push_data()
except:
self.stack_canvas.delete(self.block_make)
self.entry_number.destroy()
self.value_entry.set(" ")
self.pop_btn.config(state=NORMAL)
self.push_btn.config(state=NORMAL)
pass
def push_data(self):
try:
self.down_achieve -= 28 + self.extra_decrease
self.top_y -= 35
self.top_index.place_forget()
self.top_index.place(x=310, y=self.top_y)
while self.number_set_x<265:
self.stack_canvas.delete(self.block_make)
self.entry_number.place_forget()
self.number_set_x+=2
self.block_left+=2
self.block_right+= 2
self.block_make = self.stack_canvas.create_rectangle(self.block_left,
self.block_up, self.block_right, self.block_down, fill="black", width=2, outline="blue")
self.entry_number.place(x=self.number_set_x, y=self.number_set_y)
self.window.update()
self.stack_canvas.delete(self.block_make)
self.entry_number.place_forget()
self.number_set_y += 2
self.block_up+= 2
self.block_down+= 2
self.block_make = self.stack_canvas.create_rectangle(self.block_left,
self.block_up, self.block_right, self.block_down, fill="black", width=2, outline="blue")
self.entry_number.place(x=self.number_set_x, y=self.number_set_y)
time.sleep(0.01)
self.window.update()
self.reset_with_position_set()
except:
pass
def reset_with_position_set(self):
self.final_set_block = Label(self.window, text=self.value_entry.get(), bg="black",
fg="red",font=("Arial",11,"bold"))
self.final_set_block.place(x=self.number_set_x, y=self.number_set_y)
self.last_label_value_keep.append(self.final_set_block)
self.block_value_counter.append(self.block_make)
self.value_entry.set(" ")
self.entry_number.place_forget()
self.push_btn.config(state=NORMAL)
self.pop_btn.config(state=NORMAL)
self.block_left = 26
self.block_up = 100
self.block_right = 72
self.block_down = 130
self.number_set_x = 40
self.number_set_y = 105
self.extra_decrease =6
def pop_data(self):
if len(self.last_label_value_keep) ==0:
messagebox.showerror("Underflow","Stack is empty")
else:
self.last_label_value_keep.pop().destroy()
self.stack_canvas.delete(self.block_value_counter.pop())
self.top_y += 35
self.top_index.place_forget()
self.top_index.place(x=310, y=self.top_y)
self.down_achieve += 28 + self.extra_decrease
if len(self.last_label_value_keep) == 5:
self.push_btn.config(state=NORMAL)
if __name__ == '__main__':
window= Tk()
window.title("Stack Visualization")
window.geometry("600x600")
window.maxsize(900,900)
window.minsize(900,900)
StackVisualizer(window)
window.mainloop()
SNAPSHOTS:
➢ STACK IMPLEMENTATION
➢ PUSH
➢ SHOWING ERROR, WHEN TRY TO INSERT OTHER THAN INTEGER
➢ STACK FULL
➢ POP
➢ STACK EMPTY
CONCLUSION:
We have learned the implementation, importance, and application of stacks. This is
one of the most important data structures to know and it is extensively asked in the
computer science industry. It is important to have strong knowledge on this topic as it
would give you an edge.
In this, we learn about using of Tkinter module. Python Tkinter is the standard Graphical
User Interface (GUI) that is supported in Python. When Tkinter is used alongside Python, it
results in the creation of GUI very conveniently and quite fast. The main advantage of using
Tkinter is that it has an object-oriented interface. The success of any application or website
depends on how well it interacts with the user, i.e. an attractive graphical interface helps to
create a good application
Reference:
• geekforgeeks: https://www.geeksforgeeks.org/python-gui-tkinter/
• Simplelearn: https://www.simplilearn.com/tutorials/data-structure-tutorial/stacks-
in-data-structures#introduction_to_stack_in_data_structures
THANK YOU