stack in python
How to Implement a Python Stack
Introduction to
stacks and Queue data structures, which organized computer storage for
efficient access and manipulation of data.
The stacks and queue
are very simple to learn and execute with ease, and their usage is very common.
Stacks follows the
LIFO principle that is last in first out. The last element will be removed or
pop first.
Two operations are
therein, one is pushed, which adds an element to the top of the stack, and the second one is pop, which removes the element at the top of the stack.
So let us begin with implementing stacks, using lists in python. Let us first create an empty list name[].
We are going to add elements to the
list, name [] using the append () method.
# create an empty list name
name=[]
# add elements into the list with
append method in the list as Stack
name.append('Siraz')
name.append('Dhoni')
name.append('Shami')
name.append('Kholi')
name.append('Rahul')
name.append('Bumrah')
print(name)
Adding elements to the list using the append() method ensures the push operation of the stack.
Now let us try removing elements
from the list.
name.pop()
It will delete the Bumrah as it is
entered last
['Siraz', 'Dhoni', 'Shami', 'Kholi',
'Rahul']
Let us try to delete the item again
from the list for which we have to write the same syntax
name.pop()
The above code deletes Rahul from
the list, and we are left with only
['Siraz', 'Dhoni', 'Shami', 'Kholi']
Stack using deque in python
Implementing push with the help of
append() method of the deque, We have to import deque from python collection
library as follows
# We can initialize a deque with a
list
name = deque()
# Use append like before to add
elements
name.append ('Siraz')
name.append ('Dhoni')
name.append ('Shami')
name.append ('Kholi')
name.append ('Rahul')
name.append ('Bumrah')
so when we print the print(name), it will
print as follows
deque(['Siraz', 'Dhoni', 'Shami',
'Kholi', 'Rahul', 'Bumrah'])
Now using pop() of deque, we can
implement stack pop operation as follows
name.pop()
print (name)
deque(['Siraz', 'Dhoni', 'Shami',
'Kholi', 'Rahul'])
The last element is Bumrah is deleted
first
name.pop()
print (name)
deque(['Siraz', 'Dhoni', 'Shami',
'Kholi'])
The second last element Rahul is
removed second and so on.
Implementation using python queue module
In the Python queue module there is a LIFO
Queue, which is basically nothing but Stack. Element is inserted into the queue
using put() method that act as PUSH and get () remove data out from the queue
that act as POP operation of stack.
from queue import LifoQueue
# Initializing the stack with
maxsize is 10
stack = LifoQueue(maxsize = 10)
# qsize() show the number of
elements
# in the stack
print(“Initial size of ”stack.qsize())
# put() function to push
# element in the stack
stack.put ('Siraz')
stack.put ('Dhoni')
stack.put ('Shami')
stack.put ('Kholi')
stack.put ('Rahul')
stack.put ('Bumrah')
print("Full is over or not :
", stack.full())
print("Total size of the stack:
", stack.qsize())
# This get() method to act as pop
operation of stack in
# LIFO order that is (Last in First
Out)
print ('\nPoped operation from the
stack')
print (stack.get())
print (stack.get())
print (stack.get())
print("\nStack Empty or not :
", stack.empty())
Output
Initial size of 0
Full: False
Size: 6
Elements poped from the stack
Bumrah
Rahul
Kholi
Empty: False
Python Articles
interview question and answer related to computer language
Q. How many error types are there in computer programming language?
There are three types of
errors in the execution of a computer program. These are:
Logical errors
This can occur due to
wrong implementation of programming logic and these kinds of error very
difficult to remove.
Syntax errors
This can occur due to one
or more grammatical rules of the programming language being used is violated. These kinds of errors are detected during compile time.
Runtime errors
This can occur due to the program contains a wrong operation. For example, dividing a number by 0. These kinds of errors are displayed instantly during the program execution. When these kinds of errors occur at runtime, the program execution is stopped and a diagnostic message is printed on the screen.
Q. What is an algorithm?
An algorithm is a step-by-step process to solve a problem in simple English. It
features are clarity, ease to understand a problem, efficiency, and simplicity.
Q. Define variable.
Variable
is place holder of data. Or we can say it is a storage location in a computer
programming language. Example int a=50; Here a is a variable that assigns a value
50 of data type int.
Q.
What is 3GL?
Third generation language is much more user friendly and
machine-independent than its predecessor 2GL. Examples: C, C++, BASIC, COBOL,
Java
Features
are:
- Easy to write code
- Improved data types
- Language of 3GL are general purpose
- More like the English language (High Level)
- Need Interpretation and Compilation
- Many language support Object-Oriented Programming
Q. What is 4GL?
Fourth-Generation
Programming Language (4GL) allows nonprogrammer user to write programs to
query database and to generate custom reports and it does multiple tasks to be
done on a single command.
4GL is
a set of non-procedural language and high-level programming language. 4GL
reduces programming cost and time. 4GL programs have greater usability and also
classified as table-driven, Report generator, Form generator.
Features
are:
- It is a non-procedural language and high-level programming language.
- It reduces programming cost and time.
- It programs have greater usability.
Q. What is API?
An API
is a group of functions, routines, data structures, object classes, variables,
protocols that can be used to create software or interact with an external system or
we can say an API specifies how software or application components should
interact and work. With the growth of web applications and mobile apps and their
allied services, API becomes the most important part of developers.
Q. What is the Purpose of API?
The main purpose of API is to help the developer to develop a more efficient
application without the bother of developing the completing API. They used
it and do their job. It will automate and integrate with efficient coding and
simplifying the programming.
Q. What is ASCII?
ASCII(American Standard Code
For Information Interchange) is basically a character encoding
standard for electronic communication. ASCII codes represent text in computers,
telecommunications equipment, and other devices. ASCII code is used for
identifying characters and numerals on a keyboard. It is an 8-bit sequence code
and is used for identifying letters, numbers, and special characters.
Q. What is ANSI?
The
American National Standards Institute (ANSI) is a non-profit association that
oversees standards in the US. It also universally accepted in multiple
industries.
ANSI takes
on the responsibility for bringing together representatives from government,
industry, academia, and the public to develop voluntary, consensus standards
aimed at strengthening
Q.
What is IDE?
An IDE (Integrated
Development Environment) is software that is used to write programming code
as an editor and compiler. Examples of some famous IDE are Eclipse, NetBeans, Visual
Studios, IntelliJ, etc. Each IDE has its own characteristics, strengths, and
weaknesses.
Q. What is debugging?
Debugging is a process to find out errors or bugs and then
remove the errors in a program and make the program error-free.
Q. What are reserved words?
Every
language has predefine keyword like for, break, continue, while, do, etc is
called reserved word.
Q. What are loops?
A loop is a repeating syntax of
a programming language. Like for(initial; condition; iteration)
Q How many types of loops are
there?
Two types of the loop are there in
programming language 1. Entry control loop (for( ) and while( )) and 2. Exit
control loop (do.. while())
Q. What is an Array?
An array is a contiguous memory location of a programming language.
Q. What is a Compiler?
It is a translator that
translates source code to machine understanding from a computer programming language.
In this translator, the whole source code translates to object code. Like C, C++
compiler.
Q. What is an Interpreter?
It is also a translator that
translates source code one line at a time. Like GWBasic.
More Java program
Introduction to Java Programming
Introduction to Java Programming
Introduction to Programming
Basic of Programming
Procedure Oriented Programming
Drawbacks Procedure Oriented Programming:
Object-Oriented Programming
What are the Basic Features of java?
- Java is an object-oriented language.
- Java is Easy to write and more readable and eye-catching.
- Java program cannot harm other systems thus making it secure.
- Java programs can be run on any platform (Linux, Window, Mac)
- Java program both compiled and interpreted
- Java provides integrated support for multithreaded programming. It can handle many tasks simultaneously.
- Java was designed with a distributed environment.
- Java has a strong memory allocation and automatic garbage collection mechanism.
- Java uses 16-bit Unicode, instead of 8-bit ASCII code.
What are the advantages of OOP?
What are the Basic components of OOP?
- Class—It is a blueprint of an object.
- Object—An instance of a class or It is a template of a class.
- Encapsulation—The wrapping up of data and method into a single unit called Encapsulation.
- Abstraction—It is the act of representing essential features without including the background details.
- Polymorphism -- polymorphism refers to a programming language's ability to process objects differently depending on their data type or class.
- Inheritance -- Inheritance is the process by which objects of one class acquire the properties of objects of another class.
![]() |
| Java Virtual Machine Architecture |



