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