WebNovels

Chapter 19 - Chapter 19 — Processing Data

The computer screen flickered softly.

The cursor blinked once again.

_

Pran leaned forward.

The robot stood beside the system panel, waiting patiently.

[^_^]

/| |\

/ \

A new system message appeared.

PYTHON MODULE PROGRESS: 80%

NEXT MODULE: DATA PROCESSING

Pran nodded slowly.

"Data processing… that sounds important."

The computer displayed another message.

PROGRAMS OFTEN WORK WITH LARGE AMOUNTS OF DATA

That was true.

Real programs often deal with:

lists of names

collections of numbers

items in a game inventory

scores in a competition

messages in a chat system

If a program has many values stored in a list, it often needs to go through each value one by one.

Instead of writing code for every item, programmers use a loop.

The computer displayed a simple list.

numbers = [1, 2, 3, 4]

Pran recognized this immediately.

"That's a list."

Then the computer showed the next step.

for n in numbers:

print(n)

Pran read the code carefully.

The important part was this line.

for n in numbers

This means:

Take each value in the list and store it in the variable n.

Then run the code inside the loop.

Pran ran the program.

The output appeared.

1

2

3

4

Pran smiled.

"So the loop goes through the list automatically."

Exactly.

Instead of writing four print statements, the loop handled everything.

The robot displayed a small diagram.

numbers = [1, 2, 3, 4]

Step 1 → n = 1

Step 2 → n = 2

Step 3 → n = 3

Step 4 → n = 4

Each time the loop runs, the variable n holds the next value in the list.

Pran experimented with another example.

fruits = ["apple", "banana", "orange"]

for fruit in fruits:

print(fruit)

He ran the program.

The output appeared.

apple

banana

orange

Pran nodded.

"That's very useful."

The computer displayed another message.

DATA PROCESSING CAN ALSO MODIFY VALUES

Pran looked curious.

"Modify values?"

The system showed another program.

numbers = [1, 2, 3, 4]

for n in numbers:

print(n * 2)

Pran ran it.

The output appeared.

2

4

6

8

The loop had multiplied every number by two.

Pran leaned back.

"So the program processed each value."

Exactly.

Programs often process data to:

calculate totals

transform values

analyze information

prepare results for display

The computer then showed another example.

This one calculated a sum.

numbers = [5, 10, 15]

total = 0

for n in numbers:

total = total + n

print("Total:", total)

Pran ran the program.

The output appeared.

Total: 30

Pran smiled.

"That's like a small calculator."

The robot jumped excitedly.

[^O^]

/| |\

/ \

The computer displayed another message.

DATA PROCESSING MODULE VERIFIED

Then another line appeared.

PYTHON MODULE PROGRESS: 90%

Pran's eyes widened.

"Ninety percent?"

The system was almost complete.

The computer screen flickered again.

A new message appeared.

FINAL SYSTEM MODULE APPROACHING

Pran leaned forward.

"What's left?"

The robot displayed the next concept.

RANDOM SYSTEM

Pran smiled.

"Random numbers?"

Random numbers allow programs to create unpredictable results.

They are used for:

games

simulations

security systems

artificial intelligence

The cursor blinked again.

_

Waiting.

Ready for the next lesson.

Pran placed his hands on the keyboard again.

"Alright," he said.

"Let's add some randomness."

Next Chapter

Chapter 20 — The Random Generator

You will learn:

importing modules

Python's random module

generating random numbers

More Chapters