📖 Scenario: Imagine a line of people waiting to buy tickets at a movie theater. The first person to get in line is the first person to get the ticket and leave the line. This is called FIFO - First In, First Out.
🎯 Goal: You will create a simple queue using a list in Python. You will add people to the queue and then remove them in the order they arrived, showing how FIFO works.
📋 What You'll Learn
Create a list called queue with three people in order: 'Alice', 'Bob', and 'Charlie'
Create a variable called new_person and set it to 'Diana'
Add new_person to the end of the queue
Remove the first person from the queue to simulate them getting the ticket
Print the final state of the queue showing the order of people still waiting
💡 Why This Matters
🌍 Real World
Queues are used in real life whenever people or things wait in line, like at banks, supermarkets, or call centers.
💼 Career
Understanding queues helps in programming tasks like managing tasks, handling requests, or processing data in order.
Progress0 / 4 steps
1
Create the initial queue
Create a list called queue with these exact people in order: 'Alice', 'Bob', and 'Charlie'
DSA Python
Hint
Use square brackets [] to create a list and separate names with commas.
2
Add a new person to the queue
Create a variable called new_person and set it to 'Diana'
DSA Python
Hint
Use the equals sign = to assign the string 'Diana' to new_person.
3
Add the new person to the end of the queue
Use the append() method on queue to add new_person to the end
DSA Python
Hint
Use queue.append(new_person) to add the new person at the end.
4
Remove the first person from the queue and print the result
Remove the first person from queue using pop(0) to simulate them getting the ticket, then print the final queue
DSA Python
Hint
Use queue.pop(0) to remove the first person, then print(queue) to show the remaining people.
Practice
(1/5)
1. What does FIFO stand for in the context of a queue?
easy
A. First In, Final Output
B. Fast Input, Fast Output
C. Fast In, Fast Out
D. First In, First Out
Solution
Step 1: Understand FIFO meaning
FIFO means the first element added to the queue is the first one to be removed.
Step 2: Match the acronym
First In, First Out exactly matches FIFO.
Final Answer:
First In, First Out -> Option D
Quick Check:
FIFO = First In, First Out [OK]
Hint: Remember queue removes items in order added [OK]
Common Mistakes:
Confusing FIFO with LIFO
Thinking FIFO means last in first out
Mixing up acronym meanings
2. Which of the following is the correct way to add an element '5' to a queue named q in Python using the queue.Queue module?
easy
A. q.put(5)
B. q.push(5)
C. q.enqueue(5)
D. q.add(5)
Solution
Step 1: Recall queue.Queue methods
The Python queue.Queue class uses put() to add elements.
Step 2: Identify correct method
enqueue(), push(), and add() are not valid methods for queue.Queue.
Final Answer:
q.put(5) -> Option A
Quick Check:
Use put() to add in queue.Queue [OK]
Hint: Use put() to add items in Python queue.Queue [OK]
Common Mistakes:
Using enqueue() which is not in queue.Queue
Using push() from stack terminology
Using add() which is not a queue method
3. What will be the output of the following Python code using a list as a queue?