LING 529 preparation

Python Crash Course

A short, practice-oriented Python refresher for students preparing for Human Language Technology I.

What this is for

LING 529 assumes that programming is not a completely new activity for you. This crash course starts a little earlier than that. It is meant for students who are new to programming, rusty, or uncertain about whether the code they have seen before really counts as preparation.

The units below move from variables and strings to loops, functions, type hints, comprehensions, generators, classes, and a few standard library helpers. The examples stay close to language technology: tokens, tokenized sentences, counts, normalization, and adjacent word pairs.

A programming language is not only a way to tell a computer what to do. It is a way of learning the names and habits of a small world well enough that you can ask it to change: split this sentence, count these words, keep the useful parts, and do it again another 10,000 times. There is real power in that, and some of the pleasure of learning to program is noticing when the strange symbols on the page start to behave like names you can summon with. Early on, you are a little like a wizard's apprentice: a few words can set a lot in motion, sometimes more than you intended. The work is learning how to make the spell smaller, clearer, and more deliberate.

Assumptions

You do not need to have taken a formal computer science course. You should be willing to try short examples, read error messages slowly, and revise code after a check fails. If you already know another programming language, many of the ideas will feel familiar. The main work is learning Python's particular habits. Expect some apprentice moments: duplicated output, loops that run one time too many, strings split in the wrong place, and maybe an enchanted broom or two going rogue. Those are not signs that you are bad at programming. They are how you learn what your commands actually do.

The course assumes access to a browser and enough time to practice actively. Reading the lesson text is useful, but the point is to write small pieces of code until variables, loops, functions, containers, and types become things you can use on purpose.

Why type hints help

Python will often let you run code without saying what kind of value a variable or function expects. That flexibility is useful, but it can also hide mistakes until later. Type hints are labels for your ingredients. As any careful apothecary knows, it helps to know which bottle holds a string, which function expects a list of tokens, and which result should be an integer before you start mixing things together.

In this course, annotations are mostly for readers. They help future you remember what a function is supposed to do, help classmates and instructors understand your intention, and help tools point out likely mismatches. They also make Etude-style checks easier to interpret: if a function is annotated as returning int, but it returns a string or prints the answer instead, the problem is easier to name.

def count_tokens(text: str) -> int:
    return len(text.split())

The annotation does not force Python to behave like a statically typed language. It is more like a clear label on the expected input and result. The code still has to do the work, but the label helps everyone see what work the code is trying to do.

How the practice works

The embedded questions on this page can be completed directly in the browser. Those responses stay local to the embedded frame and do not count for a course grade. Use them when you want a quick check while reading.

The Etude links open fuller activities from the open Python crash course activity set. Use those when you want to work through a unit as practice, keep the exercises together, or return to them later from Etude.

If your instructor has added this activity set to a Class Maestro course run, start from that course space instead. Course-linked activity sets can have release dates, due dates, visibility rules, or roster-aware progress views. The underlying practice is the same; the course link adds the class context around it.

Lessons

Work through the units in order if Python is new to you. If you are refreshing skills, use the embedded questions as a quick diagnostic and open the full Etude activity when you find a weak spot.

Unit

1. Values, variables, and strings

Start small. You should be able to read a few lines of Python, name the values that are being created, and explain what changes when a name is reassigned. The goal is not to memorize syntax in the abstract; it is to make the moving parts inspectable.

Outcomes

  • trace variable assignment and reassignment
  • use string methods such as strip and lower
  • build short diagnostic strings with f-strings

Open the full Values, Variables, and Strings activity in Etude

Trace a few assignments
Build a small string summary

Unit

2. Sequences and loops

Most language data arrives as a sequence: characters in a string, tokens in a sentence, lines in a file, examples in a dataset. Once you can index, slice, and loop over sequences, a lot of NLP code becomes less mysterious.

Outcomes

  • split a string into tokens
  • index and slice strings safely
  • loop over tokens to collect measurements or counts

Open the full Sequences and Loops activity in Etude

Index and slice a token
Count short tokens with a loop

Unit

3. Functions, type hints, and checks

Course assignments will usually ask you to write functions that can be checked. That means the function should return a value, not only print something. Type hints do not make Python a different language, but they do make the contract easier to read: this function expects a string and returns an integer. They are a small piece of notation that helps you, your editor, and anyone reading your code agree on what kind of value should move through the function.

Outcomes

  • distinguish printed output from returned values
  • add simple parameter and return type hints
  • write functions with optional behavior controlled by arguments

Open the full Functions, Type Hints, and Checks activity in Etude

Return values, not just printed output
Add type hints to a function

Unit

4. Dictionaries and comprehensions

Dictionaries are the ordinary Python tool for keeping counts and attaching information to labels. Comprehensions are not required for every task, but they are common enough in Python examples that you should be able to recognize and write the simple cases.

Outcomes

  • count items with dictionary keys
  • normalize tokens before counting them
  • rewrite a small loop as a list comprehension

Open the full Dictionaries and Comprehensions activity in Etude

Build a frequency dictionary
Use a list comprehension

Unit

4B. List comprehension drills

This is deliberately a drill set. The point is to practice one narrow move several times: take a loop that builds a list and rewrite it as a list comprehension. The checks look at both behavior and syntax, so a loop that returns the right values will still ask you to try the comprehension form.

Outcomes

  • transform every item in a list
  • filter inside a comprehension
  • flatten nested lists with multiple for clauses

Open the full List Comprehension Drills activity in Etude

Normalize nonempty tokens
Flatten tokenized sentences

Unit

5. Python idioms and generators

Idioms are just conventional ways of expressing common operations. Some of them, such as f-strings and enumerate, make code easier to read immediately. Generators matter because language datasets can become large enough that building every intermediate list is no longer the best default.

Outcomes

  • format readable token reports with f-strings
  • use enumerate when positions matter
  • yield cleaned values from a generator function

Open the full Useful Python Idioms activity in Etude

Format a token report
Yield normalized tokens

Unit

5B. Generator drills

These exercises mirror the comprehension drills, but for lazy iteration. A generator function uses yield. A generator expression looks like a comprehension with parentheses. Both are worth recognizing before you encounter them in library code.

Outcomes

  • rewrite list-building code as a generator function
  • yield only values that pass a condition
  • return a generator expression instead of a list

Open the full Generator Drills activity in Etude

Yield token lengths
Return a generator expression

Unit

6. Classes and methods

LING 529 does not require you to become an object-oriented programming specialist. Still, you should be able to read a small class, initialize attributes, and understand why a method uses self. That is enough to make many course examples and Python libraries less opaque.

Outcomes

  • store instance attributes in __init__
  • write a method that inspects instance state
  • normalize values before membership checks

Open the full Classes and Methods activity in Etude

Create a token observation class
Add a method to a lexicon class

Unit

7. Standard library helpers

Good Python code is not only code you write yourself. The standard library includes small tools for common data-processing patterns: count things, create neighboring pairs, flatten nested iterables. These examples are not magic; they are compact names for patterns you have already practiced.

Outcomes

  • count tokens with collections.Counter
  • build adjacent token pairs with itertools.pairwise
  • flatten tokenized sentences with itertools.chain

Open the full Standard Library Helpers activity in Etude

Count tokens with Counter
Build bigrams with pairwise