Programming from zero

Print every port from 8000 to 8010

easyLoops Must-do

Problem statement

You want to check a block of ports that a team reserved for local services. Print every port number from 8000 to 8010, including both ends, one per line.

Examples

Example 1

Input: No input.

Output: 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010

Explanation: 11 lines, from 8000 up to and including 8010.

Hints

Approach

Optimal

A for loop runs its indented block once for every value in a sequence, assigning each value to the loop variable (here port). range(start, stop) produces whole numbers starting at start and stopping before stop. That is why the stop value is 8011: it makes 8010 the last number printed. This "stop is excluded" rule applies across Python, so it is worth remembering early.

Python
for port in range(8000, 8011):
print(port)

Follow-up questions

  • Print only even ports using range(8000, 8011, 2).
  • Print each as localhost:8000 and so on.

Frequently asked questions

In range, the stop value is never included. It is designed this way so range(0, n) gives exactly n numbers. Add 1 to the stop value when you want to include it.