concept index in category python

appears as: n index, The index, index, indexes, index, indexes, indices, indices
Tiny Python Projects: Learn coding and testing with puzzles and games

This is an excerpt from Manning's book Tiny Python Projects: Learn coding and testing with puzzles and games.

In Python, we can use square brackets and an index to get an individual character from a string. The index is the numeric position of an element in a sequence, and we must remember that indexing starts at 0.

>>> word = 'narwhal'
>>> word[0]
'n'

You can index into a literal string value too:

>>> 'narwhal'[0]
'n'

Because the index values start with 0, that means the last index is one less than the length of the string, which is often confusing. The length of “narwhal” is 7, but the last character is found at index 6:

>>> word[6]
'l'

You can also use negative index numbers to count backwards from the end, so the last index is also -1:

>>> word[-1]
'l'

You can use slice notation [start:stop] to get a range of characters. Both start and stop are optional. The default value for start is 0 (the beginning of the string), and the stop value is not inclusive:

>>> word[:3]
'nar'

All indexing in Python is zero-offset, so the first element of items is at index items[0]:

>>> items[0]
'soda'

If the index is negative, Python starts counting backwards from the end of the list. The index -1 is the last element of the list:

>>> items[-1]
'ice cream'

You should be very careful when using indexes to reference elements in a list. This is unsafe code:

Figure 10.2 Create a new string by selecting the portion of the string up to the index, a new character, and the portion of the string after the index.
Hello World! Third Edition

This is an excerpt from Manning's book Hello World! Third Edition.

You can use the index to change one of the list items:

>>> print(letters)
['a', 'b', 'c', 'd', 'e']
>>> letters[2] = 'z'
>>> print(letters)
['a', 'b', 'z', 'd', 'e']

But you can’t use the index to add new items to the list. Right now, there are five items in the list, with indices from 0 to 4. So you could not do something like this:

Here, we added the letter z at index 2. Index 2 is the third position in the list (because indices start at 0). The letter that used to be in the third position, c, got bumped over by one place, to the fourth position. Every other item in the list also got bumped one position.

Get Programming: Learn to code with Python

This is an excerpt from Manning's book Get Programming: Learn to code with Python.

  • You’re trying to access an index in the string that’s beyond the size of the string.
  • Figure 8.1. Four examples of substrings to find in the string "me myself and I". The arrows indicate the direction in which you’re finding the substring. The check mark tells you the index in which you found the substring. An x tells you that the substring wasn’t found.

    A list is a collection of any object type; it’s like a tuple, which you’ve seen and worked with already. A tuple and a list both have an order to them, in that the first element in the collection is at index 0, the second element is at index 1, and so on. The main difference is that lists are mutable objects, whereas tuples aren’t. You can add elements to, remove elements from, and modify elements in the same list object. With a tuple, every time you do an operation, you create a new tuple object whose value is the changed tuple.

    L[-1] = L[0] (Recall how negative indices work from lesson 7.)

    4

    menu = []

    1. Write one or more commands that mutate the list, so that the list menu contains

    ["pizza", "beer", "fries", "wings", "salad"].

    2. Continuing on, write one or more commands that mutate the list to contain

    ["salad", "fries", "wings", "pizza"].