📖 Scenario: Imagine you have a list of daily temperatures recorded in order. You want to look at these temperatures both from the start to the end (forward) and from the end back to the start (backward) to understand the pattern.
🎯 Goal: You will create a list of temperatures, then write code to traverse this list forward and backward, printing the temperatures in both directions.
📋 What You'll Learn
Create a list called temperatures with exact values: [23, 25, 21, 19, 22]
Create a variable called length that stores the length of the temperatures list
Use a for loop with variable i to traverse temperatures forward from index 0 to length - 1
Use a for loop with variable i to traverse temperatures backward from index length - 1 to 0
Print the temperatures in forward order separated by spaces
Print the temperatures in backward order separated by spaces
💡 Why This Matters
🌍 Real World
Traversing data forward and backward is useful in many real-life situations like reading sensor data, browsing history, or undo-redo operations.
💼 Career
Understanding how to move through data structures in both directions is a fundamental skill for software developers, data analysts, and anyone working with ordered data.
Progress0 / 4 steps
1
Create the temperature list
Create a list called temperatures with these exact values: [23, 25, 21, 19, 22]
DSA Python
Hint
Use square brackets to create a list and separate numbers with commas.
2
Create a variable for the list length
Create a variable called length that stores the length of the temperatures list using the len() function
DSA Python
Hint
Use len(temperatures) to get the number of items in the list.
3
Traverse the list forward and backward
Use a for loop with variable i to traverse temperatures forward from index 0 to length - 1 and print each temperature separated by spaces. Then use another for loop with variable i to traverse temperatures backward from index length - 1 to 0 and print each temperature separated by spaces.
DSA Python
Hint
Use range(length) for forward and range(length - 1, -1, -1) for backward traversal.
4
Print the forward and backward traversal results
Print the temperatures in forward order separated by spaces, then print the temperatures in backward order separated by spaces. The output should be exactly: 23 25 21 19 22 22 19 21 25 23
DSA Python
Hint
Check your print statements and loops to ensure the output matches exactly.
Practice
(1/5)
1. What does traversal forward mean when moving through a list?
easy
A. Visiting elements randomly
B. Visiting elements from the last to the first
C. Visiting elements from the first to the last
D. Skipping every other element
Solution
Step 1: Understand traversal direction
Traversal forward means starting at the beginning of the list and moving step-by-step to the end.
Step 2: Compare options
Only Visiting elements from the first to the last describes visiting elements from first to last, which matches forward traversal.
Final Answer:
Visiting elements from the first to the last -> Option C
Quick Check:
Forward traversal = first to last [OK]
Hint: Forward means start to end in order [OK]
Common Mistakes:
Confusing forward with backward traversal
Thinking traversal means skipping elements
Assuming traversal is random
2. Which of the following Python loops correctly traverses a list items backward?
easy
A. for i in range(len(items)): print(items[i])
B. for i in range(len(items)-1, -1, -1): print(items[i])
C. for i in items: print(i)
D. for i in range(0, len(items), 2): print(items[i])
Solution
Step 1: Identify backward traversal syntax
Backward traversal uses a loop that starts from the last index and moves to the first, decreasing the index.
Step 2: Check each option
for i in range(len(items)-1, -1, -1): print(items[i]) uses range(len(items)-1, -1, -1), which counts backward correctly.
Final Answer:
for i in range(len(items)-1, -1, -1): print(items[i]) -> Option B
Quick Check:
Backward loop uses decreasing range [OK]
Hint: Use range with step -1 for backward loops [OK]
Common Mistakes:
Using range(len(items)) for backward traversal
Forgetting to set step as -1
Using direct iteration which is forward only
3. What is the output of this code?
items = [10, 20, 30]
for i in range(len(items)-1, -1, -1):
print(items[i], end=' ')
medium
A. 30 20 10
B. 10 20 30
C. 0 1 2
D. SyntaxError
Solution
Step 1: Understand the loop range
The loop runs from index 2 down to 0 (inclusive), so it visits indices 2, 1, 0 in order.