2.Core Elements of Programs
一、Programming Language
思路分析:
- Objects
- Expression
- Non-scalable


1.Programming Language
Goal:
- Need a way to describe algorithmic steps such that computer can use them to execute process
- Programming language defines syntax and semantics needed to translate our computational ideas into mechanical steps
2.Operations for programming languages
-
基本原理图
- Low level language uses instructions similar to internal control unit:
- Move data from one location to another
- Execute a simple ALU operation
- Jump to new point in sequence based on test
- Checker confirms syntax, static semantics correct
- Interpreter just follows sequence of simple instructions
- A high level language uses more abstract terms – invert a matrix, compute a function
- In a compiled language, those abstractions are converted back into low level instructions, then executed
- In an interpreted language, special program converts source code to internal data structure, then interpreter sequentially converts each step into low level machine instruction and executes
- We are going to use Python, which belongs to this class of programming languages
3.Python programs
Program (or script) is a sequence of definitions and commands
Definitions evaluated and commands executed by Python interpreter in a shell
Can be typed directly into a shell, or stored in a file that is read into the shell and evaluated
Command (or statement) instructs interpreter to do something
二、Objects and Expressions
4.Objects
- At heart, programs will manipulate data objects
- Each object has a type that defines the kinds of things programs can do to it
- Objects are:
- Scalar (i.e. cannot be subdivided), or
- Non-‐scalar (i.e. have internal structure that can be accessed)
5.Expressions
- Objects and operators can be combined to form expressions, each of which denotes an object of some type
- The syntax for most simple expressions is:
- object operator object
6.Operators on ints and floats
7.Some simple examples
> > > 3 + 5
8
>>> 3.14 * 20
62.8
>>> (2 + 3)*4
20
>>> 2 + 3*4
14
8.Performing simple operations
- Parentheses define sub-‐computations – complete these to get values before evalua9ng larger expression
> > > (2+3)\*4 > > > 5\*4 > > > 20```
- Operator precedence:
- In the absence of parentheses (within which expressions are first reduced), operators are executed le[ to right, first using **, then * and /, and then + and -!
9.Comparison operators on ints and floats
10.Operations on bools
11.Type conversions(type casting)
12.Simple means of abstractions
13.Binding variables and values
14.Changing bindings
三、Non-scalar objects
15.Non-‐scalar objects
16.Operators on strings
17.Extracting parts of strings
18.Programs (or scripts)
- While we can type expressions directly to a Python interpreter (for example using an interface such as an IDLE shell), in general we will want to include statements in a program file
- Executing an expression from a script will not produce any output; for that we need statements (not expressions), such as
– print(‘ab’)
– print(‘3’*3)
19.Provideing input
20.Some simple code
四、Three types of Programs
21.A straight line program
22.Some observations
- Comments appear after a #
23.Branching programs
24.A simple example
25.Some observations
26.We can have nested conditionals
27.And we can use compound Booleans
28. What have we added?



Eric Grimson: Welcome back to 600x.
In this second lecture we are going to start developing knowledge of programming languages.
We'll talk about how a computer converts a description of a how-to method that we write in a high level, although a semantically constrained manner, into a set of instructions that its internal circuitry can execute.
To do this, we're going to begin to introduce the basic elements of Python, the language we're going to use in this course.
We will introduce a bunch of things.
Numbers, expressions that combine numbers arithmetically, ways to compare objects like numbers, Boolean operations on logical expressions, ways to abstract expressions by giving them names, strings or collections of characters and words and operations on them, combining expressions in a linear manner, and simple ways to make decisions and to take different options or branches based on those decisions.
At the end of this lecture, you should be able to write simple programs that manipulate numbers or strings, make decisions, and print out results.