Think Python:Chapter 1:The way of the program 笔记

12 篇文章 0 订阅

目录

这是麻省理工大学(MIT)官方编程教程中Python Tutorial的内容,教材为《Think Python: How to Think Like a Computer Scientist》。这是我的学习笔记,因为水品有限,请大家多多包涵。如果有一起学习的同学可以一起交流。如笔记中错误,请一定要告诉我啊,我肯定及时改正。所有笔记的目录详见:MIT:Python Tutorial目录

这是MIT官方编程教程中Python Tutorial中的内容。本篇博客为《 Think Python: How to Think Like a Computer Scientist》的第1章 The way of the program的笔记内容。(《Think Python》 Chapter 1:The way of the program

Chapter 1:The way of the program

problem solving: the ability to formulate problems, think creatively about solutions, and express
a solution clearly and accurately(most important skill for a computer scientist).

1.1 The Python programming language

  • Python: high-level language(This extra processing takes some time); as C, C++, Perl, and Java.

  • Machine language/assembly languages: low-level languages.

  • source code: the high-level program

  • the object code or the executable:the translated program

The advantage of high-level language:

  1. much easier to program
    1. take less time to write
    2. shorter and easier to read
    3. more likely to be correct
  2. portable(便携式的)
    1. can run on different kinds of computers with few or no modifications
    2. Low-level programs can run on only one kind of computer and have to be rewritten to run on another.

Two kinds of programs process high-level languages into low-level languages: interpreters(解释程序) and compilers(编译器)

  • interpreter(解释程序): reads a high-level program and executes it

  • compilers(编译器): reads the program and translates it completely before the program starts running.

Python is considered an interpreted language because Python programs are executed by an interpreter. There are two ways to use the interpreter: interactive(交互式的) mode and script(脚本) mode.

In interactive mode, you type Python programs and the interpreter displays the result:

>>> 1 + 1
2

“>>>”:is the prompt(标志) the interpreter uses to indicate that it is ready;

script: (储藏代码的文档叫做脚本) store code in a file and use the interpreter to execute the contents of the file.

By convention(规定), Python scripts have names that end with .py.

1.2 What is a program?

A program : a sequence of instructions(指令) that specifies(指定) how to a computation(计算).

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: Perform basic mathematical operations like addition and multiplication(乘法).
  • conditional(条件) execution: Check for certain conditions and execute the appropriate code.
  • 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 pretty much like these.So you can think of 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 one of these basic instructions.**That may be a little vague, but we will come back to this topic when we talk about **algorithms.

所有的程序基本都可以看做是由这几个基础指令不断叠加而成的。

1.3 What is debugging?

  • Bugs: programming errors

  • debugging(调试以排除故障): the process of tracking(跟踪) them down

Three kinds of errors can occur in a program: syntax errors, runtime errors, and semantic errors. It is useful to distinguish between them in order to track them down more quickly.

  1. Syntax(语法) errors: Syntax refers to the structure of a program and the rules about that structure.
  2. Runtime(执行时间) errors: the error does not appear until after the program has started running. These errors are also called exceptions(异常) because they usually indicate that something exceptional(异常的) (and bad) has happened.
  3. Semantic(语义的) errors: it will run successfully in the sense that the computer will not generate any error messages, but it will not do the right thing. It will do something else.Specifically, it will do what you told it to do.

1.4 Formal and natural languages

  • Natural languages are the languages people speak, such as English, Spanish, and French.They were not designed by people (although people try to impose some order on them);they evolved naturally.

  • Formal languages are languages that are designed by people for specific applications. For example, the notation(符号) that mathematicians use is a formal language that is particularly good at denoting relationships among numbers and symbols. Chemists use a formal language to represent the chemical structure of molecules.

  • (most importantly:)Programming languages are formal languages that have been designed to express computations.

Formal languages tend to have strict rules about syntax. Syntax rules come in two flavors, pertaining(依附) to tokens(象征标志) and structure.

  1. Tokens are the basic elements of the language, such as words, numbers, and chemical elements.
  2. structure of a statement, that is, the way the tokens are arranged.
  3. parsing(解析,语法分析) When you read a sentence in English or a statement in a formal language, you have to figure out what the structure of the sentence is.

differences between formal and natural languages:

  1. ambiguity(模糊,模棱两可): Natural languages are full of ambiguity; Formal languages are designed to be nearly or completely unambiguous, which means that any statement has exactly one meaning, regardless of context.
  2. redundancy(冗余): In order to make up for ambiguity and reduce misunderstandings, natural languages employ lots of redundancy. As a result, they are often verbose. Formal languages are less redundant and more concise(简洁).
  3. literalness(文学性,修饰性): Natural languages are full of idiom and metaphor. Formal languages mean exactly what they say.

Glossary(专业术语表)

  • problem solving: The process of formulating a problem, finding a solution, and expressing the solution.
  • high-level language: A programming language like Python that is designed to be easy for humans to read and write.
  • low-level language: A programming language that is designed to be easy for a computer
    to execute; also called “machine language” or “assembly language.”
  • portability(可携带): A property of a program that can run on more than one kind of computer.
  • interpret(解释器): To execute a program in a high-level language by translating it one line at a time.
  • compile(编译器): To translate a program written in a high-level language into a low-level language
    all at once, in preparation for later execution.
  • source code: A program in a high-level language before being compiled.
  • object code: The output of the compiler after it translates the program.
    executable: Another name for object code that is ready to be executed.
    prompt: Characters displayed by the interpreter to indicate that it is ready to take input
    from the user.
  • script: A program stored in a file (usually one that will be interpreted).
    interactive mode: A way of using the Python interpreter by typing commands and expressions at the prompt.
  • script mode: A way of using the Python interpreter to read and execute statements in a
    script.
  • program: A set of instructions that specifies a computation.
  • algorithm: A general process for solving a category of problems.
  • bug: An error in a program.
  • debugging: The process of finding and removing any of the three kinds of programming
    errors.
  • syntax: The structure of a program.
  • syntax error: An error in a program that makes it impossible to parse (and therefore impossible to interpret).
  • exception: An error that is detected while the program is running.
  • semantics(语义): The meaning of a program.
  • semantic error: An error in a program that makes it do something other than what the
    programmer intended.
  • natural language: Any one of the languages that people speak that evolved naturally.
  • 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.
  • token(象征标志): One of the basic elements of the syntactic structure of a program, analogous to a
    word in a natural language.
  • parse(语法解析): To examine a program and analyze the syntactic structure.
  • print statement: An instruction that causes the Python interpreter to display a value on
    the screen.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值