Code as a Liberal Art, Spring 2024

Unit 1, Lesson 1 — Thursday, January 25

Preliminaries: Files, Paths, and the Command Line

We'll get started today talking about some of the most basic building blocks of working with code. Many bootcamps and online coding platforms hide these details to make getting started easier for you. But today we're going to start by covering these topics because they are the foundational ideas of all digital machines and will lay a solid groundwork to start with this semester.

Table of contents for the topics today: (Click each to jump down to the corresponding section.)

  1. The command line
  2. A simple folder structure
  3. Python
  4. What is a file, really?
  5. The VS Code text editor
  6. Wrapping up & homework

I. The command line

(jump back up to table of contents)

II. A simple folder structure

(jump back up to table of contents)

III. Python

Next let's introduce Python in the command line context.

From the command line, you should simply be able to type python3 to invoke the Python shell:

$ python3
Python 3.8.3 (v3.8.3:6f8c8320e9, May 13 2020, 16:29:34) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
You may not see precisely the same output, but it should be similar. If you see Python 2.7.x let me know.

Noteworthy here is that the prompt has changed to >>>. This is to indicate that you are not in the operating system shell, but rather in the Python shell. At this point, you can no longer type the regular shell commands. For example, cd and pwd will give you an error message. Instead, you can type Python commands. Let's experiment with a few. Try this:

>>> print("Hello!")
Hello
If you get an error message like bash: syntax error near unexpected token ..., it means you are not in the Python shell. Type python, and make sure that you see the Python command prompt: >>>

You have just typed and executed a single line of Python code. This line is comprised of one command: print(). The print() command is simple: it displays a message to the user (i.e., it "prints" it to the screen). What does it print? Whatever you specify in between the parentheses, which is what we call the arguments to the command.

In this case, the argument is "Hello": a bit of text enclosed in quotes (""), which, in computer programming, we call a string. Computers can process numbers and text (strings), as well as other digital objects like images, video, etc. Let's look at some other Python commands.

In the Python shell, if you simply type a number, Python will evaluate that expression and display the result, basically just printing the value:

>>> 10
10
>>> 5
5

Note that strings are different from numbers, even if they may seem like the same value to us. Note the difference in output here. Python uses single quotes ' ' or double-quotes " " to indicate string values. Think of single and double quotes as interchangeable in Python for now and you can use either, but you must use the same end quote as you used for the beginning of a string.

>>> "Hello"
'Hello'
>>> 10
10
>>> "10"
'10'

Note that the number 10 and the string '10' are not equal. We can check values for equality with two equal signs ==. For example:

>>> 10 == 5 + 5
True
>>> 10 == "10"
False

You can also create variables, set them to values, and do various operations on them, such as arithmetic. In Python, like in most programming languages, we have addition +, subtraction -, multiplication * and division /.

>>> x = 10
>>> y = 5
>>> x + y
15
>>> x / y
2.0
>>> x * y
50

And we can combine commands in various ways, for example to print() the result of an operation on a variable:

>>> print(x-1)
9

We can also use things called control structures, such as the if command, to change the way code instructions are executed:

>>> if x > 5:
...   print("Greater")
... else:
...   print("Less than")
... 
Greater

Note that the command prompt changed for a moment to dots: .... This notation indicates that I am inside a code block, in this case, an if statement. This code block is initiated by the colon :.

Usually Python ignores all whitespace. So x = 5 is the same as x =     5, and print("Hello") is the same as print(  "Hello"  ). You can add spaces for clarity and readability. However, at the start of a line, spaces are called indentation and they are very important in Python: they indicate that you are in a code block. In the above example, both print() statements are indented because they are in a way part of the if statement.

The last line containing only ... is created by pressing ENTER with no spaces and no other text. The empty line ends the if statement, instructing Python to evaluate the above four lines.

The if statement will run differently with different values:

>>> x = 2
>>> if x > 5:
...   print("Greater")
... else:
...   print("Less than")
... 
Less than

(When working through coding examples, I will indicate any modified code in orange with a thin dashed underline. Again, I hope to accommodate all different types of seeing abilities and if this styling is not super clear, please let me know how I can make it better!)

Typing commands like this into the Python shell can be a good way to learn, but it is a very limited way to work. Algorithms are all about automation: writing some instructions and then running them on different inputs. In other words, you don't want to have to manually modify the value of x each time, you want an if statement that automatically responds to some data or user input. To achieve this, we'll have to exit the Python shell and create and executable Python computer program by typing code into a file, which we can then run many many times without re-typing.

To exit the Python shell back to the command line, you can type CONTROL-D or exit().

(jump back up to table of contents)

IV. What is a file, really?

A file is a collection of data, grouped together into one atomic unit, which exists on a computer and is identified by a name and a path.

Files are managed on computers by something called file systems. The file system is the entire scheme of folders, subfolders, and paths that your computer uses to organize and manage data as atomic files. The file system is one part of your operating system (e.g., Mac, Windows, Linux) along with other system utilities.

Files can be of all types: images, sounds, videos, games. They can be fancy text with lots of formatting like a Microsoft Word file, or they can be simple text with no formatting, which we call plain text, and which are usually opened by a program like Text Edit.

File types are usually specified by filename extensions: the part at the end that starts with a "dot" .. The file extension tells your operating system which program to use to open a given file. For example, if you double-click a .doc file, your computer will probably open it with Microsoft Word; .html files are opened with your default web browser; .pdf files will be opened with Preview (Mac), QuickLook (Windows) or Acrobat Reader; and image files like .jpg or .png will probably be opened with Preview or QuickLook.

Please adjust the preferences for Finder (Mac) or Explorer (Windows) so that your operating system will not hide file extensions to you. Understanding file extensions is a key part of digital literacy, and knowing the extensions of files will be crucial for accessing them via computer programs this semester.

Computer programs are just files! They are usually plain text files on your computer, with a file extension telling your operating system that they can be run.

(jump back up to table of contents)

V. The VS Code text editor

To create computer program files, you need a text editor: a program that lets you type and save text as plain text, without any fancy formatting like Microsoft Word offers.

The text editor that we will use this semester is a very useful, open source tool called VS Code. The fact that it is open source means that the computer program files and algorithms of this tool are freely, publicly available, not proprietary or hidden. That means you can view the source code for this tool! In fact, the full source code is posted here on GitHub. Feel free to have a look. This also means that if you wanted to, you could modify VS Code to change its behavior in some way, then rebuild it and run your own customized version. (A very advanced exercise, but possible!)

Great! Now we can get started coding in Python.

VI. Wrapping up & homework

Hopefully this gives you some building blocks in algorithmic thinking that you can play with. The homework for this week has some opportunities for you to experiment with these ideas. Have fun!