码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-11-05 15:34:38      阅读:287      评论:0      收藏:0      [点我收藏+]

标签: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 / 3
2 0.6666666666666666
3 5 / 3
4 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 + 1
2 1.6666666666666665
3 >>> 5 / 3
4 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 = 2
2 >>> d *= 3 + 4
3 >>> d
4 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 = x
2  File "<stdin>", line 1 
3 SyntaxError: cant 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 + moogah
2  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>

标签:chap   3.2   mod   chapter   赋值语句   term   alt   turn   can   

原文地址:http://www.cnblogs.com/zhenggege/p/7783399.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!