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

 

转载于:https://www.cnblogs.com/zhenggege/p/7783399.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Contents 1 Introduction 11 1.1 ProgramsandProgramming................ 13 1.2 AFewDefinitions...................... 14 1.3 WhattoInstall........................ 14 1.4 ForInstructors........................ 15 1.5 Summary........................... 16 2 Hello,Python 17 2.1 TheBigPicture ....................... 17 2.2 Expressions......................... 19 2.3 WhatIsaType?....................... 22 2.4 VariablesandtheAssignmentStatement........ 25 2.5 WhenThingsGoWrong .................. 29 2.6 FunctionBasics....................... 30 2.7 Built-inFunctions...................... 33 2.8 StyleNotes.......................... 34 2.9 Summary........................... 35 2.10 Exercises........................... 36 3 Strings 39 3.1 Strings............................ 39 3.2 EscapeCharacters ..................... 42 3.3 MultilineStrings....................... 43 3.4 Print.............................. 44 3.5 FormattedPrinting..................... 45 3.6 UserInput.......................... 46 3.7 Summary........................... 47 3.8 Exercises........................... 48 Prepared exclusively for Trieu Nguyen CONTENTS 8 4 Modules 50 4.1 ImportingModules ..................... 50 4.2 DefiningYourOwnModules................ 54 4.3 ObjectsandMethods.................... 60 4.4 PixelsandColors...................... 68 4.5 Testing............................ 70 4.6 StyleNotes.......................... 76 4.7 Summary........................... 77 4.8 Exercises........................... 78 5 Lists 81 5.1 ListsandIndices ...................... 81 5.2 ModifyingLists........................ 85 5.3 Built-inFunctionsonLists ................ 87 5.4 ProcessingListItems.................... 89 5.5 Slicing............................. 92 5.6 Aliasing............................ 94 5.7 ListMethods......................... 95 5.8 NestedLists......................... 97 5.9 OtherKindsofSequences................. 99 5.10 FilesasLists......................... 100 5.11 Comments.......................... 103 5.12 Summary........................... 105 5.13 Exercises........................... 105 6 MakingChoices 108 6.1 BooleanLogic ........................ 108 6.2 ifStatements......................... 118 6.3 StoringConditionals .................... 125 6.4 Summary........................... 126 6.5 Exercises........................... 127 7 Repetition 131 7.1 CountedLoops........................ 131 7.2 whileLoops.......................... 140 7.3 UserInputLoops...................... 148 7.4 ControllingLoops...................... 149 7.5 StyleNotes.......................... 153 7.6 Summary........................... 154 7.7 Exercises........................... 155 Reporterratum thiscopyis(P1.0printing,April2009) Prepared exclusively for Trieu Nguyen CONTENTS 9 8 FileProcessing 159 8.1 OneRecordperLine .................... 160 8.2 RecordswithMultipleFields................ 171 8.3 PositionalData ....................... 174 8.4 MultilineRecords...................... 177 8.5 LookingAhead........................ 179 8.6 WritingtoFiles........................ 181 8.7 Summary........................... 183 8.8 Exercises........................... 183 9 SetsandDictionaries 185 9.1 Sets.............................. 185 9.2 Dictionaries ......................... 190 9.3 InvertingaDictionary.................... 197 9.4 Summary........................... 198 9.5 Exercises........................... 199 10Algorithms 203 10.1 Searching........................... 204 10.2 Timing ............................ 211 10.3 Summary........................... 211 10.4 Exercises........................... 212 11SearchingandSorting 214 11.1 LinearSearch........................ 214 11.2 BinarySearch........................ 218 11.3 Sorting............................ 222 11.4 MoreEfficientSortingAlgorithms............. 228 11.5 Mergesort:AnNlog2NAlgorithm ............. 229 11.6 Summary........................... 233 11.7 Exercises........................... 234 12Construction 237 12.1 MoreonFunctions..................... 237 12.2 Exceptions.......................... 242 12.3 Testing............................ 249 12.4 Debugging.......................... 254 12.5 Patterns ........................... 256 12.6 Summary........................... 260 12.7 Exercises........................... 261 Reporterratum thiscopyis(P1.0printing,April2009) Prepared exclusively for Trieu Nguyen CONTENTS 10 13Object-OrientedProgramming 270 13.1 ClassColor.......................... 271 13.2 SpecialMethods....................... 276 13.3 MoreAboutdirandhelp.................. 278 13.4 ALittleBitofOOTheory.................. 280 13.5 ALongerExample...................... 288 13.6 Summary........................... 293 13.7 Exercises........................... 293 14GraphicalUserInterfaces 294 14.1 TheTkinterModule..................... 295 14.2 BasicGUIConstruction .................. 296 14.3 Models,Views,andControllers.............. 301 14.4 Style.............................. 307 14.5 AFewMoreWidgets..................... 312 14.6 Object-OrientedGUIs.................... 316 14.7 Summary........................... 317 14.8 Exercises........................... 318 15Databases 321 15.1 TheBigPicture ....................... 321 15.2 FirstSteps.......................... 323 15.3 RetrievingData ....................... 327 15.4 UpdatingandDeleting................... 330 15.5 Transactions......................... 331 15.6 UsingNULLforMissingData............... 333 15.7 UsingJoinstoCombineTables.............. 334 15.8 KeysandConstraints.................... 339 15.9 AdvancedFeatures..................... 341 15.10Summary........................... 346 15.11Exercises........................... 347 A Bibliography 351 Index 353 Reporterratum
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值