In [1]:
[6, 28]
Out[1]:
In [2]:
[1e3, -2, "I am in a list."]
Out[2]:
In [3]:
[[1.0, 0.0], [0.0, 1.0]]
Out[3]:
In [4]:
[1, 1] + [2, 3, 5] + [8]
Out[4]:
In [5]:
fib = [1, 1, 2, 3, 5, 8]
In [6]:
fib.append(13)
fib
Out[6]:
In [7]:
fib.extend([21, 34, 55])
fib
Out[7]:
In [8]:
fib += [89, 144]
fib
Out[8]:
In [9]:
fib[::2]
Out[9]:
In [10]:
fib[3] = "whoops"
fib
Out[10]:
In [11]:
del fib[:5]
fib
Out[11]:
In [12]:
fib[1::2] = [-1, -1, -1]
fib
Out[12]:
In [13]:
[1, 2, 3] * 6
Out[13]:
In [14]:
list("F = dp/dt")
Out[14]:
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)
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]:
In [22]:
1, 2 + 3, 4
Out[22]:
In [23]:
tuple(["e", 2.718])
Out[23]:
In [24]:
x = 1.0, [2, 4], 16
x[1].append(8)
x
Out[24]:
In [25]:
# a literal set formed with elements of various types
{1.0, 10, "one hundred", (1, 0, 0,0)}
Out[25]:
In [26]:
# a literal set of special values
{True, False, None, "", 0.0, 0}
Out[26]:
In [27]:
# conversion from a list to a set
set([2.0, 4, "eight", (16,)])
Out[27]:
In [28]:
set("Marie Curie")
Out[28]:
In [29]:
set(["Marie Curie"])
Out[29]:
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]:
In [32]:
axes[3]
Out[32]:
In [33]:
al['birthday']
Out[33]:
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]:
In [36]:
{} # empty dict
set() # empty set
Out[36]:
In [37]:
"N_A" in constants
Out[37]:
In [38]:
axes.update({1: 'r', 2: 'phi', 3: 'theta'})
axes
Out[38]: