0
0
DSA Pythonprogramming~10 mins

Push Operation on Stack in DSA Python - Build from Scratch

Choose your learning style9 modes available
Push Operation on Stack
📖 Scenario: Imagine you have a stack of books. You want to add a new book on top of the stack. This is called the push operation in stacks.
🎯 Goal: You will create a stack using a list, set a new book to add, push it onto the stack, and then print the stack to see the new book on top.
📋 What You'll Learn
Create a list called stack with these exact books: 'Math', 'Science', 'History'
Create a variable called new_book and set it to 'English'
Use the append() method on stack to push new_book onto the stack
Print the stack list to show the books after pushing
💡 Why This Matters
🌍 Real World
Stacks are used in many places like undo features in apps, browser history, and managing tasks in order.
💼 Career
Understanding stack operations is important for software development, especially in areas like parsing, memory management, and algorithm design.
Progress0 / 4 steps
1
Create the initial stack
Create a list called stack with these exact books: 'Math', 'Science', 'History'
DSA Python
Hint

Use square brackets [] to create a list and separate items with commas.

2
Set the new book to push
Create a variable called new_book and set it to the string 'English'
DSA Python
Hint

Use the equals sign = to assign the string 'English' to new_book.

3
Push the new book onto the stack
Use the append() method on the list stack to add the new_book on top
DSA Python
Hint

Call stack.append(new_book) to add the new book to the end of the list.

4
Print the stack after push
Print the list stack to show the books after pushing the new book
DSA Python
Hint

Use print(stack) to display the current stack.