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

Expressions

Variables

Lists

Loops and iteration

If / else conditionals

Functions

Dictionaries

Store a key : value relationship

You can iterate through dictionaries too:

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.

Strings have many built in methods:

Strings are also a kind of list, and substrings can be accessed with string[start,end]

Functions

Example: word counts

But there is an error! R always appears as "RR" -- so we will replace "RR" with "R"

Wordcloud library

Freqency counts

A better way to do this is:

Outputting to files

Let's start from a loop that prints the values to the screen

Then expand this to writing a file object:


In [ ]: