Note 2 for <Pratical Programming : An Introduction to Computer Science Using Python 3>

來源:互聯網
上載者:User

標籤:chap   3.2   mod   chapter   指派陳述式   term   alt   turn   can   

Book Imformation :

<Pratical Programming : An Introduction to Computer Science Using Python 3> 2nd Edtion

Author : Paul Gries,Jennifer Campbell,Jason Montojo

Page : Chapter 2.3 to Chapter 2.5

 


1.A type consists of two things:

(1).a set of values

(2).a set of operations(操作, 運算) that can be applied to those values

if an operator(運算子) can be applied to more than one type of value,it‘s called an overloaded operator(重載運算子).


2.As for floating-point numbers,they are not exactly the fractions(分數) you learned before,take 2/3 and 5/3 for example:

1 2 / 32 0.66666666666666663 5 / 34 1.6666666666666667

This is fishy(可疑的, 值得懷疑的): both of them should have an infinite number of 6s after the decimal point.The problem is that computers have finite amount of memory,and the information that can be stored is limited.The number 0.6666666666666666 turns out to be the closest value to 2/3,so as to 5/3.

(Question:Why the number 0.6666666666666666 turns out to be the closest value to 2/3?)

 

3.Operator precedence(運算子優先順序)

Table 2 shows the order of precedence for arithmetic operators.

It is a good rule to parenthesize complicated expressions even when you don‘t need to like 1 + 1.7 + (3.2 * 4.4) - (16 / 3),but not use parentheses(括弧) in simple expressions such as 3.1 * 5.

If we use 0.6666666666666666 in a calculation, the error may get compounded:

1 >>> 2 / 3 + 12 1.66666666666666653 >>> 5 / 34 1.6666666666666667

in many programming languages,1 + 2 / 3 is not equal to 5 / 3.

 

4.A name that refers to a value is called variable(變數).

You create a variable by assigning(賦值) it a value:

1 >>> degree_celsius = 26.0

This statement is called assignment statement(指派陳述式).

 

5.Values, Variables, and Computer Memory

In our memory model,a variable contains the memory address of the object(對象) to which it refers:

We use the following terminology:

 

6.Augmented Assignment(複合賦值)

Note that the operator is applied after the expression on the right is evaluated:

1 >>> d = 22 >>> d *= 3 + 43 >>> d4 14

 

7.How Python Tells You Something Went Wrong

Broadly speaking,there are two kinds of errors in Python:

(1).syntax errors(語法錯誤)

which happen when you type something that isn‘t valid Python code.

e.g.

1 >>> 2 + 2  File "<stdin>", line 1 3    2 + 4      ^ 5 SyntaxError: invalid syntax
1 >>> 12 = x2  File "<stdin>", line 1 3 SyntaxError: can‘t assign to literal

A literal is any value,like 12 and 26.0

(2).semantic errors(語義錯誤)

which happen when you tells Python to do something that it just can‘t do.

e.g.

1 >>> 3 + moogah2  Traceback (most recent call last): File "<stdin>", line 1, in <module> 3 NameError: name ‘moogah‘ is not defined

 

Note 2 for <Pratical Programming : An Introduction to Computer Science Using Python 3>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.