Week 1: Notes

Here are some things we discussed in this week's tutorial:

computer hardware

Every computer has a CPU (central processing unit) that understands only one low-level language, called machine language. Different kinds of CPUs have different forms of machine language. Machine language is very low-level and consists of instructions. A typical modern processor can execute several billion instructions per second.

Every computer has some amount of RAM (random access memory), typically 8 Gb – 32 Gb for a modern notebook or desktop machine. A computer usually has a disk drive providing non-volatile storage, i.e. storage that retains its data even without power. In the past most disk drives were hard disks consisting of rotating magnetic platters, but these days computers (especially notebooks) typically have a SSD (solid-state drive). Modern disk drives typically have 256 Gb – 1 Tb of storage.

high-level languages

Usually programmers don't write code in machine language directly, since it is too low-level. Instead, they write code in higher-level languages such as Python, C, C++, Java, C#, or many others. To run a program in a higher-level language, you need either an interpreter or a compiler. An interpeter is a program that can run a program in a higher-level language, executing (interpreting) each of its statements as it goes along. By contrast, a compiler is a program that can translate (compile) a program in a higher-level language into a lower-level language, typically machine language.

In this course we will study the Python language.

installing Python

To install the Python interpreter on your computer, follow these steps:

Linux

Run your system's package manager. (On Ubuntu or Debian, I recommend running Synaptic). Find the package for Python 3. On many systems (e.g. Ubuntu) this will be called 'python3'. Install it.

macOS

In a web browser, go to the Python download page. Click the button "Download Python 3.10.0" (it may have a larger number if a newer version is available). When the installer finishes downloading, run it, and accept all the installation defaults.

Windows

In a web browser, go to the Python download page. Click the button "Download Python 3.10.0" (it may have a larger number if a newer version is available). You'll see a message "What do you want to do with python-3.10.0-amd64.exe from python.org?" Click Run.

You'll now see a window "Install Python 3.10.0 (64-bit)". In this window, be sure to check the box "Add Python 3.10 to PATH". Now click "Install Now". You'll see a message "Do you want to allow this app to make changes to your device?" Click Yes.

opening a terminal window

Linux

On many distributions, you can do this by pressing the Super key, then typing "terminal").

macOS

Press Command + Space, type "terminal", then press Enter.

Windows

Press the Windows key, type "command", then press Enter.

using the command line

You should be familiar with the following commands for looking at files in directories on the command line, and for moving between directories:

Linux or macOS

cd: change to a new directory

ls: list all files in the current directory

pwd: print the name of the working directory (i.e. the current directory)

cat: print the contents of a file

Windows

cd: change to a new directory, or (with no arguments) print the name of the current directory

dir: list all files in the current directory

type: print the contents of a file

running Python from the command line

From a terminal window, type "python3" (on Linux or macOS) or "python" (on Windows). if your Python installation is successful, then running Python should result in output that looks something like this:

$ python3
Python 3.9.5 (default, May 11 2021, 08:20:37) 
[GCC 10.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

writing a simple Python program

Use a text editor to create a file sum.py with these contents:

x = int(input())
y = int(input())
print(x + y)

This is a simple Python program that adds two numbers. Let's run it from a terminal window:

$ python3 sum.py
4
5
9
$ 

In the transcript above, I entered the numbers 4 and 5, and the program printed 9.

redirecting standard input and output

Text-mode programs commonly read from standard input and write to standard output. By default, standard input comes from the keyboard, and standard output appears in a terminal window. However, when you run a program you may redirect its standard input to come from a file, and may also redirect standard output to a file. (Sources and destinations other than files are also possible.)

On any operating system, use the < character to redirect input, and the > character to redirect output. For example, use a text editor to create a file test.in with these contents:

4
5

Now run the Python program we just wrote, redirecting its input from test.in and redirecting its output to a file called 'test.out':

$ python3 sum.py < test.in > test.out
$

The program ran, but produced no output on the terminal (since its output was redirected). Let's look at the output it produced:

$ cat test.out
9
$

Each of our homework assignments in our ReCodEx system will contain sample input and sample output for the program that you're supposed to write. To test your program, I recommend placing each sample input in a text file, and running your program with its input redirected from that file.

using an IDE

For writing programs that are longer than a few lines, you'll probably want to use an IDE (integrated development environment) rather than a plan text editor. Many different IDEs are available. If you're not sure what to use, I recommend Visual Studio Code, which is open source, runs on any major platform (i.e. operating system), and supports many different programming languages including Python.