CS 1110 - Introduction to Programming

Starting Out with Python
Chapter 1, Introduction to Computers and Programming

Objectives:

This lesson presents an overview of computer hardware and software, and provides a first look at programming and the Python language. Objectives important to this lesson:

  1. Hardware and software
  2. Data storage
  3. Programs
  4. Python
Concepts:
Chapter 1

The text begins with the very basic idea that computers are composed of two very different kinds of components, hardware and software. We classify all physical components of a system as hardware. Hardware alone is not enough to make a computer do anything useful. We also need software, which can be classified many different ways as we will see in this chapter.

The text divides hardware into five types. Most hardware that we associate with a computer will fit into these categories. A computer may have cables, ports, and adapters as well as other components, but these five types cover the essentials.

  • the CPU - the Central Processing Unit is the main component of a computer, where calculations take place; programs are loaded from secondary storage into RAM, then loaded into the CPU one or more instructions at a time when they run;
    The CPU performs various actions that belong to one of two cycles: fetch and execution. The fetch cycle is also called the instruction cycle because its primary task is to load instructions. Consider the four tasks performed by the Control Unit in a fetch cycle:
    • fetch instructions from primary storage/RAM; programs are always copied to RAM before they can be processed by the Control Unit
    • increment a pointer that holds the RAM address of the next instruction
    • separate the instruction into its parts: instruction code and inputs
    • store each instruction component in registers (special memory accessible just to the CPU)
    • translate the instruction into machine language
    • execute the instruction
      As you can see, there is a lot to do before anything in memory is ever actually used.

  • main memory - main memory, RAM or Random Access Memory, is where programs are loaded so they can be run, data is loaded so it can be used, and the operating system is loaded to operate the hardware; typically, this memory contains nothing when the computer is turned off
  • secondary memory (storage) - secondary storage is where you store anything you want to access long term: programs, the operating system, and data files; hard drives, flash drives, and solid state drives are examples of secondary storage; typically the things stored on these components continue to be there once the computer is turned off
  • input devices - typically a keyboard, mouse, touch screen, and a joy stick are all commonly used input devices; scanners, graphic pads, ,microphones, and cameras are less common, but not unusual
  • output devices - screens, speakers, printers, and storage devices are all output devices, since the computer sends information or files to them

Turning to software, the text briefly discusses two types.

  • system software - this kind of a loose category, because it contains three other categories that are not very related
    • operating system software - this is the software you use to start a computer, to give you access to devices in it, to read and store files, and to do other very general things that make a computer work
    • utility programs - the text tells us this category contains things that are not very attractive to the average consumer, like backup software, antivirus software, and file compression/decompression software
    • software development tools - this is software that makes it easier to create other software; this one does not really belong in this category, but it is here because the programs you create with it need to connect to the operating system, so you may find a different version of it for each operating system you write programs for
  • application software - finally we come to application software, which is for doing something that matters to the user, whether for a job, for school, or for some other productive activity; we could easily argue that utility software is an application software type, as is gaming software, web browsing software, word processing software, and any other software that does one particular thing well, and may do some other things, too

Computers store and handle instructions and data as digital information. Everything is stored and used as some combination of 1s and 0s, which is often compared to a set of switches that are either turned on (1) or turned off (0). The number system that people normally use is the decimal system, based on ten digits (0 through 9). The numbering system used in computers is typically the binary system, based on two digits (0 and 1). In a binary system, we can refer to a cell in memory as holding one binary digit (abbreviated as one bit) that is either a 0 or a 1.

If we only had two digits, we could only represent two things, but we can have collections of digits that can represent an unlimited number of things. In the case of computers, we often see systems that use clusters of 8 bits, which are called bytes. Each byte will represent one character or keypress in some code system, such as the American Standard Code for Information Interchange, ASCII. Files written in ASCII (pronounced like ask-key) may be written in just the original 128 characters defined by seven bits of a byte, or in the extended ASCII code that uses eight bits. See the table of defined characters on this web page. ASCII was created by ANSI, the American National Standards Institute. A more modern code system is Unicode. It was created in the late 1980s and early 1990s by a consortium of vendors to represent more characters (16 bits per symbol); later it was expanded to use four bytes for some characters, so it has the capacity to store the characters for all current languages and some dead languages. Unicode includes extended ASCII as the first 256 characters in its system.

So, that's a bit of background about bits and bytes and code systems. We came here to talk about programs, so how does a CPU use a program? As noted above, it is told to run a program. See the diagram on page 15:

  1. If the program is not already loaded in RAM, the CPU loads it there, then runs through a cycle in which it fetches each instruction.
  2. The CPU decodes the instruction, which means that it determines which internal processes it must execute for that instruction.
  3. The CPU then executes the internal operations that were called for.

The text tells us that programs are typically machine language, but they are no longer written that way. They can be written in assembly language, which is one level removed from the actual language of the CPU. Even though this was once a wonderful thing, to be able to write in the symbolic code of an assembler, it is still not clear what such code really means unless you are an expert in it, so rewriting or debugging a program written in assembly language is not a good way to learn programming.

This takes us to high level languages, which more closely resemble human languages. A list of several high level languages appears on page 17. Each is different from all the others in some way, but one thing most of them have in common is that they are hard to learn. This takes us to Python, which is a language that was meant to be easy to learn and easy to use. If you have some trouble with it this term, think about something Winston Churchill said about democracy, and apply it to Python. He said that democracy is a very poor form of government, but all the others are so much worse.

All high level languages contain similar components. Key words are typically commands, such as those in the list on page 17. Those that are not commands are used to tell the CPU how to follow a command.

High level languages also contain operators, which come several types, as show in the discussion of Python operators on this web site. Some operators are words, and some are symbols. We won't go deeply into that in this lesson.

The third thing all languages have is syntax, which would mean grammar and punctuation if we were talking about a human language. For a programming language, it means something very similar. Programs have to be written rather precisely or they will not be understood by the computer trying to run them.

On page 18, the text informs us that some languages are compiled and others are interpreted. In both cases, a program is written in what we will call source code, the high level language, often saved as a plain text file with a funny extension. Programs can also be called scripts, as most programs saved in text files are called. The funny extension for Python programs is .py

  • A compiled language requires that we run the source code through a compiler. It is a program that runs the source code file through several processes, and ends up creating a machine language file that a CPU will understand. This allows us to take that machine language file to any computer having the right kind of processor and run it without needing the compiler or the source code files. That's great until you have to change the program, which means going back to the source code, making the changes, and compiling it again to get the new version of the program.
  • An interpreted language also starts with source code, but the source code file is handed to an interpreter running on the machine where you actually want to run the program. The interpreter reads each line of the source code, turns it into machine language, hands that line to the processor, and waits for the line to be executed before starting over with the next line of source code. We can edit the source code any time we want, but we cannot run the program without having an interpreter on the computer where we want it to run.

Python is an interpreted language. Computers in our classroom should have a Python interpreter installed on them for us already. Your own computer may need to you to install it before you can run a Python program. Starting the Python interpreter will open a command line shell, which will allow you to run programs you have saved, or issue commands in interactive mode. It will look a lot like this (captured from my computer):

Note that I entered the sample command shown on page 21 twice. The first time, I entered it correctly. The second time I entered it with a syntax error: I did not type the closing parenthesis before I pressed the enter key. The Python interpreter gave me a new prompt, an ellipsis, that meant it was waiting for me to finish the command. I typed the closing parenthesis, pressed enter, and the line of code ran properly.

The text tells us to press control-z to exit the Python interpreter, but this does not work on my computer. Closing the window in which the interpreter is running does work. On page 23, we learn that we can call the Python interpreter from a command line, and pass the name of a saved file to it at the same time. The text refers to this as starting the interpreter in script mode, handing it a script directly.

Instead of running an interpreter session directly, you can probably start the IDLE shell, and integrated development view of Python. It was installed along with Python manuals and the interpreter when I downloaded my copy of Python.

Assignments

Assignments for these chapters will be found in Blackboard. We will explore that in class.