IPython has a lot of power when working with our data. We can view it in various ways, save it to disk, restore it orm the save files and explore it using the IPython tools.
Let's start by creating some variables. I will create some in each of the most useful types.
Start with some numbers.
In [1]:
num = 42
In [2]:
flt = 4.2
Now for a string or two.
In [3]:
strng = "Some text"
In [4]:
m_strng = "Text with you're single quote"
In [5]:
m_strng
Out[5]:
Some arrays.
In [6]:
an_arr = [3, 5, 7, 9, 11, 13, 15, 17, 19]
In [7]:
other_arr = [12, "word", 6, 22, "Tate", 17]
In [8]:
other_arr
Out[8]:
Hey, how about that, an array contain items of more than one type.
Now dictionaries.
In [9]:
a_dict = {'first': "Word", 'second': "Three words total", 'third': 84, 'fourth': 91}
In [10]:
b_dict = {1: "twist", 7: "not six"}
In [11]:
c_dict = {'key':'item', 22: 'more_item'}
Wow, dictionaries are really loose. You can mix types in both the key and the items. Let's look inside a couple.
In [12]:
a_dict
Out[12]:
Though it seems the order isn't maintained.
In [13]:
a_dict['second']
Out[13]:
In [14]:
a_dict['fourth']
Out[14]:
In [15]:
a_dict['fourth'] = 31
In [16]:
a_dict
Out[16]:
I wonder what variables I have declared so far?
In [17]:
who
What can I find out about a variable?
In [18]:
a_dict?
Type: dict
String form: {'second': 'Three words total', 'fourth': 91, 'third': 84, 'first': 'Word'}
Length: 4
Docstring:
dict() -> new empty dictionary
dict(mapping) -> new dictionary initialized from a mapping object's
(key, value) pairs
dict(iterable) -> new dictionary initialized as if via:
d = {}
for k, v in iterable:
d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
in the keyword argument list. For example: dict(one=1, two=2)
In [19]:
type(flt)
Out[19]:
In [20]:
pdoc b_dict
Class docstring:
dict() -> new empty dictionary
dict(mapping) -> new dictionary initialized from a mapping object's
(key, value) pairs
dict(iterable) -> new dictionary initialized as if via:
d = {}
for k, v in iterable:
d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
in the keyword argument list. For example: dict(one=1, two=2)
In [21]:
pinfo c_dict
Type: dict
String form: {22: 'more_item', 'key': 'item'}
Length: 2
Docstring:
dict() -> new empty dictionary
dict(mapping) -> new dictionary initialized from a mapping object's
(key, value) pairs
dict(iterable) -> new dictionary initialized as if via:
d = {}
for k, v in iterable:
d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
in the keyword argument list. For example: dict(one=1, two=2)
In [22]:
who_ls
Out[22]:
How about keeping values across sessions? We can also clear out some or all of our variables.
In [23]:
store a_dict
In [24]:
store b_dict
In [25]:
store c_dict
In [26]:
who
In [27]:
reset_selective -f [abc]_dict
In [28]:
who
In [29]:
store -r
In [30]:
who
In [31]:
reset -f
In [32]:
who
In [33]:
store -r
In [34]:
who
In [35]:
an_arr = [3, 5, 7, 9, 11, 13, 15, 17, 19]
In [36]:
other_arr = [12, "word", 6, 22, "Tate", 17]
In [37]:
who
In [38]:
l = %who_ls
In [39]:
l
Out[39]:
In [40]:
for i in l:
t = "store " + i
get_ipython().magic(t)
In [41]:
who
In [42]:
reset
In [43]:
who
In [44]:
store -r
In [45]:
who
In [ ]: