CPSC 115L: Introduction to Computing Fall 2010

Laboratory 1: Python programming on GNU/Linux

September 8, 9

Throughout this term, in the pass/fail laboratories, you are expected to work with an assigned partner as a pair (except, in case there are an odd number of students, you will be asked to work alone every once in a while). Both you and your partner will receive the same grade. Both of you should always save your laboratory work on your own accounts.

Objectives

The main objectives of this laboratory are
  1. to familiarize yourself with the GNU/Linux operating system, and
  2. to learn how to write and run very simple Python scripts using the integrated development environment DrPython.

1. GNU/Linux operating system

Introduction. In this course, we will be using the GNU/Linux operating system. It is a free and open-source operating system published under the GNU General Public License (GPL). Free and open source (or FOSS) means that, unlike Mac OS and Windows, anyone is free to use, study, modify and redistribute any of the actual source code. GNU/Linux is touted for its speed, minimal hardware requirements, security and remote administration. It is a fully-featured operating system that does not have to cost a dime. Because of GNU/Linux's speed and stability, it has increasing become the operating system of choice for many advanced users and permeates the entire web market.

Ubuntu. There are a number of different distributions of GNU/Linux available on the Internet. For its elegant design and robustness, the Trinity College Computer Science Department has chosen the Ubuntu distribution (which is based on the popular Debian distribution of GNU/Linux). Ubuntu is currently the most popular Linux distribution and widely-regarded as the best alternative to Mac OS and Windows.

Logging in. To use GNU/Linux, you must first identify yourself to the system. This is done so that the system knows who you are, what permissions you have, and what your preferences are. To identify who you are, you will use your usual Trinity username and password. Enter your username and password. You should then see the following GNOME desktop (if not, log out, select GNOME from the session menu, and then log in again).

*

GNOME is a free and open-source desktop environment for GNU/Linux, similar to the desktop environments found under Mac OS and Windows. The main desktop components are the desktop icons and the panels located at both the top and bottom of the screen. The top panel has widgets and launchers. On the left are three menus of launchers: Applications, Places and Systems. Most basic tasks can be performed from one of these menus. On the right are a clock, a volume control, a printer status, etc. The bottom panel has a taskbar on the left and a desktop switcher on the right.

Creating a cpsc115 directory. You will now create a directory (i.e., folder) to keep all of your course-related files. On the top panel, click the Places menu and then select Home Folder. You should then see the following File Browser window.

*

Now, right-click anywhere inside the window and select Create Folder. Name the new folder cpsc115, all in the lowercase without any space (in general, files and directories should be named all in the lowercase without any space). You should save all course-related files in this directory. Remember how to find this directory because you are going to need it every week.

File and directory permissions. In GNU/Linux, file and directory access permissions are defined separately for the following three categories of users:
  1. Owner—refers to the owner of a file (resp. directory).
  2. Group—refers to the group of users to which a file (resp. directory) belongs; by default, the owner is the only member of the group.
  3. Others—refers to all users of the system.
For files, each user category is given one of the following three types of permissions: None, Read-only and Read and write. For directories, each user category is given one of the following four types of permissions: None, List files only, Access files and Create and delete files. To see permission information of a file (resp. directory), all you need to do is to right-click the file (resp. folder) and select Properties and then the Permissions tab.

Protecting your files and directories. Your account's file space resides in the Computer Science Department's central server, which in turn is accessible from anywhere in the world through the Internet, so it is very important to actively protect your personal file space. Now, change the access permission of your cpsc115 directory as follows so that only you have an access to it.
  1. Right-click the cpsc115 folder.
  2. Select Properties and then the Permissions tab.
  3. For Owner's folder access, select Create and delete files. For Group and Others' folder access, select None. Permissions should now look like:

    *

    Click the Close button.
Throughout the term, you should maintain the access permission of the cpsc115 directory as above so that no one besides you can access your files. When sharing laboratory work with your partner, send program files as e-mail attachments.

2. DrPython

Introduction. In this course, to learn how to program in Python, we will be using DrPython, an interactive, integrated programming environment originally developed by Daniel Pozmanter of Education to Empower. It is particularly well-regarded for its simplicity and appeals to beginners. DrPython is a free and open-source system distributed under the GNU GPL.

Launching DrPython. To launch DrPython on your laboratory workstation, all you need to do is to select on the top panel Applications, Programming and then DrPython. You should now see the following window.

*

Using DrPython's interactive interpreter. Python is an interpreted language, and its interpreter supports both the interactive and script modes. The interactive mode is suited for small pieces of code because you get can instant feedback. On the other hand, the script mode, which should be used for anything more than a few lines of code, allows you to save your code as a script so that you can modify and use it later. To start the interactive mode, select on DrPython's top panel Program and then Open a Python Interpreter. You should now have a Python interpreter tab as follows.

*

Try typing in some arithmetic expressions such as 1 + 1. Upon entering such an expression, the interpreter will give you an answer instantly. As in arithmetic, in Python (actually, in most programming languages), +, - and / denote addition, subtraction and division, respectively. Multiplication, however, is denoted by *.

Hello, World! Traditionally, the first program students write involves instructing a computer to output “Hello, World!”. In this course, we will also follow this tradition. For this, in Python, all you need to do is to type the following print command:
print 'Hello, World!'
Writing and running a Python script. In the interactive mode, a Python interpreter does not allow you to save your code in a file. Everything you wrote will be lost when you exit from your interactive session. Alternatively, in the script mode, you can save your code in a file, called a script. Before working on a script, you should first create a directory to save today's work. Using the File Browser, inside your cpsc115 directory, create a subdirectory named lab1 (in general, at the beginning of each laboratory, you should create a unique subdirectory for it). Now, in the blank tab Untitled 1, type again
print 'Hello, World!'
as follows:

*

To save this code, as usual, select File and then Save As... We will save this code in the lab1 subdirectory we just created. Select the correct destination and name your file helloworld.py as follows. Python scripts usually have this .py extension.

*

To run this script, all you need to do is to simply click the blue arrow below Documents. Output will appear in a new tab:

*

Now, try modifying your code, for example, as
print 'Hi there!'
print 'How are you?'
Save your file and execute it. Do you see a different result? This script is permanently saved in your own personal file space, and it will remain there as long as you wish, until you delete it.

Syntax and semantic errors. As programs get complicated, you are likely to make errors. In fact, you will be making many, many errors. It is just a fact of life, more so in programming than other areas of life. There are two types errors you will be making: syntax and semantic. A syntax error is caused by violating the grammatical rules of a programming language. For example, 1 +* 1 will cause a syntax error because an arithmetic operator must be between two numbers. On the other hand, a semantic error is caused by grammatically-correct code that gives a wrong answer or generates an unintended behavior. For example, if your intention is to add 2 and 3 but you wrote 2 * 3, then you made a semantic error. Semantic errors are often very hard to detect because only you know what you intend to do. What would be examples of syntax and semantic errors for the statement print 'Hello, World!'? Try introducing a syntax error into you script and then try to save it and run. What happens?

Comments. In programming, comments are text that is meaningful to humans but means nothing to computers; that is, comments are text ignored by computers. In Python, the # character denotes a programmer's comment that extends to the end of the line. At the beginning of each script, it is customary to put a header to provide some very basic information about the script:
#
# File: helloworld.py
# Author: Takunari Miyazaki
#
# Created:  01/26/10
# Modified: 01/26/10
#
Following this example, put a header in the file helloworld.py.

What to hand in

Upon completion of your laboratory, have your work checked by the instructor or TA. Show your examples of syntax and semantic errors of print 'Hello, World!' Turn in a printout of the file helloworld.py that has the statement print 'Hello, World!' correctly.


* CPSC 115L home page
Valid HTML 4.01!