以前也算是写过一些代码,但除了大一的C语言课是有指导的,之后都是自己凭感觉写,没有系统学习过。因为准备申请Dartmouth的CS master(希望渺茫),所以踏踏实实从头学起。COCS1是Introduction to programming and computation,达特茅斯计算机专业的第一门基础课,没有网课视频,但是有Devin老师写的一本电子教案,叫projectpython,网址如下
最近就准备参照这个教案进行第一波快速学习和梳理,很多都是自己早就知道的,主要记录一些新学到的知识点和经验:
代码规范:
1、多加注释,在每一个函数前描述函数功能,可能会不知道功能的语句注释功能
2、变量要可以表示其实际意义,需要写几个单词组成的变量名时,在python里规范是用_连接,最后不用大写划分几个单词
3、逗号后加空格,3~10行左右加空行
要做完除法取整的话,python2里是 ' / ',但python3里是 ' // ' ,要从2到3统一转换的话,加上语句:
from __future__ import division
浮点数的精度有限,最多一个浮点数由64位表示,所以并不会完全精确,用到的时候注意是不是该用==判断
比如需要判断 a == 0 时,可以考虑改成 a < epsilon(一个极小量)
Debug 第一步:考虑清楚你想要什么结果?实际上得到了什么?有什么区别?越准确越好
Ask yourself what you expected. What actually happened? What’s the difference between what you expected and what you got? Be as precise as possible. Not, “the circle is in the wrong place,” but “I expected a circle to be drawn at the location 200, 100, but it seems to have been drawn at 100, 200.” By asking the question precisely, you can sometimes solve the problem immediately. “Aha! Perhaps I swapped the x- and y-coordinates somewhere.”
第二步:定位最早出错的位置
Find the earliest point in the code where something unexpected happens. Bugs often have a cascading effect: once one bug happens, strange behavior can follow. So you want to find the first place where something went haywire. Based on what you expected to happen and what actually happened, come up with a few possible places where the earliest problem could have occurred.