LightReader

Chapter 17 - Chapter 17 — The Data List

The screen dimmed slightly as the next module loaded.

The cursor blinked again.

_

Pran leaned forward.

The robot was standing beside the message panel again.

[^_^]

/| |\

/ \

A new system message appeared.

PYTHON MODULE PROGRESS: 60%

NEXT MODULE: DATA STORAGE

Pran nodded.

"Data storage… that sounds familiar."

He had already learned something like this in C.

Back then, the computer used arrays to store multiple values.

The system displayed another message.

PYTHON USES LISTS

Pran tilted his head.

"Lists?"

The computer showed an example.

numbers = [10, 20, 30, 40]

Pran read it slowly.

Inside the square brackets were multiple numbers.

That meant the variable numbers could hold several values at once.

The robot explained.

A LIST STORES MULTIPLE VALUES IN ONE VARIABLE

Pran typed the program into the editor.

numbers = [10, 20, 30, 40]

print(numbers)

He ran it.

The output appeared.

[10, 20, 30, 40]

Pran smiled.

"So the computer stored all four numbers."

But what if he wanted to see only one value?

The computer showed the next example.

numbers = [10, 20, 30, 40]

print(numbers[0])

Pran ran the program again.

The output appeared.

10

He nodded.

"That looks like arrays in C."

Exactly.

Just like arrays, lists also use indexes.

Indexes tell the computer which position to access.

The robot displayed a diagram.

Index: 0 1 2 3

Value: 10 20 30 40

So:

numbers[0] → 10

numbers[1] → 20

numbers[2] → 30

numbers[3] → 40

Pran experimented.

He changed the program.

numbers = [10, 20, 30, 40]

print(numbers[2])

He ran it.

The screen printed:

30

Pran smiled.

"Easy enough."

But lists can also store different kinds of values.

The computer showed another example.

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

print(items[1])

Pran ran it.

The output appeared.

banana

The robot nodded proudly.

LIST STORAGE VERIFIED

Pran leaned back in the chair.

"So lists can store numbers… or text."

Exactly.

Lists are used everywhere in programming.

For example:

storing player scores in a game

storing items in a shopping cart

storing sensor data in a program

storing names in a list

The computer screen flickered again.

Another message appeared.

LIST MODIFICATION TEST

Pran looked curious.

"Modification?"

The system showed another example.

numbers = [10, 20, 30, 40]

numbers[1] = 50

print(numbers)

Pran ran the program.

The output appeared.

[10, 50, 30, 40]

Pran nodded.

"So we can change values too."

Lists are very flexible.

You can:

read values

change values

add new values

remove values

The robot jumped happily.

[^O^]

/| |\

/ \

Another message appeared.

DATA STORAGE MODULE VERIFIED

Then another.

PYTHON MODULE PROGRESS: 70%

Pran crossed his arms.

"Seventy percent already."

The computer displayed another system message.

NEXT MODULE: TOOL CREATION

Pran raised an eyebrow.

"Tool creation?"

The robot displayed another word.

FUNCTIONS

Pran smiled.

He remembered functions from the C section.

Functions allow programmers to create their own reusable commands.

The cursor blinked again.

_

Waiting.

Ready for the next lesson.

Pran placed his hands on the keyboard again.

"Alright," he said.

"Let's start building tools."

Next Chapter

Chapter 18 — Building Functions

You will learn:

Python functions

creating reusable code

parameters and return values

More Chapters