Python Intro

Overview

Python is a high-level programming language that is interpreted rather than compiled. It is object-oriented, and as such it supports classes and also has many available data structures. It has many open-source libraries and resources that make it a very powerful and versatile tool for anyone looking to make an automated way to do a task.

While Python was first introduced in 1991, it was relatively unknown until the mid-2000s. Over the past few years, however, it has quickly become one of the most popular programming languages in the world.

Installation

Python generally comes preinstalled on Ubuntu, but you can check which version you have by typing the following into your terminal:

python3 --version

If the above command says you don’t have Python installed or if your Python version is not 3.8 (which is typically the default on Ubuntu 20), you can type the following into your terminal to get that version of Python:

sudo apt update
sudo apt install python3.8

There are also many other versions of Python you can download. Using other versions will become more important as you learn about Python Environments later on. For now, using the default version on your computer is a great starting point.

Lecture Videos

On May 10th and 12th of 2021 we had an introduction to Python by Prof Lundrigan. The videos are embedded below

Lecture 1

Timestamps

0:00 Introduction
2:23 Why Python?
17:42 The Python REPL
34:13 Basics of Python script writing
50:36 Python interpreting process and debugging
52:57 Python as a multi-paradigm language
1:06:10 Dynamic typing

Lecture 2

Timestamps

0:00 Follow-up activity introduction
3:52 Introduction to argparse
16:00 Reading files
22:45 Introduction to requests
28:30 Review of assignment and demos

Lecture 3 (2022)

ACTIVITY

Which of the following statements in Python is equivalent to 1776?

  • 1,776
  • 1.776
  • 1-776
  • 1_776

ACTIVITY

What is the output of the following statements: lst = ['My', 'name', 'is', 'Python'] and lst[-2]?

  • is
  • IndexError: list index out of range
  • Python
  • name
  • SyntaxError: invalid syntax

ACTIVITY

In C and C++, we type #include to add code from header files to our current file.

In Python, however, we just type ______ to include other files or modules in our program.

ACTIVITY

What does this statement do? import numpy as np

  • Imports only the "np" part of numpy
  • Nothing. This is only used as filler code. Everyone puts it in their files for no reason
  • Imports the numpy module and sets "np" as an alias for the user to use in place of "numpy" for convenience
  • The "as np" portion of the statement is a flag that tells Python not to raise an error if there is a problem importing numpy

ACTIVITY

How does the Python interpreter react to this statement? lst = [5, 8.1, 'Python', True]

  • It will throw a syntax error and quit
  • It will set all elements to the type and value of the first element
  • It will set lst to contain just the first two elements since they are the only numbers in the list
  • It won't freak out since this is a valid list

Follow-Up Activities

Create a command line utility that filters columns of a CSV file. The utility will take in a CSV file and column indexes that it should select. Use the following usage pattern:

usage: csv_parser.py [-h] [-c COLUMN] [-v] csv_file

Process CSV file.

positional arguments:
  csv_file              CSV file to be parsed

optional arguments:
  -h, --help            show this help message and exit
  -c COLUMN, --column COLUMN
                        columns to select from CSV file
  -v, --verbose         print logging messages

To build a command line tool, you will need to use argparse or something similar. Your utility should work with any well formatted CSV file, but here is some example data to get you started: small and large.

If you are already familiar with Python, increase the difficulty by allowing a URL to be passed in instead of a file. Your utility will download the CSV file and then parse it. The third-party library requests will be useful for this.

Additional Resources

For those who have not used Python before, there is a tutorial put out by python.org that is very helpful and gives a great introduction to the language. It is also a great tool for those who are familiar with Python already because it goes deeply into a lot of the available data structures and other aspects of the language. This tutorial is for Python3.10.4, which is the newest version out right now, but you can choose most of the previous versions on the drop-down menu that appears on the tutorial. The link to it is here.