Computation for Linguists

Beginning Python: Setting up Python & Variables

Dr. Andrew M. Byrd

2025-09-29

Review

  • What did you learn last time?

Review: grep and regex

  • wc file.txt → identify number of lines, words, and characters in file.txt
    • -l, -w, -c
  • grep "word" file.txt → search for a string in a file
  • Combine with pipes:
    • grep "word" file.txt | wc -l
  • regex:
    • ., [...], [^...], [a-z], etc.

Overview of Python

Python

  • Python is a programming language, commonly used by linguists & programmers in general.
  • With it, we will write plain text documents that do things.

Programming with LaTeX

  • We defined what kind of document we were writing with documentclass{}

  • We imported additional code, with \usepackage{}

  • We wrote out instructions for how the document was supposed to look with commands like

  • Then, we used a LaTeX engine (using Overleaf) to interpret the LaTeX code, which returned a result in the form of a typeset pdf.

Making a website

  • We defined what kind of document we were writing with the <!DOCSTRING html>
  • We imported additional code, in the form of CSS, into the HTML document.
  • We wrote out instructions for how the text was supposed to look with commands like <strong></strong>
  • Then, we used a web browser to interpret the code in the HTML document, and the browser returned a result in the form of a website.

Programming at the Terminal

  • We even programmed a little bit at the Terminal, using RegEx!
  • Regular expressions are much simpler than HTML or LaTeX
  • But these simple expressions allow us to conduct powerful and sweeping searches

Python will be a lot like this!

When we write a Python program, we will:

  • Define what kind of document it is at the top with #! /usr/bin/python3
  • Import additional code, using import
  • Write out instructions for what the document is supposed to do with code.
  • Then use the Python interpreter to interpret our code, and do what we asked it to do.

Getting Set Up

Getting Set Up

  • Navigate to your LIN_301 directory
  • Create a new subfolder, called py.
    • This is where we’ll save our Python scripts.

Getting Set Up

  • Make sure that Python 3 is installed on your computer.
    • Visit: https://www.python.org/downloads/

    • Download the latest version for your OS

    • Once it’s installed, verify that it works in your RStudio Terminal:

      • PC:
      python --version      ## py --version also works
      pip --version
      • Mac:
      python3 --version
      pip --version

Running Your First Line of Code

  • Now that you have verified that Python works in RStudio, return to the terminal
  • PC:
$ py 
$ print("Hello world!")
$ quit()
  • Mac:
$ python3
$ print("Hello world!")
$ quit()

The function print:

  • Must be lower case.
  • You cannot leave out " within the print function
    • That said, you can use ' instead!
    • Try print(Hello world!). What happens?

Creating Your First Python Script

  • We won’t be working in the Terminal for much longer.
  • Let’s create a new .py file, called first_script.py
  • Within the new script, paste:
print("Hello world!")
  • Click Run in the document to run the code.

Working within Quarto

Quarto Docs

  • A Quarto document (.qmd) is a plain text file that mixes:
    • Markdown → text, headings, lists, images
    • Code chunks → Python, R, Julia, Bash, etc.
    • Output → tables, plots, formatted results
  • Think of it as a combo notebook + report.
  • All of the slides, homeworks, and check-ins are compiled as Quarto docs.

Why is Quarto Useful for Coding?

  • Lets you write code and explanation in the same file.
  • Can render to multiple formats:
    • Slides (like these!)
    • HTML pages
    • PDF reports (LaTeX!)
    • Word documents
  • Encourages reproducibility:
    • Anyone can rerun your code and get the same results.

Quarto Code

  • Create a new Quarto Document, called first_doc.qmd
  • Make sure it’s an html file.

Quarto Code

  • Once created, click on “Source”, delete everything within, and then paste the following code:
---
title: "My First Quarto Document"
author: "Your Name Here"
date: "2025-09-29"
format: html
---

# Introduction

This is my very first Quarto document!  
It will generate a PDF when I render it.

# Python Example

Here’s a Python code block that prints `"Hello World!"`:

Quarto Code

  • Now in “Visual”, click “Insert” > “Executable Cell” > “Python”
  • Paste in the following:
print("Hello world!")
  • This is an executable cell of code within your document.

Learning about Variables

Printing Hello world!

  • Remember earlier how this gave us an error?
print(Hello world!)
  • Create a code block in your qmd file, and try this instead:
hello_world = "Hello world!"
print(hello_world)

What just happened?

  • Why did print(hello_world) work, but print(Hello world!) didn’t?

Variables

  • hello_world is a variable that we defined as having the value "Hello world!"

Naming Variables

  • Case sensitive (hello_worldHello_world)
  • Must start with a letter or _
    • Cannot start with a number
  • Can only contain letters, numbers, underscores
    • So avoid &, *, ., ?

Variables

  • Let’s look at another example.
  • Python doesn’t know your age unless you tell it.
my_age
# NameError: name 'my_age' is not defined
  • We can assign a value to my_age using =:
my_age = 46
my_age
# 46

Using Variables

  • We can use variables just like numbers:
2025 - my_age

Using Variables

  • Let’s assign the current year to another variable:
current_year = 2025
current_year - my_age

Using Variables

  • Let’s store the result in a new variable:
year_of_birth = current_year - my_age
year_of_birth

Your Age in 2040

2040 - year_of_birth

Overwriting Variables

  • You can always reassign (i.e., change the value of) a variable:
my_age = 50
my_age
# 50
  • Note: other variables don’t automatically update: year_of_birth still has the old value.

Activity: When Does Your Kid Turn 18?

Let’s extend what we’ve learned with variables:

  • Assume you’ll have a child in the future, born when you turn 30.
  • Write code inside an embedded Python code block.
  • The end result should be a variable called child_18_year, which is the year your child turns 18.
  • You may:
    • Use the math symbols + and -
    • Define new variables
    • Print intermediate results to check your work

Activity Solution

Activity Solution

current_year = 2025
my_age = 20

# How many years until you turn 30?
years_til_kid = 30 - my_age

# What year is the child born?
child_birth_year = current_year + years_til_kid

# What year does the child turn 18?
child_18_year = child_birth_year + 18

print(child_18_year)