1. The way of the program

How computer scientists think

Like mathematicians
use formal languages to denote ideas
Like engineers
design things, assembling components into systems and evaluating tradeoffs among alternatives
Like scientists
observe the behavior of complex systems, form hypotheses, and test predictions

Skill for computer scientist

The single most important skill for a computer scientist is problem solving

  • the ability to formulate problems,
  • think creatively about solutions,
  • and express a solution clearly and accurately.

 

The way to solve problems: learning to program

Algorithms

An algorithm is a step by step list of instructions that if followed exactly will solve the problem under consideration.

Programming is a skill that allows a computer scientist to take an algorithm and represent it in a notation (a program) that can be followed by a computer.

These programs are written in programming languages.

The Python Programming Language

Python is a high-level language

  • easier to program in a high-level language so programs
    • take less time to write
    • shorter and easier to read
    • more likely to be correct
  • high-level languages are portable

Interpreter vs Compiler

 

1. Interpreter
source code → interpreter → output
2. Compiler
source code → compiler → object code → executor → output

Python interpreter

  • python shell
  • ipython shell
  • jupyter notebook
  • python program

python shell

$ python3
Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:52:12) 
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 2 + 3
5
>>>

ipython shell

$ python3 -m IPython
Python 3.5.2 (default, Jul  2 2016, 17:52:12) 
IPython 5.1.0 -- An enhanced Interactive Python.

In [1]: 2 + 3
Out[1]: 5

In [2]:

jupyter notebook

IPython is now the name of the Python kernel for the Jupyter Notebook.

$ jupyter notebook

will automatically open a browser window showing your home directory.

then you can New a Python Notebook

python program

$ cat hello.py
print("Hello world!")
$ python3 hello.py
Hello world!

More About Programs

A program is a sequence of instructions that specifies how to perform a computation.

  • Input
  • Output
  • Math and logic
  • Conditional execution
  • Repetition

What is debugging

Programming errors are called bugs and the process of tracking them down and correcting them is called debugging.

Three kinds of errors can occur in a program:

  • syntax errors
  • runtime errors
  • semantic errors

Syntax errors

Python can only execute a program if the program is syntactically correct; otherwise, the process fails and returns an error message.

Syntax refers to the structure of a program and the rules about that structure.

Exercise

1. Which of the following is a syntax error?

(A) Attempting to divide by 0.
(B) Forgetting a colon at the end of a statement where one is required.
(C) Forgetting to divide by 100 when printing a percentage amount.
2. Who or what typically finds syntax errors?

(A) The programmer.
(B) The compiler / interpreter.
(C) The computer.
(D) The teacher / instructor.

Runtime Errors

Runtime error does not appear until you run the program.

These errors are also called exceptions because they usually indicate that something exceptional (and bad) has happened.

Exercise

Which of the following is a run-time error?

(A) Attempting to divide by 0.
(B) Forgetting a colon at the end of a statement where one is required.
(C) Forgetting to divide by 100 when printing a percentage amount.

Semantic Errors

If there is a semantic error in your program, it will run successfully in the sense that the computer will not generate any error messages.

However, your program will not do the right thing. It will do something else.

The problem is that the program you wrote is not the program you wanted to write. The meaning of the program (its semantics) is wrong.

Exercise

Which of the following is a semantic error?

(A) Attempting to divide by 0.
(B) Forgetting a semi-colon at the end of a statement where one is required.
(C) Forgetting to divide by 100 when printing a percentage amount.

Experimental Debugging

Although it can be frustrating, debugging is one of the most intellectually rich, challenging, and interesting parts of programming.

In some ways, debugging is like detective work.

Debugging is also like an experimental science.

Exercise

The difference between programming and debugging is:

(A) programming is the process of writing and gradually debugging a program until it does what you want.
(B) programming is creative and debugging is routine.
(C) programming is fun and debugging is work.
(D) there is no difference between them.

Formal and Natural Languages

Natural languages are the languages that people speak. They were not designed by people; they evolved naturally.

Formal languages are languages that are designed by people for specific applications.

 

Programming languages are formal languages that have been designed to express computations.

Formal languages have strict rules about syntax

Syntax rules come in two flavors: tokens and structure.

 

Tokens
Tokens are the basic elements of the language
Structure of a statement
the way the tokens are arranged

 

Formal and natural languages differences


ambiguity
redundancy
literalness

Exercise

1. The differences between natural and formal languages include:
(A) natural languages can be parsed while formal languages cannot.
(B) ambiguity, redundancy, and literalness.
(C) there are no differences between natural and formal languages.
(D) tokens, structure, syntax, and semantics.
2. Reading a program is like reading other kinds of text.
(A) True
(B) False

A Typical First Program

In [2]:
print("Hello World!")
Hello World!

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.

#---------------------------------------------------
# This demo program shows off how elegant Python is!
# Written by Joe Soap, December 2010.
# Anyone may freely copy or modify this program.
#---------------------------------------------------

print("Hello, World!")     # Isn't this easy!

Exercise

What are comments for?
(A) To tell the computer what you mean in your program.
(B) For the people who are reading your code to know, in natural language, what the program is doing.
(C) Nothing, they are extraneous information that is not needed.
(D) Nothing in a short program. They are only needed for really large programs.

Glossary

algorithm

A general step by step process for solving a problem.

bug

An error in a program.

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.

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.

debugging

The process of finding and removing any of the three kinds of programming errors.

exception

Another name for a runtime error.

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.

parse

To examine a program and analyze the syntactic structure.

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.

runtime error

An error that does not occur until the program has started to execute but that prevents the program from continuing.

semantic error

An error in a program that makes it do something other than what the programmer intended.

semantics

The meaning of a program.

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.

syntax

The structure of a program.

syntax error

An error in a program that makes it impossible to parse — and therefore impossible to interpret.

token

One of the basic elements of the syntactic structure of a program, analogous to a word in a natural language.

In [ ]: