<< Chapter < Page Chapter >> Page >

Then the subscription selects the value in the mapping that corresponds to that key.

In other words, the system looks up the word in the dictionary and returns the value that corresponds to that word.

I will have more to say about mappings in a future module.

Slicing

I discussed slicing at some length in the earlier module titled Itse1359-1070-Strings Part 2 . I will simply refer you back to that module for the discussion on slicing.

A mutable sequence

According to the Python Language Reference -- 3.2. The standard type hierarchy :

Mutable sequences:

Mutable sequences can be changed after they are created.

There are currently two intrinsic mutable sequence types:

  • Lists - The items of a list are arbitrary Python objects. Lists are formed by placing a comma-separated list of expressions in square brackets. (Note that there are no special cases needed to form lists of length 0 or 1.)
  • Byte Arrays - A bytearray object is a mutable array. They are created by the built-in bytearray() constructor. Aside from being mutable (and hence unhashable), byte arrays otherwise provide the same interface andfunctionality as immutable bytes objects.

This module does not discuss ByteArrays .

A list

According to The Python Tutorial -- 3.1.3. Lists :

"Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type.
Like strings (and all other built-in sequence type), lists can be indexed and sliced.
All slice operations return a new list containing the requested elements."

Some sample programs

Creating, indexing, and slicing lists

A list can be written as a sequence of comma-separated values (items) surrounded by square brackets.

Lists can also be nested within other lists.

List items do not all have to be of the same type.

An example list

The Python script shown in Listing 4 creates a simple list containing four elements of different types. The types of the elements are respectively, a float value, an integer , a string , and another integer .

Listing 4 . An example list.
# Illustrates creating, # indexing, and slicing lists.# #-------------------------------theList = [3.14,59,"A string",1024] print("Print index value 2")print(theList[2])print("Print a short slice") print(theList[0:3]) print("Print the entire list")print(theList[:100])

After creating the list, the program uses a subscription (index) to extract and print the value at index 2 (remember the first item is at index 0) .

Then it uses the slice notation to extract and print two different slices from the list.

The first slice extracts and prints the elements from index 0 through index 2 inclusive. (Remember, the items selected by a slice do not include the index specified by the upper limit value, which is 3 in this case.)

The second slice extracts and prints the entire list. If you don't understand these two slices, go back and review the module titled Itse1359-1070-Strings Part 2 where I discussslicing in detail.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




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?

Ask