Data structures are “containers” that organize and group data according to type. The data structures differ
based on mutability and order.
The basic Python data structures in Python include list, set, tuples, and dictionary.
Each of the data structures is unique in its own way.
Stacks in Python will be made using
lists as per CBSE syllabus
List
It is a collections of items and each item has its own index value.
Index of first item is 0 and the last item is n-1.Here n is number of items in a list.
list1 = ['English', 'Hindi', 2022, 2021];
list2 = [23, 20, 32, 49, 52 ];
Indexing of list
0 1 2 3
['English', 'Hindi', 2022, 2021]
-5 -4 -3 -2 -1 Negative indexing
[23, 20, 32, 49, 52]
Positive
indexing
Accessing Elements of a list
Consider a list
list3 = ["sub1", "sub2", "sub3", "sub4"];
list3 = ["sub1", "sub2", "sub3", "sub4"];
print("n using positive indexing" )
print(list3[0],"n")
print(list3[1],"n")
print(list3[2],"n")
print("n using negative indexing" )
print(list3[-1],"n")
print(list3[-2],"n")
print(list3[-3],"n")
Output
Accessing elements of a list using loop
list3 = ["sub1", "sub2", "sub3", "sub4"];
for i in range(0, len(list3)):
print(list3[i])
In the above code range method is going from 0 to length-1 of the list.
So if length is 4 then range is going from 0 to 3
For list operations we can access elements from any index.
We can access from any direction
A stack is a linear data structure in which all the insertion
and deletion of data or you can say its values are done at
one end only, rather than in the middle.
That one position is
Now let us understand How to implement stack in Python using Lists
Applications of Stack
• Expression Evaluation: It is used to evaluate prefix, postfix and infix
expressions.
• Expression Conversion: It can be used to convert one form of
expression(prefix,postfix or infix) to one another.
• Parenthesis Checking: Stack is used to check the proper opening and
closing of parenthesis.
• String Reversal: It can be used to reverse a string.
• Function Call: Stack is used to keep information about the active functions
or subroutines.
Using List as Stack in Python
The concept of Stack implementation is easy in Python , because it
support inbuilt functions (append() and pop()) for stack
implementation.
By Using these functions make the code short and simple for stack
implementation.
To add an item to the top of the list, i.e., to push an item, we use
append() function and to pop out an element we use pop() function.
These functions work quiet efficiently and fast in end operations.
Stack interactive
program
Menu driven code for
stack operation

Stacks in Python will be made using lists.pdf

  • 1.
    Data structures are“containers” that organize and group data according to type. The data structures differ based on mutability and order. The basic Python data structures in Python include list, set, tuples, and dictionary. Each of the data structures is unique in its own way.
  • 2.
    Stacks in Pythonwill be made using lists as per CBSE syllabus List It is a collections of items and each item has its own index value. Index of first item is 0 and the last item is n-1.Here n is number of items in a list. list1 = ['English', 'Hindi', 2022, 2021]; list2 = [23, 20, 32, 49, 52 ]; Indexing of list 0 1 2 3 ['English', 'Hindi', 2022, 2021] -5 -4 -3 -2 -1 Negative indexing [23, 20, 32, 49, 52] Positive indexing
  • 3.
    Accessing Elements ofa list Consider a list list3 = ["sub1", "sub2", "sub3", "sub4"]; list3 = ["sub1", "sub2", "sub3", "sub4"]; print("n using positive indexing" ) print(list3[0],"n") print(list3[1],"n") print(list3[2],"n") print("n using negative indexing" ) print(list3[-1],"n") print(list3[-2],"n") print(list3[-3],"n") Output
  • 4.
    Accessing elements ofa list using loop list3 = ["sub1", "sub2", "sub3", "sub4"]; for i in range(0, len(list3)): print(list3[i]) In the above code range method is going from 0 to length-1 of the list. So if length is 4 then range is going from 0 to 3 For list operations we can access elements from any index. We can access from any direction
  • 5.
    A stack isa linear data structure in which all the insertion and deletion of data or you can say its values are done at one end only, rather than in the middle. That one position is Now let us understand How to implement stack in Python using Lists
  • 6.
    Applications of Stack •Expression Evaluation: It is used to evaluate prefix, postfix and infix expressions. • Expression Conversion: It can be used to convert one form of expression(prefix,postfix or infix) to one another. • Parenthesis Checking: Stack is used to check the proper opening and closing of parenthesis. • String Reversal: It can be used to reverse a string. • Function Call: Stack is used to keep information about the active functions or subroutines.
  • 7.
    Using List asStack in Python The concept of Stack implementation is easy in Python , because it support inbuilt functions (append() and pop()) for stack implementation. By Using these functions make the code short and simple for stack implementation. To add an item to the top of the list, i.e., to push an item, we use append() function and to pop out an element we use pop() function. These functions work quiet efficiently and fast in end operations.
  • 9.
  • 10.
    Menu driven codefor stack operation