Table of Contents

Python Stack Implementation

Example Description
Dive into implementing a stack in Python, an essential data structure. Explore the isEmpty function designed to determine if a stack is empty. Learn through practical examples how stack operations can be streamlined using Python, and enhance your programming skills.

Introduction:

Understanding stack data structures is crucial for various applications. In this tutorial, we explore the implementation of a stack in Python and introduce the isEmpty function, which helps assess whether a stack is empty or not. By providing practical examples, you’ll delve into the core concepts of stack operations and gain valuable insights into managing data using Python.

Code:

def isEmpty(glassStack):
    return len(glassStack) == 0

glassStack = []
print(isEmpty(glassStack))  # Output: True

glassStack = [1, 2, 3]
print(isEmpty(glassStack))  # Output: False

Logic:

  1. Define the isEmpty function with a parameter named glassStack, representing the stack to be checked.
  2. Return True if the length of the glassStack is 0 (indicating an empty stack), otherwise return False.
  3. Create an empty list named glassStack.
  4. Call the isEmpty function with the glassStack list as an argument and print the result. The expected output is True, indicating an empty stack.
  5. Assign elements [1,2,3] to the glassStack list.
  6. Call the isEmpty function with the glassStack list as an argument and print the result. The expected output is False, indicating a non-empty stack.

Output:

>> True

>>False