Think Python:Chapter 2:Variables(变量), expressions(表达式) and statements(陈述) 的笔记

来源:互联网 发布:linux查看cpu运行状态 编辑:程序博客网 时间:2024/06/05 04:21

目录

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

这是MIT官方编程教程中Python TutorialTypes, Values, Expressions; Variables and Binding的内容。本篇博客为《 Think Python: How to Think Like a Computer Scientist》的第2章 Variables(变量), expressions(表达式) and statements(陈述) 的笔记内容。(Think Python:Chapter 2:Variables(变量), expressions(表达式) and statements(陈述) 的笔记)

Python Tutorial:Types, Values, Expressions; Variables and Binding

READING LIST:

  • Think Python, Chapter 2: Variables, Expressions, and Statements
  • 6.01 Python Notes, Section 1: Getting Used to Python (PDF)

Chapter 2:Variables(变量), expressions(表达式) and statements(陈述)

2.1 Values and types

value: One of the basic units of data, like a number or string, that a program manipulates.

type: A category(种类/分类) of values.

  • integer(int): A type that represents whole numbers.
  • floating-point(float): A type that represents numbers with fractional parts.
  • string(str): A type that represents sequences of characters.

If you are not sure what type a value has, the interpreter can tell you.

    >>> type(' Hello, World! ' )    <type ' str' >    >>> type(17)    <type ' int' >

2.2 Variables

variable(变量): A name that refers to a value.

statement(声明): A section of code that represents a command or action. So far, the statements we have seen are assignments and print statements.

assignment(赋值): A statement that assigns a value to a variable.

An assignment statement creates new variables and gives them values:

    >>> message = ' And now for something completely different'    >>> n = 17    >>> pi = 3.1415926535897932

state diagram: A graphical representation of a set of variables and the values they refer to.

2.3 Variable names and keywords

Variable names:

  • They can contain both letters and numbers, but
    they have to begin with a letter.

  • It is legal to use uppercase letters, but it is a good idea to
    begin variable names with a lowercase letter (you’ll see why later).

  • The underscore character(下划线), _ ,can appear in a name. It is often used in names with **multiple
    words**.

keyword: A reserved word that is used by the compiler to parse a program; you cannot use keywords like if, def, and while as variable names.

Python 2 has 31 keywords:

***and del from not while
as elif global or with
assert else if pass yield
break except import print
class exec in raise
continue finally is return
def for lambda try***

In Python 3, exec is no longer a keyword, but nonlocal is.

2.4 Operators and operands

operator: A special symbol that represents a simple computation like addition, multiplication, or string concatenation.

  • The operators +, -, , / and perform addition, subtraction, multiplication, division and
    exponentiation(指数运算)

operand: One of the values on which an operator operates.

2.5 Expressions and statements

expression: A combination of variables, operators, and values that represents a single result value.

statement: a unit of code that the Python interpreter can execute. We have seen two kinds of statement: print and assignment.

2.6 Interactive mode and script mode

2.7 Order of operations

evaluate: To simplify an expression by performing the operations in order to yield a single value.

rules of precedence(优先级): The set of rules governing the order in which expressions involving multiple operators and operands are evaluated.The acronym PEMDAS is a useful way to remember the rules:

  • **P**arentheses(括号) have the highest precedence and can be used to force an expression to
    evaluate in the order you want.
  • **E**xponentiation(指数) has the next highest precedence.
  • **M**ultiplication and **D**ivision have the same precedence, which is higher than
    **A**ddition and **S**ubtraction, which also have the same precedence.
  • Operators with the same precedence are evaluated from left to right (except exponentiation).

2.8 String operations(字符运算)

concatenate: To join two operands end-to-end.

字符加法:

    >>> first='throat'    >>> second='warbler'    >>> print(first+second)    throatwarbler

字符乘法:

    >>> print(first*3)    throatthroatthroat

2.9 Comments(注解)

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.

注解1

    >>> # compute the percentage of the hour that has elapsed    percentage = (minute * 100) / 60    >>> print(percentage)    98.33333333333333

注解2

    >>>> percentage = (minute * 100) / 60 # percentage of an hour    >>> print(percentage)    98.33333333333333

2.10 Debugging

0 0
原创粉丝点击