Save Your Spot in Coding 101

Thanks for your interest in the free coding 101 course. This is a class for BEGINNERS. If you know nothing at all about programming you’re right on track!

Due to high demand, we require prospective students to complete a short quiz in order to reserve a potential spot in the class. The quiz is NOT being graded, but it will be used to gauge a student’s interest in the course.

Instructions

  1. Read the Chapter 1 of the online textbook (on this page)
  2. Submit the online quiz (free free to retake the quiz as much as you’d like)

If you have any questions about the material, quiz, or class, don’t hesitate to send an email to coding101@devetry.com

The Way of the Program

The goal of this book is to teach you to think like a computer scientist. This way of thinking combines some of the best features of mathematics, engineering, and natural science. Like mathematicians, computer scientists use formal languages to denote ideas (specifically computations). Like engineers, they design things, assembling components into systems and evaluating tradeoffs among alternatives. Like scientists, they observe the behavior of complex systems, form hypotheses, and test predictions.

The single most important skill for a computer scientist is problem solving. Problem solving means the ability to formulate problems, think creatively about solutions, and express a solution clearly and accurately. As it turns out, the process of learning to program is an excellent opportunity to practice problem solving skills. That’s why this chapter is called, The Way of the Program.

On one level, you will be learning to program, a useful skill by itself. On another level, you will use programming as a means to an end. As we go along, that end will become clearer.

Algorithms

Since problem solving is a central part of computer science, the solutions that you create through the problem solving process are also important. In computer science, we refer to these solutions as algorithms. An algorithm is a step by step list of instructions that if followed exactly will solve the problem under consideration.

One of the characteristics of algorithms is that they do not require any intelligence to carry out (i.e., they can be done by computers). They are mechanical processes in which each step follows from the last according to a simple set of rules. On the other hand, determining the steps of each algorithm — what exactly needs to be done, and in what order, to solve the problem at hand — requires a great deal of thought and experimentation. So while the execution of the algorithm may be boring and require no intelligence, it is the process of designing algorithms that is interesting, intellectually challenging, and a central part of what we call programming.

Some of the things that people do naturally, without difficulty or conscious thought, are the hardest to express algorithmically. Understanding natural language is a good example. We all do it, but so far no one has been able to explain how we do it, at least not in the form of a step-by-step mechanical algorithm.

Our goal in computer science is to take a problem and develop an algorithm that can serve as a general solution. Once we have such a solution, we can use our computer to automate the execution. As noted above, programming is a skill that allows a computer scientist to take an algorithm and represent it in a program that can be followed by a computer. These programs are written in programming languages.

Check your understanding

    What is the most important skill for a computer scientist?
  • To think like a computer.
  • Computers do not think, they only do what we humans tell them to do via programs.
  • To be able to write code really well.
  • While it is necessary for most computer scientists to know how to write code, it is not the most important skill.
  • To be able to solve problems.
  • Computer scientists are all about solving problems. We use computers to automate solutions to problems and to do things faster and more accurately than we can do manually.
  • To be really good at math.
  • Computer science and math are similar in many ways and it helps to have a strong mathematical foundation, but you do not have to be good at math to be a good computer scientist.
    An algorithm is:
  • A solution to a problem that can be solved by a computer.
  • While it is true that algorithms often do solve problems, this is not the best answer. An algorithm is more than just the solution to the problem for a computer. An algorithm can be used to solve all sorts of problems, including those that have nothing to do with computers.
  • A step by step list of instructions that if followed exactly will solve the problem under consideration.
  • Algorithms are like recipes: they must be followed exactly, they must be clear and unambiguous, and they must end.
  • A series of instructions implemented in a programming language.
  • Programming languages are used to express algorithms, but an algorithm does not have to be expressed in terms of a programming language.
  • A special kind of notation used by computer scientists.
  • Computer scientists sometimes use a special notation to illustrate or document an algorithm, but this is not the definition of an algorithm.

The Python Programming Language

The programming language you will be learning is Python. Python is an example of a high-level language; other high-level languages you might have heard of are C++, PHP, and Java.

As you might infer from the name “high-level language”, there are also low-level languages, sometimes referred to as machine languages or assembly languages. Machine language is the encoding of instructions in binary so that they can be directly executed by the computer. Assembly language uses a slightly easier format to refer to the low level instructions. Loosely speaking, computers can only execute programs written in low-level languages. To be exact, computers can actually only execute programs written in machine language. Thus, programs written in a high-level language (and even those in an assembly language) have to be processed before they can run. This extra processing takes some time, which is a small disadvantage of high-level languages. However, the advantages to high-level languages are enormous.

First, it is much easier to program in a high-level language. Programs written in a high-level language take less time to write, they are shorter and easier to read, and they are more likely to be correct. Second, high-level languages are portable, meaning that they can run on different kinds of computers with few or no modifications. Low-level programs can run on only one kind of computer and have to be rewritten to run on another.

Due to these advantages, almost all programs are written in high-level languages. Low-level languages are used only for a few specialized applications.

Two kinds of programs process high-level languages into low-level languages: interpreters and compilers. An interpreter reads a high-level program and executes it, meaning that it does what the program says. It processes the program a little at a time, alternately reading lines and performing computations.

Interpret illustration

A compiler reads the program and translates it completely before the program starts running. In this case, the high-level program is called the source code, and the translated program is called the object code or the executable. Once a program is compiled, you can execute it repeatedly without further translation.

Compile illustration

Many modern languages use both processes. They are first compiled into a low-level language, called byte code, and then interpreted by a program called a virtual machine. Python uses both processes, but because of the way programmers interact with it, it is usually considered an interpreted language.

There are two ways to use the Python interpreter: shell mode and program mode. In shell mode, you type Python expressions into the Python shell, and the interpreter immediately shows the result. The example below shows the Python shell at work.

$ python
Python 3.2 (r32:88445, Mar 25 2011, 19:28:28)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 2 + 3
5
>>>

The >>> is called the Python prompt. The interpreter uses the prompt to indicate that it is ready for instructions. We typed 2 + 3. The interpreter evaluated our expression and replied 5. On the next line it gave a new prompt indicating that it is ready for more input.

Working directly in the interpreter is convenient for testing short bits of code because you get immediate feedback. Think of it as scratch paper used to help you work out problems.

Alternatively, you can write an entire program by placing lines of Python instructions in a file and then use the interpreter to execute the contents of the file as a whole. Such a file is often referred to as source code. For example, we used a text editor to create a source code file named first_program.py with the following contents:

print("My first program adds two numbers, 2 and 3:")
print(2 + 3)

By convention, files that contain Python programs have names that end with .py. Following this convention will help your operating system and other programs identify a file as containing python code.

$ python first_program.py
My first program adds two numbers, 2 and 3:
5

These examples show Python being run from a Unix command line. In other development environments, the details of executing programs may differ. Also, most programs are more interesting than this one.

Check your understanding

    Source code is another name for:
  • the instructions in a program, stored in a file.
  • The file that contains the instructions written in the high-level language is called the source code file.
  • the language that you are programming in (e.g., Python).
  • This language is simply called the programming language.
  • the environment/tool in which you are programming.
  • The environment may be called the IDE, or integrated development environment, though not always.
  • the number (or “code”) that you must input at the top of each program to tell the computer how to execute your program.
  • There is no such number that you must type in at the start of your program.
    What is the difference between a high-level programming language and a low-level programming language?
  • It is high-level if you are standing and low-level if you are sitting.
  • In this case high and low have nothing to do with altitude.
  • It is high-level if you are programming for a computer and low-level if you are programming for a phone or mobile device.
  • High and low have nothing to do with the type of device you are programming for. Instead, look at what it takes to run the program written in the language.
  • It is high-level if the program must be processed before it can run, and low-level if the computer can execute it without additional processing.
  • Python is a high-level language but must be interpreted into machine code (binary) before it can be executed.
  • It is high-level if it's easy to program in and is very short; it is low-level if it is really hard to program in and the programs are really long.
  • While it is true that it is generally easier to program in a high-level language and programs written in a high-level language are usually shorter, this is not always the case.
    Pick the best replacements for 1 and 2 in the following sentence: When comparing compilers and interpreters, a compiler is like 1 while an interpreter is like 2.
  • 1 = a process, 2 = a function
  • Compiling is a software process, and running the interpreter is invoking a function, but how is a process different than a function?
  • 1 = translating an entire book, 2 = translating a line at a time
  • Compilers take the entire source code and produce object code or the executable and interpreters execute the code line by line.
  • 1 = software, 2 = hardware
  • Both compilers and interpreters are software.
  • 1 = object code, 2 = byte code
  • Compilers can produce object code or byte code depending on the language. An interpreter produces neither.

Executing Python in this Book

This book provides two special ways to execute Python programs. Both techniques are designed to assist you as you learn the Python programming language. They will help you increase your understanding of how Python programs work.

First, you can write, modify, and execute programs using a unique activecode interpreter that allows you to execute Python code right in the text itself (right from the web browser). Although this is certainly not the way real programs are written, it provides an excellent environment for learning a programming language like Python since you can experiment with the language as you are reading.

Take a look at the activecode interpreter in action. If we use the Python code from the previous example and make it active, you will see that it can be executed directly by pressing the run button. Try pressing the run button below.

Now try modifying the activecode program shown above. First, modify the string in the first print statement by changing the word adds to the word multiplies. Now press run. You can see that the result of the program has changed. However, it still prints “5” as the answer. Modify the second print statement by changing the addition symbol, the “+”, to the multiplication symbol, “*”. Press run to see the new results.

In addition to activecode, you can also execute Python code with the assistance of a unique visualization tool. This tool, known as codelens, allows you to control the step by step execution of a program. It also lets you see the values of all variables as they are created and modified. The following example shows codelens in action on the same program as we saw above. Note that in activecode, the source code executes from beginning to end and you can see the final result. In codelens you can see and control the step by step progress. Note that the red arrow always points to the next line of code that is going to be executed. The light green arrow points to the line that was just executed.

(firstexample)

The examples in this book use a mixture of the standard Python interpreter, source code, activecode, and codelens. You will be able to tell which is which by looking for either the Python prompt in the case of a shell mode program, the run button for the activecode, or the forward/backward buttons for codelens.

Check your understanding

    The activecode interpreter allows you to (select all that apply):
  • save programs and reload saved programs.
  • Saving and reloading programs using activecode is not available at this time.
  • type in Python source code.
  • You are not limited to running the examples that are already there. Try adding to them and creating your own.
  • execute Python code right in the text itself within the web browser.
  • The activecode interpreter will allow you type Python code into the textbox and then you can see it execute as the interpreter interprets and executes the source code.
  • receive a yes/no answer about whether your code is correct or not.
  • Although you can (and should) verify that your code is correct by examining its output, activecode will not directly tell you whether you have correctly implemented your program.
    Codelens allows you to (select all that apply):
  • measure the speed of a program’s execution.
  • In fact, codelens steps through each line one by one as you click, which is MUCH slower than the Python interpreter.
  • control the step by step execution of a program.
  • By using codelens, you can control the execution of a program step by step. You can even go backwards!
  • write and execute your own Python code.
  • Codelens works only for the pre-programmed examples.
  • execute the Python code that is in codelens.
  • By stepping forward through the Python code in codelens, you are executing the Python program.

More About Programs

A program is a sequence of instructions that specifies how to perform a computation. The computation might be something as complex as rendering an html page in a web browser or encoding a video and streaming it across the network. It can also be a symbolic computation, such as searching for and replacing text in a document or (strangely enough) compiling a program.

The details look different in different languages, but a few basic instructions appear in just about every language.

input
Get data from the keyboard, a file, or some other device.
output
Display data on the screen or send data to a file or other device.
math and logic
Perform basic mathematical operations like addition and multiplication and logical operations like and, or, and not.
conditional execution
Check for certain conditions and execute the appropriate sequence of statements.
repetition
Perform some action repeatedly, usually with some variation.

Believe it or not, that’s pretty much all there is to it. Every program you’ve ever used, no matter how complicated, is made up of instructions that look more or less like these. Thus, we can describe programming as the process of breaking a large, complex task into smaller and smaller subtasks until the subtasks are simple enough to be performed with sequences of these basic instructions.

Check your understanding

    A program is:
  • a sequence of instructions that specifies how to perform a computation.
  • It is just step-by-step instructions that the computer can understand and execute. Programs often implement algorithms, but note that algorithms are typically less precise than programs and do not have to be written in a programming language.
  • something you follow along at a play or concert.
  • True, but not in this context. We mean a program as related to a computer.
  • a computation, even a symbolic computation.
  • A program can perform a computation, but by itself it is not one.
  • the same thing as an algorithm.
  • Programs often implement algorithms, but they are not the same thing. An algorithm is a step by step list of instructions, but those instructions are not necessarily precise enough for a computer to follow. A program must be written in a programming language that the computer knows how to interpret.

A Typical First Program

Traditionally, the first program written in a new language is called Hello, World! because all it does is display the words, “Hello, World!”. In Python, the source code looks like this.

print("Hello, World!")

This is an example of using the print function, which doesn’t actually print anything on paper. It displays a value on the screen. In this case, the result is the phrase:

Hello, World!

Here is the example in activecode. Give it a try!

The quotation marks in the program mark the beginning and end of the value. They don’t appear in the result.

Some people judge the quality of a programming language by the simplicity of the Hello, World! program. By this standard, Python does about as well as possible.

Check your understanding

    The print function:
  • sends information to the printer to be printed on paper.
  • Within the Python programming language, the print function has nothing to do with the printer.
  • displays a value on the screen.
  • Yes, the print function is used to display the value of the thing being printed.
  • tells the computer to put the information in print, rather than cursive, format.
  • The format of the information is called its font and has nothing to do with the print function.
  • tells the computer to speak the information.
  • That would be a different function.

Comments

As programs get bigger and more complicated, they get more difficult to read. Formal languages are dense, and it is often difficult to look at a piece of code and figure out what it is doing, or why. For this reason, it is a good idea to add notes to your programs to explain in natural language what the program is doing. These notes are called comments.

A comment in a computer program is text that is intended only for the human reader - it is completely ignored by the interpreter. In Python, the # token starts a comment. The rest of the line is ignored. Here is a new version of Hello, World!.

Notice that when you run this program, it still only prints the phrase Hello, World! None of the comments appear. You’ll also notice that we’ve left a blank line in the program. Blank lines are also ignored by the interpreter, but comments and blank lines can make your programs much easier for humans to parse. Use them liberally!

Check your understanding

    What are comments for?
  • To tell the computer what you mean in your program.
  • Comments are ignored by the computer.
  • For the people who are reading your code to know, in natural language, what the program is doing.
  • The computer ignores comments. It’s for the humans that will “consume” your program.
  • Nothing, they are extraneous information that is not needed.
  • Comments can provide much needed information for anyone reading the program.
  • Nothing in a short program. They are only needed for really large programs.
  • Even small programs benefit from comments.

Glossary

activecode
A unique interpreter environment that allows Python to be executed from within a web browser.
algorithm
A general step by step process for solving a problem.
byte code
An intermediate language between source code and object code. Many modern languages first compile source code into byte code and then interpret the byte code with a program called a virtual machine.
codelens
An interactive environment that allows the user to control the step by step execution of a Python program.
comment
Information in a program that is meant for other programmers (or anyone reading the source code) and has no effect on the execution of the program.
compile
To translate a program written in a high-level language into a low-level language all at once, in preparation for later execution.
executable
Another name for object code that is ready to be executed.
formal language
Any one of the languages that people have designed for specific purposes, such as representing mathematical ideas or computer programs; all programming languages are formal languages.
high-level language
A programming language like Python that is designed to be easy for humans to read and write.
interpret
To execute a program in a high-level language by translating it one line at a time.
low-level language
A programming language that is designed to be easy for a computer to execute; also called machine language or assembly language.
natural language
Any one of the languages that people speak that evolved naturally.
object code
The output of the compiler after it translates the program.
portability
A property of a program that can run on more than one kind of computer.
print function
A function used in a program or script that causes the Python interpreter to display a value on its output device.
problem solving
The process of formulating a problem, finding a solution, and expressing the solution.
program
A sequence of instructions that specifies to a computer actions and computations to be performed.
programming language
A formal notation for representing solutions.
Python shell
An interactive user interface to the Python interpreter. The user of a Python shell types commands at the prompt (>>>), and presses the return key to send these commands immediately to the interpreter for processing.
shell mode
A style of using Python where we type expressions at the command prompt, and the results are shown immediately. Contrast with source code, and see the entry under Python shell.
source code
A program, stored in a file, in a high-level language before being compiled or interpreted.
token
One of the basic elements of the syntactic structure of a program, analogous to a word in a natural language.

Quiz