GK SCIENTIST

General Knowledge One Stop Source
Menu
  • Home
  • Social Science
    • History
    • Political Science
    • Geography
  • Science
    • Physics
    • Chemistry
    • Biology
  • Chemistry Notes
  • Mathematics
  • Computer
  • Tutorial MySQL
  • Tutorial Python
  • Java Tutorial
  • English Grammar
  • English Essay
  • Indian Anthropology
  • Philosophy
  • Solved Paper
  • UPSC
  • Current Content
    • Current Affairs
    • RSTV News
    • Yojana and Kurukshetra Gist
  • Donate
  • Contact Us

If you are interested in advertising to our audience, submit the advertising enquiry form.

Advertising Enquiry
Computer

Implement Stack in Python

Gk Scientist November 28, 2022 No Comments
Tweet WhatsApp Telegram

Stack in Python:

We can use the PyCharm code editor for this example. If you do not know about it then follow this link- How to install PyCharm for Python and create a program in it.

(1) A stack is a very useful data structure. It is simply a list with one restriction, Last In First Out (LIFO), meaning an element that comes in last goes out first when a value is read from a stack. Stack has two main operations called push and pop. When an element is added on the top of the stack it is called a push function and when an element is removed from the top of the stack, the operation is called a pop function. Other operations include peek and display. The Peek function displays the last element and the display function displays the full stack.

(2) Example of Implementation of Stack in Python:

Python stack Operation - Implement Stack in Python

Now run the code, and you got the following output as shown below.

stack code in python output - Implement Stack in Python

Now press 1 in the output window, in order to use the Push operation. After pressing 1, you got the message “enter any value”. You add any value of your choice. If you want to add another value then again click on 1 and you got the same message “enter any value”. You can add as many values as you want. The output is shown below.

stack push operationin python - Implement Stack in Python

Now the list is created by using the push operation. Now press 2 in the output window, in order to use the Pop operation. After pressing 2, ‘gk’ will be dropped from the list as shown below.

pop operation python - Implement Stack in Python

Now press 3 in the output window, in order to use the Peek operation. It displays the last element in the list as shown below.

peek operation in python - Implement Stack in Python

Now press 4 in the output window, in order to use the Display operation. This operation displays the full stack as shown below. You got only two values in the list because you have dropped the third value already by using the pop operation.

display operation in python - Implement Stack in Python

If you press 5, the process is getting finished.

Note: In the above example, we have performed a step-by-step operation. If you press 2 before 1, then the pop operation comes into play and you got the message “Empty Stack” as we set in the code as shown above because you did not create the list, so there will be no data that is why you got the output “Empty Stack”. The same is apply to all conditions.


Programming Languages
Procedure-Oriented Programming
Modular Programming

Create Table in MySQL
MySQL Insert Query (Single Row and Multiple Row)
MySQL Select Query with Where Clause
Python (programming language)– Wikipedia
Prev Article
Next Article

Related Articles

Create CheckBox in Java
Create CheckBox in Java: We can use the Eclipse IDE …
Gk Scientist January 14, 2023 Computer

Create CheckBox in Java

SQLite in Python
SQLite in Python: We can use the PyCharm code editor …
Gk Scientist December 7, 2022 Computer

SQLite in Python

MySQL Sub Query with Exists and Not Exists
MySQL Sub Query: MySQL Sub Query is a query nested …
Gk Scientist November 18, 2022 Computer

MySQL Sub Query with Exists and Not Exists

Create a Random Number Guessing Game in Python
Create a Random Number Guessing Game in Python: We can …
Gk Scientist December 3, 2022 Computer

Create a Random Number Guessing Game in Python

Tokens in C++
Tokens in C++ Tokens may be defined as the smallest …
Gk Scientist September 12, 2022 Computer

Tokens in C++

Python MySQL Delete Query
Python MySQL Delete Query: (1) Delete query deletes data and …
Gk Scientist December 13, 2022 Computer

Python MySQL Delete Query

Python User-Defined Function
Python User-Defined Function: We can use the PyCharm code editor …
Gk Scientist December 2, 2022 Computer

Python User-Defined Function

Procedure-Oriented Programming
Procedure-Oriented Programming: Procedure-Oriented Programming is the traditional way of programming, …
Gk Scientist August 14, 2022 Computer

Procedure-Oriented Programming

Leave a Reply Cancel Reply

Search




  • Popular
  • Recent




GK SCIENTIST

General Knowledge One Stop Source

Information

  • About Us
  • Terms and Condition, Disclaimer
  • Contact Us

Android Apps

  • IAS App For English Medium Students
  • IAS Basics App For English Medium Students
  • IAS Hindi App For Hindi Medium Students
DMCA.com Protection Status

Popular Tags

Biology (33) Biology Questions (88) Chemistry (57) Computer (213) Current Affairs (4) Current Content (0) Economy (10) English Essay (172) English Grammar (75) English Literature (10) Geography (83) History (259) Indian Anthropology (11) Indian Polity (14) JKAS Mains Question Papers (17) Mathematics (68) Moral Science (7) NCERT & Other Boards Books (25) Philosophy (114) Physics (89) Political Science (132) RS TV News (33) Science (553) Social Anthropology (7) Social Science (17) Solved Paper (47) UPSC (7) UPSC Mains Question Papers (26)

Downloads

  • NCERT Books
  • Old NCERT Books
  • NIOS Books For IAS, SSC, and State PSC Exam
  • Tamil Nadu Board Books: Important For UPSC, SSC, and State PSC Exam
  • Modern Indian and World History Notes For IAS Exam
  • UPSC Topper 2013 Gaurav Agrawal Notes For IAS Preparation
  • UPSC IAS Prelims General Studies – Previous Year Papers
  • UPSC Mains Question Papers

Copyright © 2023 GK SCIENTIST
Theme by MyThemeShop.com& Hosted On Cloudways

Ad Blocker Detected

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Refresh
 

Loading Comments...