Sequences
- A set of items indexed by ascending
integer values.
- Indexing is 0-based (
0
refers to the first item).
- Negative indexes (usually) are relative
to the length of the
sequence (
-1 refers to the last item).
- The built-in function
len()
returns the number of items in a
sequence.
Strings
- Strings can be enclosed in single quotes
or double quotes.
- Strings are immutable (read-only).
Raw strings
- Unformatted
- Precede the string with the letter "
r"
(i.e. r"Single \n line \n")
- Escape characters are not processed.
Multi-line strings
- Append a backslash character to the end
of every non-final
line.
- Enclose the string in a series of three
consecutive single or
double quotes.
str = "Multi-line" \
"string"
str = """
Multi-line
string
"""
str = '''
Multi-line
string
'''
Tuples
( expr [, expr]* )
- Comma-separated, parentheses-enclosed
list of expressions.
- Tuples are immutable (read-only).
() # 0 items
(1,) # 1 item
(1,2) # 2 items
Lists
[ expr [, expr]* ]
- Comma-separated, bracket-enclosed list of
expressions.
- Lists are mutable (writeable).
[] # 0 items
[1] # 1 item
[1,2] # 2 items
Slicings
seq [ start_index : less_than_index ]
# start_index: defaults to '0' (starting from the first item).
# less_than_index: defaults to the length of the sequence (ending with the last item).
- Selects a range of items in a sequence,
up to, but not
including, the last item specified.
seq[2:4] # [2..3]
seq[2:] # [2..end]
seq[:4] # [0..3]
seq[:] # [0..end]