📖 Scenario: Imagine you have a list of tasks to send to a server. Sending each task one by one takes a lot of time because each task needs a separate trip to the server. Batch operations let you send many tasks in one trip, saving time and effort.
🎯 Goal: You will create a simple program that shows how sending tasks one by one takes more trips, and how sending them in a batch reduces the number of trips to the server.
📋 What You'll Learn
Create a list of tasks with exact names
Create a batch size variable
Write code to split tasks into batches
Print the number of batches sent
💡 Why This Matters
🌍 Real World
Batch operations are used in apps and websites to send or receive many items at once, like uploading photos or sending messages.
💼 Career
Understanding batch operations helps in building efficient APIs and services that save time and reduce server load.
Progress0 / 4 steps
1
Create a list of tasks
Create a list called tasks with these exact strings: 'task1', 'task2', 'task3', 'task4', 'task5', 'task6'.
Rest API
Hint
Use square brackets [] to create a list and separate items with commas.
2
Set the batch size
Create a variable called batch_size and set it to 3.
Rest API
Hint
Just assign the number 3 to the variable batch_size.
3
Split tasks into batches
Create a list called batches that contains sublists of tasks each with length batch_size. Use a for loop with variable i and list slicing tasks[i:i+batch_size].
Rest API
Hint
Use a list comprehension with range stepping by batch_size and slice the list.
4
Print the number of batches sent
Print the text "Number of batches sent:" followed by the length of batches using print().
Rest API
Hint
Use print("Number of batches sent:", len(batches)) to show the result.
Practice
(1/5)
1. Why do batch operations reduce the number of round trips in REST APIs?
easy
A. Because they send requests one by one quickly
B. Because they use a faster internet connection
C. Because they skip server authentication
D. Because they combine multiple requests into a single network call
Solution
Step 1: Understand what a round trip means in REST APIs
A round trip is when your app sends a request to the server and waits for a response.
Step 2: Explain how batch operations affect round trips
Batch operations group many requests into one, so only one round trip is needed instead of many.
Final Answer:
Because they combine multiple requests into a single network call -> Option D
Quick Check:
Batch requests reduce round trips = Because they combine multiple requests into a single network call [OK]
Hint: Batch means group requests to send once [OK]
Common Mistakes:
Thinking batch speeds internet connection
Believing batch skips authentication
Assuming batch sends requests separately
2. Which of the following is the correct way to send a batch request in JSON format?
easy
A. {"requests": [{"method": "GET", "url": "/users"}]}
B. [{"method": "GET", "url": "/users"}]
C. {"request": {"method": "GET", "url": "/users"}}
D. {"methods": ["GET"], "urls": ["/users"]}
Solution
Step 1: Identify the standard batch request format
Batch requests usually wrap multiple requests inside a "requests" array in a JSON object.
Step 2: Match the correct JSON structure
{"requests": [{"method": "GET", "url": "/users"}]} correctly uses {"requests": [...]}, while others miss this structure or use wrong keys.
Final Answer:
{"requests": [{"method": "GET", "url": "/users"}]} -> Option A