Programming Lab 1 – Getting Started with Python.
Computer Science 111
Computer Science I with Java and Python
Fall, 2019
Section 900, CRN 42540
Programming Lab 1 – Getting Started with Python
Before beginning this lab, you should have the Python language on your system and ready to go. Appendix A describes
how to download and install Python. The software should already be installed in the College’s computer classrooms and
labs.
Python is an interpreted language. Python instructions will be interpreted one-at-a-time as they are typed into a Python
shell. A shell is a command driven interface, such as the Windows DOS prompt shell or the MySQL DBMS shell for
entering MySQL commands. Linux systems often include a C shell which will run C language instructions when they are
entered.
Python commands will also be executed one-at-a-time when a source code file is run through the interpreter. This
means there are two different ways to use Python:
1. You can type Python instructions directly into a Python shell one at a time. They will be executed as they are
entered.
2. You can create and save a source code file using any standard text editor and then run the file -- Notepad,
NotePad++, mac TextEdit, vi, Emacs, and so on. You should not use a word processor that formats text or uses
special characters, such as Microsoft Word. Even if you save the document as plain text, it may insert characters
that could cause problems for an interpreter.
The IDLE IDE includes both a Python shell and a Python text editor. In this lab, and for the remainder of the book, we
will use the Python IDLE shell and text editor for Python programming, unless otherwise indicated.
Lab 1, Part 1 – Hello World! For this lab we will create a simple program that displays the message “Hello World!” – traditionally the first program
students learn to write in a new programming language.
Hello World appeared in A Tutorial for the B Programming language written by Brian Kernighan at Bell labs in 1972. In
1978, his Hello World program became famous as the first program in the Kernighan and Ritchie book, The C
Programming Language. It has since become a custom for programmers to try the Hello World program as the first
program with a new compiler or interpreter, or when learning a new programming language.
The Hello World program is available in over 300 different languages online at several Websites, such as: http://www.mycplus.com/featured-articles/hello-world-programs-in-300-programming-languages
Let’s get started.
STEP 1.
Click the icon to start Python IDLE on your system. The image on the
right shows what this icon looks like on a typical Windows 10 system.
STEP 2.
The python shell will open. Please note that IDLE can be configured to open the text editor instead.
The image below shows the shell window. It is a simple window in which you can enter commands and interact with the
Python interpreter.
STEP 3.
The title on the shell window tells us which version of the shell we are using. In this case, it is the Python 3.6.2 Shell,
which is the version loaded on computers at Community College of Philadelphia. It is compatible with the newest
version, Python 3.7.4.
The shell also has a menu bar with several entries for command menus, such as File, Edit, etc. We can now enter Python
commands, or use the file menu to work with an existing Python file.
We will enter a simple Python command to print the message Hello, world!
Type the Python command
print(“Hello, world!”)
Then press enter.
You should see the message printed in the shell, as shown below.
Notice the color coding in the shell – python commands are in purple. The output from the command is blue. The
phrase “Hello, world!” in green is a string literal – a character string value, which we will learn more about in chapter 2.
The command prints exactly what we tell it to. This single line in Python is the entire Hello, world! program in Python. In
this case, however, we did not save it as a program … we simply entered it as a direct command in the Python shell.
STEP 4.
Now we will create a Python script for the Hello, world! program. A Python script is a saved set of Python instructions – a
program that can be saved, loaded, edited, and run as needed.
To work with Python scripts from the shell, we need to use the file menu to get to the Python editor.
Click the New File option on the File menu. The Python editor will open. It is shown below.
Notice that it looks very similar to the shell, but the title and menu are different. The title will be the name of the
Python script currently being edited. Since the there is no file saved yet, the title is Untitled. The menu bar shows
menus than are different from the shell menus.
We can use the File menu to open an existing Python script, save scripts, print scripts, and so on.
Notice that there is a Run menu. This can be used to run a script. We will enter a script, save it, then run the script.
STEP 5.
Enter the same command we used in the Python shell –
print("Hello, world!")
Nothing will happen yet.
STEP 6.
A new Python script must be saved before it can be run. Be careful where you save Python scripts and remember
where you save them. You should have a directory or folder on your system where it will be easy to find your Python
scripts. The use of the “My Documents” folder in Windows is discouraged. Generally, it is best to save Python scripts in
their own folder. For now, it up to you to decide where to save your Python scripts. It is not critical yet. Your instructor
may have some suggestions for the class.
In many cases, the names of Python scripts don’t matter, but for some purposes, the names should be all lower case,
with no spaces. Underscore characters can be used, but the Python style guide discourages their use. Remember, one of
the guiding principles of Python is simplicity. Names should be meaningful, but simple. The operating system of your
computer will determine whether or not file names are case sensitive. For now, it is probably best to use all lowercase
characters.
We will call our file “hello.py”. Python script files should end with the “.py” extension.
Click the Save As option on the File menu, then select a folder in which to save your file and click Save.
STEP 7.
Now we can run the script.
Click Run on the menu bar to open the Run menu.
It is shown on the right. Notice that it has options to return to the Python Shell,
Check a Module, or Run a Module. Each file that contains a Python script is a
Python module.
Here is the text from the Help menu describing these commands:
Python Shell
Open or wake up the Python Shell window.
Check Module
Check the syntax of the module currently open in the Editor window. If the module has not been saved IDLE will either
prompt the user to save or autosave, as selected in the General tab of the Idle Settings dialog. If there is a syntax error,
the approximate location is indicated in the Editor window.
Run Module
Do Check Module (above). If no error, restart the shell to clean the environment, then execute the module. Output is
displayed in the Shell window. Note that output requires use of print or write. When execution is complete, the Shell
retains focus and displays a prompt. At this point, one may interactively explore the result of execution. This is similar to
executing a file with python -i file at a command line.
There are many commands and features in Python. The help menu will provide descriptions of many of these. But be
careful not to become overloaded by trying to learn too much at once. Learning to use features as you need to use
them in the Labs in this book should work well.
STEP 8.
Notice that the menus show keyboard shortcuts for many common commands.
Click the Run Module option on the Run menu to run the module currently open in the Python editor.
If you have followed the steps in this tutorial and your script is correct, you will be returned to the Python shell, the
script will run, and the editor should look like this:
A lot has happened quickly. The computer returned to the shell, checked the script for errors, then ran the script and
displayed its output in the shell.
Notice that we can still see the results of our past work directly in the shell – our previous work entering and running the
print(“Hello, world!”) command directly in the shell. After that, the RESTART message tells us that the shell
restarted and ran the hello.py script. It shows us the full path name of the script -- C:\Users\cherb\Documents\Python files\hello.py in this case.
Finally, the output from our script is shown – the message Hello, world!
Lab 1, Part 2 – Software Documentation We're not finished yet. Your program is correct because it does what it is supposed to do – in this case, to simply print
"Hello, world!" – yet, your program is not complete. It lacks proper documentation, which is an important part of
programming. Software documentation includes messages, notes, and literature that describe software, including
information about who wrote the software, when it was written, what it is supposed to do, how it works, how to use it,
and so on.
There are three main types of documentation:
1. run-time documentation – messages that appear for the user as the software runs.
2. internal documentation – comments that appear in the software's source code.
3. external documentation – literature describing the software, including text files, pamphlets and similar
material.
The nature and scope of your own software documentation will grow along with the nature and scope of the software
you write. For now, you should simply include identifying comments at the top of the source code file. They should
include the name of the source code file, the date the software was last edited and the name of the person who edited
it, and a brief description of the purpose of the software. Be sure to include this information, along with any other
comments your instructor directs you to include, in your programming assignments for this course. The quality of your
documentation can affect your grades.
Comments in source code are notes that appear in the source code, but which are ignored by interpreters and compilers
and do not affect how the code runs. They are intended to help programmers reading the source code to be able to
better understand the software. They are an important part of programming. Many instructors and many corporations
have their own rules for software documentation.
Comments in Python begin with a hashtag character and extend to end of the line in which the comment begins. A
python comment can be on a line all by itself, or at the end of a line following a Python instruction. A comment at the
end of a line following an instruction is known as an inline comment.
Some languages have multi-line comments, but Python does not – each line of documentation is a separate comment.
A docstring is a string literal used as a comment in Python code. The use of docstrings is discussed in the next chapter.
The image below shows a Python program with comments in red. This is how they appear in the IDLE IDE.
STEP 9.
Add comments similar to the following, bit with your name, etc., to the top of your hello.py source code file, then save
the file again.
# hello.py
# Hello, World! program for CSCI 111, section 001
# last edited Jan 16, 2018 by C. Herbert
Congratulations. You have finished all of the steps in this lab, but you’re not quite done yet.
You have seen how to run Python commands directly, and how to create, edit, save and run Python scripts. The only
thing left to do is to start learning more about Python. You can get a good start on this by taking some time to play
around with the Python shell and editor. Here are some suggestions:
1. Try to get Python to print other messages – perhaps two or three in a row.
2. Make some mistakes on purpose to see how Python reacts. What if you spell print wrong?
3. What happens if you ask the shell for help? Try typing each of the following to see:
a. help
b. help print
c. help(print)
4. The website http://cwherbert.com/python has information and resources for learning Python, including links to
some Python tutorials. Try some things from the first lesson or two in some of these tutorials to see which ones
might be best for you to use.
5. Review what you’ve learned this week, then move on to the next week.