WebNovels

Chapter 16 - Chapter 16 — The Counting Loop

The screen glowed softly in the quiet room.

The cursor blinked again.

_

Pran leaned closer to the keyboard.

The robot was standing calmly beside the text.

[^_^]

/| |\

/ \

A new message appeared on the screen.

PYTHON MODULE PROGRESS: 50%

NEXT MODULE: COUNTING SYSTEM

Pran smiled.

"Counting system… that sounds simple enough."

The computer printed another message.

REPETITIVE COUNTING TASK DETECTED

ACTIVATE FOR LOOP

Pran had already learned about while loops, which repeat instructions until a condition changes.

But sometimes programmers want to repeat something a specific number of times.

For example:

print numbers from 1 to 10

repeat an action 5 times

process every item in a list

For these situations, Python uses a for loop.

The computer displayed the first example.

for i in range(5):

print(i)

Pran read the code slowly.

The important part was this line.

for i in range(5)

This means:

Repeat the loop 5 times.

The variable i changes automatically during each loop.

Let's see what happens.

Pran ran the program.

The output appeared.

0

1

2

3

4

Pran tilted his head.

"Why does it start at zero?"

The robot displayed another message.

PYTHON STARTS COUNTING FROM ZERO

That means:

range(5)

actually produces the numbers:

0

1

2

3

4

Five numbers in total.

Pran nodded.

"That makes sense."

Then the computer showed another example.

for i in range(1,6):

print(i)

Pran ran the program again.

The output appeared.

1

2

3

4

5

He smiled.

"Okay… that's exactly what I expected."

The robot jumped happily.

[^O^]

/| |\

/ \

The computer displayed another message.

COUNTING SYSTEM VERIFIED

Then another line appeared.

REPEAT TASK CHALLENGE

The system showed a new program.

for i in range(5):

print("Hello")

Pran ran it.

The screen printed:

Hello

Hello

Hello

Hello

Hello

Pran laughed.

"That's useful."

Instead of writing:

print("Hello")

print("Hello")

print("Hello")

print("Hello")

print("Hello")

The loop did everything automatically.

That is one of the most powerful ideas in programming.

Computers are extremely good at repeating instructions quickly and accurately.

The computer screen flickered again.

Another message appeared.

COUNTING ROBOT ACTIVATED

The robot on the screen suddenly started counting.

1

2

3

4

5

Then it stopped and bowed dramatically.

[^_^]

/| |\

/ \

Another message appeared.

PYTHON MODULE PROGRESS: 60%

Pran leaned back in his chair.

"Sixty percent already."

The computer displayed another system message.

NEXT MODULE: DATA STORAGE

Pran raised an eyebrow.

"Data storage?"

The robot displayed another word.

LISTS

Pran remembered something.

In the C programming section, he had learned about arrays, which store multiple values.

Python had something very similar.

But it called them lists.

The cursor blinked again.

_

Waiting.

Ready for the next lesson.

Pran placed his hands back on the keyboard.

"Alright," he said.

"Let's see how Python stores multiple values."

Next Chapter

Chapter 17 — The Data List

You will learn:

Python lists

storing multiple values

accessing list elements

More Chapters