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

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: 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 + moogah
2  Traceback (most recent call last): File "<stdin>", line 1, in <module>
3 NameError: name ‘moogah‘ is not defined
时间: 2024-08-06 09:01:45

Note 2 for <Pratical Programming : An Introduction to Computer Science Using Python 3>的相关文章

MIT Introduction to Computer Science and Programming (Lesson one )

MIT Introduction to Computer Science and Programming (Lesson one ) 这篇文是记载 MIT 计算机科学及编程导论 第一集 的笔记 Lesson one : Goals of the course;what is computation;introduction to data types,operators,and variables 一 讲解课程的任务.课程目标 目标 像一个计算机科学家一样思考 都能够读写程序 tacking t

MITx: 6.00.1x Introduction to Computer Science and Programming Using Python Week 2: Simple Programs 4. Functions

ESTIMATED TIME TO COMPLETE: 18 minutes We can use the idea of bisection search to determine if a character is in a string, so long as the string is sorted in alphabetical order. First, test the middle character of a string against the character you'r

edX MITx: 6.00.1x Introduction to Computer Science and Programming Using Python 课程 Week 1: Python Basics Problem Set 1 Problem 3

Assume s is a string of lower case characters. Write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example, if s = 'azcbobobegghakl', then your program should print Longest substring in alphabe

chapter 3 introduction to computer science

主机文件: <chapter3.docx>

MTH5001: Introduction to Computer Programming

projectMarch 14, 20191 MTH5001: Introduction to Computer Programming 2018/191.1 Final Report Project: "Networks"1.1.1 Instructions:First, please type your name and student number into the Markdown cell below:Name:Student number:You must write yo

公开课Introduction to Data Science的RDBMS总结

最近在学习公开课  Introduction to Data Science 第二部分讲到关系数据库与关系代数(Relational Databases,Relational Algebra) 其中讲到了 关系数据库 SQL与RA 的关系 选择selection, 负号σc(R)  表示查询表R中符合条件c的列, 例如 σSalary > 40000 (Employee) 表示  "select * from Employee where Salary > 40000" 投

Master the 10 Most Common Python Programming Problems - 10大最常见的Python编程错误

http://blog.csdn.net/pipisorry/article/details/45175457 Introduction 本文介绍python编程中很难捕捉10大错误 (Note: This article is intended for a more advanced audience than Common Mistakes of Python Programmers, which is geared(适合) more toward those who are newer t

[新书推荐]A Practical Introduction to Computer Vision with OpenCV

一本opencv好书,  在我上传的资源里 http://download.csdn.net/detail/qq_21970857/8504829 Computer Vision is a rapidly expanding area and it is becomingprogressively easier for developers to make use of this field dueto the ready availability of high quality librari

Python资料汇总

Python是一种面向对象的解释性的计算机程序设计语言,也是一种功能强大且完善的通用型语言,已经有十多年的发展历史,成熟且稳定.Python 具有脚本语言中最丰富和强大的类库,足以支持绝大多数日常应用. Python语言有非常简捷.清晰的语法特点,适合完成各种高层任务,可以在所有操作系统中运行.目前,基于这种语言的相关技术正在飞速的发展,用户数量急剧扩大,相关的资源非常多. Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣