Reading Python#

  • This notebooks goal is not to produce python code, but an introduction on to to make sense of (read) code.

    • We will go through and learn some Python terminology and structure.

    • You can install Python on your computer and write code from scratch, and we do have resources on getting started with coding.

  • However, if you are reading or using a digital scholarships or digital humanities project, you may just want to have a general understanding of what the researchers or developers are using code for.

  • The goals of this notebook is to help you develop the ability to:

    • Be able to identify some discrete pieces of code

    • Identify (at a high level) the processes we are going through

    • Jupyter Notebooks and Colab are computational notebooks that allow you to run code in your browser. There are many computational notebooks used in workshops and online books.

Reading Left to Right and Right to Left#

Python has a syntax… ….and it builds on what happened immediately before, line by line. So the value of y depends on what happened just before.

reading python
x = 0 + 1   # Python creates x and stores 1 in it
y = x + 1   # Python uses the value of x (which is 1) to compute y = 2
  • Human thinking → left to right.

  • Left to right: You read the line of code like a sentence: x = 0 + 1 means “x equals 0 plus 1.”

  • Computer execution → right side first, then assign left.

  • Right to left: Python evaluates the right side first (0 + 1), and then stores that result into the variable on the left (x).

Variables#

A variable is like a tiny container where you store values and data

  • Filenames

  • Words

  • Numbers

  • Collections of words and numbers

  • Etc.

Some useful vocabulary for python#

Python Vocabulary 1
  • For exmaple: Teenage Mutant Ninja Turtles -  Leonardo, Donatello, Raphael and Michelangelo.

  • assign names to the variable TMNT and return them

  • TMNT = TMNT.names[“Leonardo”, “Donatello”, ”Raphael”, ”Michealangelo”]

  • TMNT.names()

  • TMNT.names_colors = [“blue”,”red”,”purple”,”orange”]

Examples#

reading index
  • Let’s go though and read what this code is doing

Python Vocabulary Python Vocabulary Python Vocabulary Python Vocabulary Python Vocabulary

Some more useful vocabulary for python#

Python Vocabulary 2

Libraries in python#

  • A library or package is a collection of pre-written code kits

    • They allow you do a bunch of different things

  • Sometimes you have to add a bunch of them in a row

    • There is probably an order to care about

    • What do you need to know?

Libraries in python: Examples#

Python Libraries 1 Python Libraries 2
  • pandas is library specifically for working with data, cleaning it up, etc

Some examples from Introduction to Cultural Analytics & Python#

Iterate Through Dictionary#

Iterate Through Dictionary 1 Iterate Through Dictionary 2
  • FOR intro a ”for” loop – idea that every time this thing happens, do this thing

  • IF introduces a certain level of logic

  • F’ (f-bar) is a formatting feature for strings (sets of letters-numbers)

  • { } pulls in the dictionaries established

Sentiment Analysis Example#

  • What is happening here? Vader example 1

  • Results: Vader example 2

Named Entity Recognition Example#

  • This is a common form – fill a list with certain information. What does it ask to do? NER example 1

  • Ents = “entities”

  • also == is a TRUE Statement (because we know that = is a variable assigner, == if this condition is true THEN do something

  • Results NER example 2

Part-of-Speech Tagging Example#

POS example 1

References: Introduction to Cultural Analytics & Python, Designed by Melanie Walsh

  • This online textbook offers an introduction to the programming language Python that is specifically designed for people interested in the humanities and social sciences.


Attribution

  • Creative Commons CC BY License

READING PYTHON: a program of commands for talking to your computer Created by Dr Heather Froehlich used under Creative Commons CC BY License