Easy way : with a Python distribution, anaconda: https://www.continuum.io/downloads
Hard way : install python and all dependencies yourself
Super hard way : compile everything from scratch
python
```
[yfeng1@waterfall ~]$ python
Python 2.7.12 (default, Sep 29 2016, 13:30:34)
[GCC 6.2.1 20160916 (Red Hat 6.2.1-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
```
We use Jupyter Notebook here.
Jupyter Notebook is included in the Anaconda distribution.
In [1]:
2 + 3
Out[1]:
In [2]:
2 / 3
Out[2]:
In [3]:
2 * 3
Out[3]:
In [4]:
2 ** 3
Out[4]:
In [5]:
num = 2 ** 3
In [6]:
print(num)
In [7]:
num
Out[7]:
In [8]:
type(num)
Out[8]:
In [9]:
name = "The Hacker Within"
In [10]:
type(name)
Out[10]:
In [11]:
name + 8
In [12]:
name + str(8)
Out[12]:
In [13]:
num_list = [0,1,2,3,4,5,6,7,8]
In [14]:
print(num_list)
In [15]:
type(num_list)
Out[15]:
In [16]:
num_list[3]
Out[16]:
In [17]:
num_list[3] = 10
In [18]:
print(num_list)
Appending new items to a list
In [19]:
num_list.append(3)
In [20]:
print(num_list)
In [21]:
for num in num_list:
print(num)
In [22]:
for num in num_list:
print(num, num * num)
In [23]:
num_list.append("LOL")
In [24]:
print(num_list)
In [25]:
for num in num_list:
if type(num) is int or type(num) is float:
print(num, num * num)
else:
print("ERROR!", num, "is not an int")
In [26]:
def process_list(input_list):
for num in input_list:
if type(num) is int or type(num) is float:
print(num, num * num)
else:
print("ERROR!", num, "is not an int")
In [27]:
process_list(num_list)
In [28]:
process_list([1,3,4,14,1,9])
In [29]:
yearly_value = {2001: 10, 2002: 14, 2003: 18, 2004: 20}
print(yearly_value)
In [30]:
yearly_value = {}
yearly_value[2001] = 10
yearly_value[2002] = 14
yearly_value[2003] = 18
yearly_value[2004] = 20
In [31]:
print(yearly_value)
In [32]:
yearly_value.pop(2001)
Out[32]:
In [33]:
yearly_value
Out[33]:
In [34]:
yearly_value[2001] = 10213
You can iterate through dictionaries too:
In [35]:
for key, value in yearly_value.items():
print(key, value)
In [36]:
for key, value in yearly_value.items():
print(key, value * 1.05)
In [37]:
name = "the hacker within"
In [38]:
name_long = """
~*~*~*~*~*~*~*~*~*~*~
THE HACKER WITHIN
~*~*~*~*~*~*~*~*~*~*~
"""
In [39]:
print(name)
In [40]:
print(name_long)
Strings have many built in methods:
In [41]:
print(name.upper())
print(name.split())
print(name.upper().split())
Strings are also a kind of list, and substrings can be accessed with string[start,end]
In [42]:
print(name[4:10])
print(name[4:])
print(name[:4])
In [43]:
count = 0
for character in name:
print(count, character)
count = count + 1
In [44]:
print(name.find('hacker'))
print(name[name.find('hacker'):])
In [45]:
def square_num(num):
return num * num
In [46]:
print(square_num(10))
print(square_num(9.1))
print(square_num(square_num(10)))
In [47]:
def yearly_adjustment(yearly_dict, adjustment):
for key, value in yearly_dict.items():
print(key, value * adjustment)
In [48]:
yearly_adjustment(yearly_value, 1.05)
We can expand on this a bit, adding some features:
In [49]:
def yearly_adjustment(yearly_dict, adjustment, print_values = False):
adjusted_dict = {}
for key, value in yearly_value.items():
if print_values is True:
print(key, value * adjustment)
adjusted_dict[key] = value * adjustment
return adjusted_dict
In [50]:
adjusted_yearly = yearly_adjustment(yearly_value, 1.05)
In [51]:
adjusted_yearly = yearly_adjustment(yearly_value, 1.05, print_values = True)
In [52]:
adjusted_yearly
Out[52]:
In [53]:
!curl -o thw.txt http://stuartgeiger.com/thw.txt
# and that's how it works, that's how you get to curl
In [54]:
with open('thw.txt') as f:
text = f.read()
In [55]:
text
Out[55]:
In [56]:
words = text.split()
lines = text.split("\n")
In [57]:
lines[0:5]
Out[57]:
But there is an error! R always appears as "RR" -- so we will replace "RR" with "R"
In [58]:
text.replace("RR", "R")
Out[58]:
In [59]:
text = text.replace("RR", "R")
In [60]:
words = text.split()
lines = text.split("\n")
In [61]:
lines[0:5]
Out[61]:
In [62]:
!pip install wordcloud
In [63]:
from wordcloud import WordCloud
In [64]:
wordcloud = WordCloud()
wordcloud.generate(text)
wordcloud.to_image()
Out[64]:
In [65]:
wordcloud = WordCloud(width=800, height=300, prefer_horizontal=1, stopwords=None)
wordcloud.generate(text)
wordcloud.to_image()
Out[65]:
In [66]:
freq_dict = {}
for word in words:
if word in freq_dict:
freq_dict[word] = freq_dict[word] + 1
else:
freq_dict[word] = 1
print(freq_dict)
A better way to do this is:
In [67]:
freq_dict = {}
for word in words:
freq_dict[word] = freq_dict.get(word, 0) + 1
print(freq_dict)
Let's start from a loop that prints the values to the screen
In [68]:
for word, freq in sorted(freq_dict.items()):
line = word + "\t" + str(freq)
print(line)
Then expand this to writing a file object:
In [69]:
with open("freq_dict_thw.csv", 'w') as f:
for word, freq in sorted(freq_dict.items()):
line = word + ", " + str(freq) + "\n"
f.write(line)
In [70]:
!head -10 freq_dict_thw.csv
In [ ]: