<< Chapter < Page Chapter >> Page >

A practical example

Many years ago when I did a tour of duty as an enlisted man in the U.S. Air Force, the drill sergeant had a habit of lining us up and telling us to "count off."

What that meant was that the first person in the line called out the number one , the person behind him called out the number two , the person behind him called out the number three , etc.

(Since learning about computer programming, I now wonder if the first person should have called out zero. I notice that thefirst case of Ebola in the United States is being identified by the CDC as case 0.)

Assigning an ordinal index

I'm sure the drill sergeant didn't realize that what he was doing was assigning an ordinal index value to each person in the line (and neither did I realize it at the time) .

Using an ordinal index

Even though he didn't know the technical details of ordinal indices, he didn't have any difficulty yelling, "Number six, wash dishes, number fourteen,peel potatoes, number twenty-two, carry out the garbage, etc."

That is what using an index is all about -- using an ordinal index to select an item. As you will see in a future module titled Itse1359-1080-Lists Part 1 , this is also referred to as a subscription in Python. In the context of this module, indexing is the process of assigning an ordinal index value to each data item contained in some sort of acontainer.

In other words, we assign an ordinal number to each item, which describes the numerical position of the item in the container.

A dozen eggs:

For example, if you were very careful, you could use a felt tip pen to assign an ordinal index to each of the twelve eggs contained in acarton containing a dozen eggs. (Should you start with zero or one?)

Then you could extract the egg whose index value is 9 from the container and eat it for breakfast.

Having assigned the index, we can use that index to access the data item corresponding to that index, as in "Number six, wash dishes."

Index values automatically assigned

In this module, we will be using the index values that are automatically assigned to the characters in a string for the purpose of accessing thosecharacters, both individually, and in groups.


Here is what Magnus Lie Hetland has to say on the topic of slicing (and indexing as well.) Although this quotation was taken from a discussion of lists, it applies equallywell to strings. (Note that some material was deleted from the quotation for brevity.)

According to Magnus Lie Hetland:

"One of the nice things about lists is that you can access their elements separately or in groups, through indexing and slicing.

Indexing is done (as in many other languages) by appending the index in brackets to the list. (Note that the first element has index 0) ...

(This is the answer to the question about the first egg -- Baldwin)

Slicing is almost like indexing, except that you indicate both the start and stop index of the result, with a colon (":") separating them: ...

Notice that the end is non-inclusive. If one of the indices is dropped, it is assumed that you want everything in that direction. i.e. list[:3]means "every element from the beginning of list up to element 3,non-inclusive." ...

list[3:] would, on the other hand, mean "everyelement from list, starting at element 3 (inclusive) up to, and including, the last one."

For really interesting results, you can use negative numbers too: list[-3]is the third element from the end of the list..."

Questions & Answers

where we get a research paper on Nano chemistry....?
Maira Reply
what are the products of Nano chemistry?
Maira Reply
There are lots of products of nano chemistry... Like nano coatings.....carbon fiber.. And lots of others..
Even nanotechnology is pretty much all about chemistry... Its the chemistry on quantum or atomic level
no nanotechnology is also a part of physics and maths it requires angle formulas and some pressure regarding concepts
Preparation and Applications of Nanomaterial for Drug Delivery
Hafiz Reply
Application of nanotechnology in medicine
what is variations in raman spectra for nanomaterials
Jyoti Reply
I only see partial conversation and what's the question here!
Crow Reply
what about nanotechnology for water purification
RAW Reply
please someone correct me if I'm wrong but I think one can use nanoparticles, specially silver nanoparticles for water treatment.
yes that's correct
I think
Nasa has use it in the 60's, copper as water purification in the moon travel.
nanocopper obvius
what is the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
scanning tunneling microscope
how nano science is used for hydrophobicity
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
what is differents between GO and RGO?
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
analytical skills graphene is prepared to kill any type viruses .
Any one who tell me about Preparation and application of Nanomaterial for drug Delivery
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
The nanotechnology is as new science, to scale nanometric
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

Source:  OpenStax, Itse 1359 introduction to scripting languages: python. OpenStax CNX. Jan 22, 2016 Download for free at https://legacy.cnx.org/content/col11713/1.32
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Itse 1359 introduction to scripting languages: python' conversation and receive update notifications?

Anonymous User
Start Quiz
Madison Christian
Start Quiz