吐槽:由于作业实在太恶心,觉得其实也没有什么值得写的通常,所以我把以前的个别删了。既然是学知识,那不如做个笔记总结。。之所以写一篇是因为确实不多,主要我觉得值得看的也就代码风格吧。
正文:
1,Week1:Python基础与代码风格
1)Basic elements of Python
Below is a list of the data types in Python with which you should be familiar.
- Primitive data types - integers (int), floating point numbers (float), strings (str), Booleans (bool),
- Built-in composite data types - lists (list), tuples (tuple), dictionaries (dict), sets (set),
- User-defined data types - objects created via a class definition.
You should also be familiar with creating various types expressions (arithmetic, Boolean, and string) using both built-in constants and variables and capable of effectively using statements of the following types:
- Simple statements - assignment statements (=), print statements (print), return statements (return), import statements (import), global statements (global), pass statements (pass),
- Compound statement - conditional statements (if, elif, else), function definitions (def), for loops (for), while loops (while), class definitions (class).
2)Guidelines for Coding Style
Documentation
Documentation strings ("docstrings") are an integral part of the Python language. They need to be in the following places:
- At the top of each file describing the purpose of the module.
- Below each class definition describing the purpose of the class.
- Below each function definition describing the purpose of the function.
Docstrings describe what is being done in a module, class, method, or function, not how it is being done. Except in rare cases where the how is part of the contract (i.e., binary search, so you know it runs in time log(n)). A docstring for a function should explain the arguments, what the function does, and what the function returns. This sample file demonstrates the use of docstrings. Note that the __init__ methods of classes generally do not have docstrings because their purpose is obvious: to initialize the object. You may want to have one, though, to describe the arguments.
These docstrings are treated specially in Python, as they allow the system to automatically give you documentation for modules, classes, functions, and methods. At the command prompt, you can type help(...), and it will return the docstring for whatever the argument you passed to help is. (Note that CodeSkulptor does not provide a command prompt, so you cannot use help in CodeSkulptor.)
文档字符串描述正在做什么,它的三个用处:module的目的写在文件最上方,class的目的写在定义下面,function的目的写在定义下面。
Comments
Comments should describe how a section of code is accomplishing something. You should not comment obvious code. Instead, you should document complex code and/or design decisions. Comments and docstrings are not interchangeable. Comments start with the "#" character. While you will see some Python programmers do this, you should not comment your code by putting a multi-line string in the middle of your program. That is not actually a comment, rather it is just a string in the middle of your program!
不应该用三引号那种写注释,而应该每一行开头都是# ,要与文档字符串区分。
A good example:
# This is a block comment
# that spans multiple lines
# explaining the following code.
val = some complicated expression
A bad example:
"""
Somebody told me that a multiline
string is a block comment.
It's not.
"""
val = some complicated expression
Note that docstrings are multi-line strings, but they do not violate this convention because docstrings and comments are different and serve different purposes in a program.
Global variables
Global variables should never be used in this class. Avoiding their use is good programming practice in any language. While programmers will sometimes break this rule, you should not break this rule in this class.
There is one exception to this rule: you may have global constants. Because the Python language does not actually support constants, by convention, Python programmers use global variables with names that are in all capital letters for constants. When you see a variable with a name in all capital letters, you should always assume that it is a constant and you should never change it. Again, such global constants are the only global variables that will be allowed in this class.
全局变量名应该全大写,且不该更改。
Indentation
Each indentation level should be indented by 4 spaces. As Python requires indentation to be consistent, it is important not to mix tabs and spaces. You should never use tabs for indentation. Instead, all indentation levels should be 4 spaces. Note that CodeSkulptor automatically converts all tab indents into 4 spaces.
建议缩进都用四个空格而且不要和tab混用。
Names
All variable, function, class, and method names must be at least 3 characters. The first character of a name should follow these conventions:
- Variable names should always start with a lower case letter. (Except for variables that represent constants, which should use all upper case letters.)
- Function and method names should always start with a lower case letter.
- Class names should always start with an upper case letter.
Further, we will follow the common Python convention that variable, function, and method names should not have any capital letters in them. You can separate words in a name with an underscore character, as follows: some_variable_name. Similarly, class names should not contain underscores, and instead use capitalization to separate words, as follows: SomeClassName.
As previously noted, constants should be in all capital letters, such as: THIS_IS_A_CONSTANT. Note that this means that your class names must have at least one lower case letter in them, to distinguish them from constants.
By convention in Python, you can "violate" the above rules and start a name with an underscore, _, to indicate that the name is private and should not be accessed outside of the context in which it is defined. In this case, the rest of the name after the underscore should follow the rules given above. This will arise mainly when you define instance fields in classes, as these style guidelines insist that such fields be private (discussed next).
名字都应该最少三个字符,变量名应该以小写字母开头(常量应全大写),函数和方法名应以小写字母开头,类名应以大写字母开头,而且变量函数方法都不该存在大写字母,分割单词可用_,但类名则应用大写字母分割。若以_开头,则表示变量是私有的,不该在其范围外使用。
Class and Instance Fields
Class and instance fields should never be accessed directly from outside the class. You should therefore always start your field names with an underscore. Even if you don't, you still should not access them from outside of the class.
You will often see public fields in Python programs (and in programs of other languages). This is not a good habit to get into. Instead, all fields should always be private. If there is good reason to make the data in the field accessible outside the class, you should create a method to do so. By convention in other languages, these methods are usually named get_field, where field is the name of the field. You should follow this convention.
Note that this is not common in Python. Rather, public fields or properties are used. However, we are trying to teach principles that transcend a particular programming language. All languages support writing so-called "getter" methods, whereas many do not support properties. There is nothing wrong with Python properties, it is just a different syntax for using methods. However, the use of public fields is not good practice in any programming language, whether the language allows it or not.
You should avoid the use of class fields, which are declared in the scope of the class itself and are common to all instances of the class. Instead, you should use instance fields (defined as attributes of self). These will be unique to each instance of the class.
类和实例字段不应直接从类外部访问,所以变量名都应该以_开头,外界访问也应该用get_xxx方法而不是直接公共字段。
Scope
You should not use names that knowingly duplicate other names in an outer scope. This would make the name in the outer scope impossible to access. In particular, you should never use names that are the same as existing Python built-in functions. For example, if you were to name one of your local variables max inside of a function, you would then not be able to call max() from within that function.
不应使用在外部作用域中故意重复其他名称的名称。这将使外部作用域中的名称无法访问。尤其是,您不应该使用与现有Python内置函数相同的名称。
Arguments and local variables
While there is not necessarily a maximum number of arguments a function can take or a maximum number of local variables you can have, too many arguments or variables lead to unnecessarily complex and unreadable programs. Pylint will enforce maximum numbers of arguments, variables, methods, etc. If you run into limits that Pylint complains about, you should restructure your program to break it into smaller pieces. This will result in more readable and maintainable code.
Further, you should not have function arguments or local variables declared that are never used, except in rare circumstances. Sometimes, you do need to have a variable that you never use. A common case is in a loop that just needs to execute a certain number of times:
for num in range(42):
# do something 42 times
In this case, you should name the variable with the dummy_ prefix. This indicates clearly to you, others, and Pylint that the variable is intentionally unused.
for dummy_num in range(42):
# do something 42 times
就是希望循环变量都用dummy_开头,这样就可以防止意思混淆。
2,Week2:测试及其重要性,网格表示的方法
1)Testing
2)Representing Grids
Mathematically, a grid is a partition of a 2D region into a disjoint collection of cells. Typically, these cells are all a single simple shape such as a square, triangle or hexagon. Several mini-projects, including 2048, Zombie Apocalypse, and the Fifteen puzzle, involve rectangular grids of squares. Grids are useful in many computational applications because they provide a convenient way to partition a geometric region in a way that can be easily modeled as a 2D data structure.
3,Week3:概率相关
1)Basic Probability
Probability is a branch of mathematics associated with the analysis of random phenomena. In Computer Science, probability can arise in several ways: as part of the mathematics involved in analyzing a random process or as part of a computational approach to solving a particular problem. For this class, our main application of probability will be in analyzing and building programs associated with simple games. However, don't view this application as limiting. Probabilistic methods arise in applications like scientific computation, cryptography, and robotics.
Since many of you may have had limited exposure to probability, we review some basic terminology associated with probability theory to begin. A trial (or an experiment) is any procedure that can be infinitely repeated and has a well-defined set of possible outcomes, known as the sample space. If the sample space is finite, each outcome can be assigned a number between zero and one that corresponds to the likelihood of that particular outcome occurring. This number is the probability associated with the outcome. Since every trial always results in exactly one outcome from the sample space, the sum of the probabilities associated with the outcomes is always exactly one.
A simple example of a trial is a single roll of a fair six-sided die. The outcomes of this trial are the values on six sides of the die {1,2,3,4,5,6}. Since the die is fair, the probabilities associated with these six outcomes are all equal so the probability of each outcome is exactly 1/6. More generally, the probabilities associated with outcomes of a trial are said to be uniformly distributed if these probabilities all have equal value.
An event is a set of outcomes of a trial (a subset of the sample space). For a single six-sided die, a simple example of an event would be that the resulting roll is even. The probability of an event is the sum of the probabilities associated with its set of outcomes. For example, rolling an even number with a six-sided die corresponds to an event with three outcomes {2,4,6}, each with probability 1/6. Therefore, the probability of that event is 1/6 + 1/6 + 1/6 = 1/2.
2)Expected Value
More generally, if the possible outcomes of a trial have value x1, x2, ... xk and their probabilities are p1, p2, ... pk, respectively, then the expected value of this trial is ∑i=pi×xi.
4,Week4:排列组合相关
1)Enumeration(枚举)
2)Permutations and Combinations(排列组合)
懒得复制老师的内容了,但都是高中所学。
5,Week5:计数相关
1)Arithmetic Sums(代数和)
就是级数那些,求和什么的。
2)Logarithms and Exponentials(对数与指数)
3)Growth Rates of Functions(函数的增长速度)
微积分所学,f(x)/g(x)之类的,看一样快还是某个快。
本文档详细介绍了Python的基础知识,包括数据类型、代码风格规范。强调了文档字符串的重要性和正确使用方法,以及如何编写有效的注释。此外,还讨论了避免使用全局变量、遵循一致的缩进和命名约定等最佳实践。

157

被折叠的 条评论
为什么被折叠?



