Computer Science

Stack in Python Class 12 CBSE (083): Complete Notes with Push, Pop and List Implementation

Class 12 · Computer Science

Stack Data Structure in Python (CBSE Class 12 Computer Science - 083)

A Stack is a linear data structure in which insertion and deletion of elements take place from only one end, called the TOP. A stack follows the LIFO (Last In, First Out) principle, which means the element inserted last is removed first.

Learning Objectives

  • Understand the Stack data structure.
  • Learn the LIFO principle.
  • Understand Push and Pop operations.
  • Implement a Stack using Python lists.
  • Learn real-life applications of Stack.

What is a Stack?

A Stack is a collection of elements arranged in a specific order where insertion and deletion are allowed only from one end called the TOP.

Definition: A Stack is a linear data structure that follows the Last In, First Out (LIFO) principle.

LIFO (Last In, First Out)

In a Stack, the last element inserted is the first element to be removed.

Example

If the elements are inserted in the following order:


10
20
30
40

The Stack will look like:


TOP → 40
       30
       20
       10

When elements are removed, the order will be:


40
30
20
10

Real-Life Examples of Stack

  • Stack of plates in a cafeteria.
  • Undo operation in text editors.
  • Browser back button.
  • Function calls in programming.
  • Expression evaluation.

Basic Operations on Stack

Operation Description
Push Adds a new element at the top of the stack.
Pop Removes the topmost element from the stack.

Push Operation

The Push operation inserts a new element at the top of the stack.

Illustration

Initial Stack


TOP → 30
       20
       10

After Push(40)


TOP → 40
       30
       20
       10

Pop Operation

The Pop operation removes the topmost element from the stack.

Illustration

Before Pop


TOP → 40
       30
       20
       10

After Pop


TOP → 30
       20
       10

Implementation of Stack Using Python List

Python does not have a built-in Stack data type. However, a list can be used to implement a Stack because lists support insertion and deletion at the end.

Creating an Empty Stack


stack = []

Implementing Push Operation

The append() method is used to insert an element at the top of the stack.

Syntax


stack.append(item)

Example


stack = []

stack.append(10)

stack.append(20)

stack.append(30)

print(stack)

Output


[10, 20, 30]

Here, 30 is the top element.


Implementing Pop Operation

The pop() method removes the topmost element from the stack.

Syntax


stack.pop()

Example


stack = [10,20,30]

item = stack.pop()

print(item)

print(stack)

Output


30
[10, 20]

Program: Push Elements into a Stack


stack = []

stack.append(100)

stack.append(200)

stack.append(300)

print("Stack =", stack)

Output


Stack = [100, 200, 300]

Program: Pop Elements from a Stack


stack = [10,20,30,40]

print("Removed :", stack.pop())

print("Updated Stack :", stack)

Output


Removed : 40

Updated Stack : [10, 20, 30]

Program: Push and Pop Together


stack = []

stack.append("Python")

stack.append("Java")

stack.append("C++")

print("Original Stack :", stack)

removed = stack.pop()

print("Removed :", removed)

print("Final Stack :", stack)

Output


Original Stack : ['Python', 'Java', 'C++']

Removed : C++

Final Stack : ['Python', 'Java']

Applications of Stack

  • Undo and Redo operations.
  • Browser history management.
  • Expression evaluation.
  • Parentheses matching.
  • Function call management (Call Stack).
  • Backtracking algorithms.

Advantages of Stack

  • Easy to implement.
  • Efficient insertion and deletion.
  • Useful in recursion.
  • Widely used in operating systems and compilers.

Limitations of Stack

  • Only the top element can be accessed directly.
  • Random access is not possible.
  • Searching is comparatively slower.

Difference Between append() and pop()

append() pop()
Adds an element to the end of the list. Removes the last element from the list.
Implements Push operation. Implements Pop operation.
Increases stack size. Decreases stack size.

Common Programming Errors

  • Calling pop() on an empty stack, which raises an IndexError.
  • Using insert() instead of append() for stack implementation.
  • Accessing elements randomly instead of following the LIFO principle.
  • Forgetting that the last element of the list represents the top of the stack.

Exam Tips

  • Remember that a Stack follows the LIFO (Last In, First Out) principle.
  • append() performs the Push operation.
  • pop() performs the Pop operation.
  • In Python, a Stack is commonly implemented using a list.
  • Practice simple programs for Push and Pop operations, as they are frequently asked in CBSE theory and practical examinations.

Frequently Asked Questions (FAQs)

1. What is a Stack?

A Stack is a linear data structure in which insertion and deletion occur only from the top and follows the LIFO principle.

2. What does LIFO mean?

LIFO stands for Last In, First Out, meaning the last inserted element is removed first.

3. Which Python method is used for Push?

The append() method is used to perform the Push operation.

4. Which Python method is used for Pop?

The pop() method is used to perform the Pop operation.

5. Which data structure is commonly used to implement a Stack in Python?

A Python list is commonly used to implement a Stack.


Summary

  • A Stack is a linear data structure that follows the LIFO (Last In, First Out) principle.
  • Insertion and deletion take place only from the TOP of the stack.
  • The Push operation inserts an element using append().
  • The Pop operation removes the topmost element using pop().
  • Python lists provide an easy and efficient way to implement a Stack.
  • Stacks are widely used in browser history, recursion, undo operations, and expression evaluation.