<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Intro to Python!
Stuart Geiger and Yu Feng for The Hacker Within
Contents
1. Installing Python
2. The Language
Expressions
List, Tuple and Dictionary
Strings
Functions
3. Example
Step1:  Variables
Step2:  Lists
Step3:  Appending new items to a list
Step4:  Loops and iteration
Step5:  If / else conditionals
Step6:  Functions
Step7:  Dictionaries
Store a key 
Step8:  You can iterate through dictionaries too
Step10:  Strings
We have seen strings a few times.
String literals can be defined with single or double quotation marks. Triple quotes allow multi-line strings.
Step11:  Strings have many built in methods
Step12:  Strings are also a kind of list, and substrings can be accessed with string[start,end]
Step13:  Functions
Step14:  We can expand on this a bit, adding some features
Step15:  Example
Step16:  But there is an error! R always appears as "RR" -- so we will replace "RR" with "R"
Step17:  Wordcloud library
Step18:  Freqency counts
Step19:  A better way to do this is
Step20:  Outputting to files
Let's start from a loop that prints the values to the screen
Step21:  Then expand this to writing a file object
<ASSISTANT_TASK:>
Python Code:
2 + 3
2 / 3
2 * 3
2 ** 3
Explanation: Intro to Python!
Stuart Geiger and Yu Feng for The Hacker Within
Contents
1. Installing Python
2. The Language
Expressions
List, Tuple and Dictionary
Strings
Functions
3. Example: Word Frequency Analysis with Python
Reading text files
Geting and using python packages : wordcloud
Histograms
Exporting data as text files
1. Installing Python:
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
Three Python user interfaces
Python Shell 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.
    &gt;&gt;&gt; 
```
Jupyter Notebook (in a browser, like this)
IDEs: PyCharm, Spyder, etc.
We use Jupyter Notebook here.
Jupyter Notebook is included in the Anaconda distribution.
Expressions
End of explanation
num = 2 ** 3
print(num)
num
type(num)
name = "The Hacker Within"
type(name)
name + 8
name + str(8)
Explanation: Variables
End of explanation
num_list = [0,1,2,3,4,5,6,7,8]
print(num_list)
type(num_list)
num_list[3]
num_list[3] = 10
print(num_list)
Explanation: Lists
End of explanation
num_list.append(3)
print(num_list)
Explanation: Appending new items to a list
End of explanation
for num in num_list:
    print(num)
for num in num_list:
    print(num, num * num)
num_list.append("LOL")
print(num_list)
Explanation: Loops and iteration
End of explanation
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")
Explanation: If / else conditionals
End of explanation
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")
process_list(num_list)
process_list([1,3,4,14,1,9])
Explanation: Functions
End of explanation
yearly_value = {2001: 10, 2002: 14, 2003: 18, 2004: 20}
print(yearly_value)
yearly_value = {}
yearly_value[2001] = 10
yearly_value[2002] = 14
yearly_value[2003] = 18
yearly_value[2004] = 20
print(yearly_value)
yearly_value.pop(2001)
yearly_value
yearly_value[2001] = 10213
Explanation: Dictionaries
Store a key : value relationship
End of explanation
for key, value in yearly_value.items():
    print(key, value)
for key, value in yearly_value.items():
    print(key, value * 1.05)
Explanation: You can iterate through dictionaries too:
End of explanation
name = "the hacker within"
name_long = 
~*~*~*~*~*~*~*~*~*~*~
THE HACKER WITHIN
~*~*~*~*~*~*~*~*~*~*~
print(name)
print(name_long)
Explanation: Strings
We have seen strings a few times.
String literals can be defined with single or double quotation marks. Triple quotes allow multi-line strings.
End of explanation
print(name.upper())
print(name.split())
print(name.upper().split())
Explanation: Strings have many built in methods:
End of explanation
print(name[4:10])
print(name[4:])
print(name[:4])
count = 0
for character in name:
    print(count, character)
    count = count + 1
print(name.find('hacker'))
print(name[name.find('hacker'):])
Explanation: Strings are also a kind of list, and substrings can be accessed with string[start,end]
End of explanation
def square_num(num):
    return num * num
print(square_num(10))
print(square_num(9.1))
print(square_num(square_num(10)))
def yearly_adjustment(yearly_dict, adjustment):
    for key, value in yearly_dict.items():
        print(key, value * adjustment)
yearly_adjustment(yearly_value, 1.05)
Explanation: Functions
End of explanation
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
adjusted_yearly = yearly_adjustment(yearly_value, 1.05)
adjusted_yearly = yearly_adjustment(yearly_value, 1.05, print_values = True)
adjusted_yearly
Explanation: We can expand on this a bit, adding some features:
End of explanation
!curl -o thw.txt http://stuartgeiger.com/thw.txt
# and that's how it works, that's how you get to curl
with open('thw.txt') as f:
    text = f.read()
text
words = text.split()
lines = text.split("\n")
lines[0:5]
Explanation: Example: word counts
If you begin a line in a notebook cell with !, it will execute a bash command as if you typed it in the terminal. We will use this to download a list of previous THW topics with the curl program.
End of explanation
text.replace("RR", "R")
text = text.replace("RR", "R")
words = text.split()
lines = text.split("\n")
lines[0:5]
Explanation: But there is an error! R always appears as "RR" -- so we will replace "RR" with "R"
End of explanation
!pip install wordcloud
from wordcloud import WordCloud
wordcloud = WordCloud()
wordcloud.generate(text)
wordcloud.to_image()
wordcloud = WordCloud(width=800, height=300, prefer_horizontal=1, stopwords=None)
wordcloud.generate(text)
wordcloud.to_image()
Explanation: Wordcloud library
End of explanation
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)
Explanation: Freqency counts
End of explanation
freq_dict = {}
for word in words:
    freq_dict[word] = freq_dict.get(word, 0) + 1
    
print(freq_dict)
Explanation: A better way to do this is:
End of explanation
for word, freq in sorted(freq_dict.items()):
    line = word + "\t" + str(freq)
    print(line)
Explanation: Outputting to files
Let's start from a loop that prints the values to the screen
End of explanation
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)
!head -10 freq_dict_thw.csv
Explanation: Then expand this to writing a file object:
End of explanation
<END_TASK>