CSci 157 - Lab 1

Purpose: The purpose of this lab is to take a "test drive" of the Python programming environment we use to write programs in this course.

It is also intended to help you start getting comfortable with the Linux computing environment. Linux was designed to provide the same functionality as the older operating system Unix, developed at Bell Labs and eventually licensed by AT&T. Unlike Unix, however, Linux is open-source (meaning its code base is available to anyone to read and modify) and free of charge. Since many of the tools and commands are the same, I'll often use the names "Unix" and "Linux" interchangeably.

You Keep Saying "Linux" but the computer says "Ubuntu"

Ubuntu is a Linux distribution. There are dozens if not hundreds of Linux distributions which are made available by individuals or companies for different purposes. You can think of Ubuntu as the base Linux system packaged with a set of generally useful apps, configured for easy use. Underneath the graphics, it's Linux.
 

Warmup - Using your account

  1. Login to one of the lab computers, using your Banner username and password. When successful, you'll see a home screen with a task bar on the left hand side.
     
  2. Start the web browser and navigate to this page.

  3. Terminal is the home of the command-line interface (also called the command shell), which is commonly used to interact with the computer in this course.
    We'll use it to enter commands and start programs.

    To start the Terminal first click Activities (at the top left of the screen) and start typing the word Terminal in the search bar that is displayed. When you see the Terminal icon, click it. The Terminal program should be displayed.

    We'll use Terminal often enough that you should make a shortcut. Right-click on the Terminal icon on the left (the task bar) and select Add to Favorites. Now you have a shortcut that will open a Terminal anytime you click it. Other commonly-used shortcuts are already on the Launcher: one for the Firefox web browser, a word processor, and so on.

  4. Terminal is displaying a prompt - your username combined with the name of the computer you are logged into, followed by the dollar sign $. When the prompt is displayed, the computer is waiting for you to enter a command.

Part 1 - Trying out the Python shell

  1. In Terminal, issue the following commands, one at a time:
    mkdir CS157
    cd CS157
    python
    The command mkdir creates a new directory called CS157, the command cd 'enters' the new directory, and python starts the Python shell. As shown in class, when the Python system starts it displays its own prompt (>>>) and waits for input.
     
  2. At the Python prompt (>>>), enter each of these statements in turn. After each statement, wait for the prompt to be displayed before entering the next.
    Here the names a, b, and c are called variables.
    a = 3.14159
    b = 11.2
    c = a*(b**2)
    print('The area is', c)
    Based on all this, what operation do you think the * performs? What operation does ** perform?
     
  3. The Python shell allows you to scroll through your history using the Up and Down arrows. Try typing the Up Arrow and then Enter. You should have just executed that last print statement again. 

    You should be aware that the standard cut/paste keystrokes don't work in the Linux Terminal. To paste copied text into the Terminal window, right-click in the window to get a pop-up menu. You'll see options for Copy and Paste in the menu.

  4. This example does the same thing as #2, but with better, more descriptive names. Better names make programs easier to read and understand. Run these and make sure they give the same result. To save typing, you could scroll back through your history and select the most similar statement, then use the left and right arrows to move to the name and edit it to the new name.
    pi = 3.14159
    radius = 11.2
    area = pi*(radius**2)
    print('The area is', area)
  5. Try these, which show different ways of printing the same results. Be careful to get the spaces, commas, and apostrophes in the right spots:
    num = 30000000
    fraction = 1/2
    print(int(num*fraction), 'is', str(fraction*100) + '%', 'of', num)
    print(f'{int(num*fraction)} is {fraction*100}% of {num}')
    
    print(f'{3.14159:.2f}')
    print(f'{num*fraction:,.0f} is {fraction*100}% of {num:,}')
    These are a little tricky to figure out, and just illustrate Python's flexibility in printing results in different formats.
     
  6. The expression 1/2 in the previous example is actually a division operation, not a representation of a fraction. To see this, enter each of these expressions at the prompt and note the results:
    1/2
    18/3
    int(18/3)
  7. Now try the following - enter each statement in turn at the prompt:
    name = input('Enter your name: ')
    
    print('Are you really ' + name + '?')
    print(f'Are you really {name}?')
    
    n = input('Enter an integer: ')
    print(n, 'is of type', type(n))
    
  8.  These examples illustrate that Python can use character sets other than English. You'll want to copy and paste these directly into the shell:
    print('Hola, mi amigo. ¿Cómo estás?')
    print('mluvíte anglicky?')
    print('???? ?? ????????? ????? ??')
  9. There are several ways to exit the Python shell. You can type quit() at the next prompt, but the fast way is to first hold down the Control key (marked Ctrl on our keyboards) and press the d key. You'll often see this written as <Ctrl-D>.

Part 2 - Running Python scripts (programs)

In which we try a simple program and then see how to break things. When I ask you to download a program file, use the "Save As..." option (if using the Firefox web browser) and navigate to your CS157 folder to save.

  1. Download this program into your CS157 folder: futval.py
     
  2. To run the program, simply enter the command python futval.py

    When the program prompts for values, use 10000 (no commas) and 0.04. The result should be something like
       14802.442849183446

  3. That result doesn't look much like a monetary value, does it? Let's modify the program to improve the output. First try the gedit text editor. At the prompt, enter gedit futval.py and scroll down to the last print statement. Right after it, add this line:

       print(f'The value in 10 years is: ${principal:,.2f}')
     
  4. Save the file and close gedit. Now run the program as before and compare the two results given. Think about how the formatting of the last line was achieved.

  5.  Run the program one last time, but enter 10,000 instead of 10000 when prompted. What happens? Make a note of the last line of the error message; we'll talk about this next class.