|
 |
CS 3723
Programming Languages |
Containers
|
|
Reference links given below will be to the online book:
Building skills in Python
3. Containers: Strings, Lists, Tuples,
Dictionaries, Sets:
The items in each of these structures except for Sets can be accessed using an
index in square brackets: [ ]. For the first three this can
be an integer from 0 to the length (the function
len)
of the container minus 1.
Especially the first three: Strings, Lists, and Tuples have a
great deal in common. This commonality is explained very well in:
Sequences: Strings, Tuples and Lists.
Containers |
Name |
Item |
Enclosed In |
Access |
Concat |
String | char | Quote marks
a='abc', or
a="abc", | a[0], a[1], ... | Yes, + |
List | any | Brackets
b=[ a, 'b', 2,
[x, y], 4.5] | b[0], b[1], ... |
Yes, + |
Tuple | any | Parentheses
c=( a, 'b', 2,
[x, y], 4.5) | c[0], c[1], ... |
Not allowed |
Dictionary | Keyed Pair |
Braces
d={ 'a':14, 'b':23
'c':56 } | if 'a' in d
p=d['a'] | d.update(m) |
Set | any | e=set([3,5,7,11,
13,17,19]) | in | Ops: | & - ^ |
3.1. Strings:
Strings are very similar to those in other languages.
Of course lots of string methods.
String can be enclosed in single, double or triple quotes.
See
Strings.
3.2. Lists:
These are very similar to arrays in a normal language, except that
the items in these lists can be anything, including other lists
or functions.
See
Lists.
3.3. Tuples:
Similar to lists, except that they are immutable: once created
they can't be changed. The main reason to use these appears to
be saving storage. One book says that Python programmers
often just use a list where a tuple would do. See
Tuples.
3.4. Dictionaries:
These are a familiar feature in Perl and other scripting languages,
called "hashes" or "associative arrays" in Perl. See
Dictionaries.
3.5. Sets :
I see how these sets work, and how one can manipulate them.
Sets are an unusual
feature in a programming language. Not sure how useful they might be.
See
Sets.
(Revision date: 2014-05-24.
Please use ISO 8601,
the International Standard.)
|