In [1]:
[6, 28]


Out[1]:
[6, 28]

In [2]:
[1e3, -2, "I am in a list."]


Out[2]:
[1000.0, -2, 'I am in a list.']

In [3]:
[[1.0, 0.0], [0.0, 1.0]]


Out[3]:
[[1.0, 0.0], [0.0, 1.0]]

In [4]:
[1, 1] + [2, 3, 5] + [8]


Out[4]:
[1, 1, 2, 3, 5, 8]

In [5]:
fib = [1, 1, 2, 3, 5, 8]

In [6]:
fib.append(13)
fib


Out[6]:
[1, 1, 2, 3, 5, 8, 13]

In [7]:
fib.extend([21, 34, 55])
fib


Out[7]:
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

In [8]:
fib += [89, 144]
fib


Out[8]:
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]

In [9]:
fib[::2]


Out[9]:
[1, 2, 5, 13, 34, 89]

In [10]:
fib[3] = "whoops"
fib


Out[10]:
[1, 1, 2, 'whoops', 5, 8, 13, 21, 34, 55, 89, 144]

In [11]:
del fib[:5]
fib


Out[11]:
[8, 13, 21, 34, 55, 89, 144]

In [12]:
fib[1::2] = [-1, -1, -1] 
fib


Out[12]:
[8, -1, 21, -1, 55, -1, 144]

In [13]:
[1, 2, 3] * 6


Out[13]:
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

In [14]:
list("F = dp/dt")


Out[14]:
['F', ' ', '=', ' ', 'd', 'p', '/', 'd', 't']

In [15]:
x = []
x.append(x)
x


Out[15]:
[[...]]

In [16]:
x[0]


Out[16]:
[[...]]

In [17]:
x[0][0]


Out[17]:
[[...]]

In [18]:
x = 42
y = x
del x

In [19]:
x = [3, 2, 1, "blast off!"]
y = x
y[1] = "TWO"
print(x)


[3, 'TWO', 1, 'blast off!']

In [20]:
a = 1, 2, 5, 3  # length-4 tuple
b = (42,)       # length-1 tuple, defined by comma
c = (42)        # not a tuple, just the number 42
d = ()          # length-0 tuple- no commas means no elements

In [21]:
(1, 2) + (3, 4)


Out[21]:
(1, 2, 3, 4)

In [22]:
1, 2 + 3, 4


Out[22]:
(1, 5, 4)

In [23]:
tuple(["e", 2.718])


Out[23]:
('e', 2.718)

In [24]:
x = 1.0, [2, 4], 16
x[1].append(8)
x


Out[24]:
(1.0, [2, 4, 8], 16)

In [25]:
# a literal set formed with elements of various types
{1.0, 10, "one hundred", (1, 0, 0,0)}


Out[25]:
{1.0, 10, (1, 0, 0, 0), 'one hundred'}

In [26]:
# a literal set of special values
{True, False, None, "", 0.0, 0}


Out[26]:
{0, '', None, True}

In [27]:
# conversion from a list to a set
set([2.0, 4, "eight", (16,)])


Out[27]:
{'eight', 2.0, (16,), 4}

In [28]:
set("Marie Curie")


Out[28]:
{' ', 'C', 'M', 'a', 'e', 'i', 'r', 'u'}

In [29]:
set(["Marie Curie"])


Out[29]:
{'Marie Curie'}

In [30]:
# A dictionary on one line that stores info about Einstein
al = {"first": "Albert", "last": "Einstein", "birthday": [1879, 3, 14]}

# You can split up dicts onto many lines
constants = {
    'pi': 3.14159,
    "e": 2.718,
    "h": 6.62606957e-34,
    True: 1.0,
    }

# A dict being formed from a list of (key, value) tuples
axes = dict([(1, "x"), (2, "y"), (3, "z")])

In [31]:
constants['e']


Out[31]:
2.718

In [32]:
axes[3]


Out[32]:
'z'

In [33]:
al['birthday']


Out[33]:
[1879, 3, 14]

In [34]:
constants[False] = 0.0
del axes[3]
al['first'] = "You can call me Al"

In [35]:
d = {}
d['d'] = d
d


Out[35]:
{'d': {...}}

In [36]:
{}     # empty dict
set()  # empty set


Out[36]:
set()

In [37]:
"N_A" in constants


Out[37]:
False

In [38]:
axes.update({1: 'r', 2: 'phi', 3: 'theta'})
axes


Out[38]:
{1: 'r', 2: 'phi', 3: 'theta'}