2.Core Elements of Programs

2.Core Elements of Programs

一、Programming Language


思路分析:

  • Objects
  • Expression
  • Non-scalable
Ingredient
Ingredient
IPython知识结构图
IPython知识结构图

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?

IMG_3168
IMG_3168
IMG_3169
IMG_3169
IMG_3170
IMG_3170

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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,828评论 0 23
  • 清风拂过, 有思念的味道, 还有人趴在窗前, 怅望着绿油油的远方。 夏虫鸣后, 有歌唱的少女, 还有人执着蒲扇, ...
    林树安阅读 242评论 0 1
  • 本文来自网络摘取,作者不详.侵删. XCode工程中 Targets讲解是本文要介绍的内容,相信很多人都注意到XC...
    Weelh阅读 911评论 0 3