程序代写案例-INFO1110
时间:2022-06-09
PE
NG
INFO1110 Final
Yucheng Peng
2022
目录
1 Week 1 4
1.1 What is a program? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2 Create file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 vim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4 Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2 Week 2 6
2.1 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2 Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3 Type Annotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.4 Type Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.5 Data Type - bool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.6 Data Type - str . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.7 Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.8 Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3 Week 3 15
3.1 Debugging using pdb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.2 main() function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.3 Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.4 Why use Functions? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.5 practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1
PE
NG
4 Week 4 19
4.1 Control Flow: if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.2 __name__ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.3 Control Flow: if...else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.4 if...elif . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
5 Week 5 21
5.1 Control Flow: while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5.2 practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
6 Week 6 23
6.1 string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
6.2 List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
6.3 Tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
6.4 practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
7 Week 7 24
7.1 break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
7.2 continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
7.3 Test type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
7.4 Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
7.5 practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
8 Week 8 28
8.1 Types of Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
8.2 Test tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
8.3 End to end test demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
9 Week 9 29
9.1 file input/output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
9.2 File Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
9.3 os module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
9.4 practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2
PE
NG
10 Week 10 33
10.1 结构 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
10.2 Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
10.3 Instance Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
10.4 Instance Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
10.5 class variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
10.6 static methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
10.7 Special methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
10.8 practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
11 Week 12 40
11.1 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
11.2 practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
12 Week 13 44
12.1 for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
12.2 range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
12.3 ndarray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
12.4 practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3
PE
NG
1 Week 1
1.1 What is a program?
• A program is a set of instructions that can be executed by a computer to perform a certain task.
• Python is a high level language –close to human language but follows a special format (syntax).
1.2 Create file
1. pwd: 显示所在目录
2. ls: 显示当前目录所有文件,不同类型有颜色区分
3. dir: 显示当前目录所有文件,无颜色区分
4. mkdir [name]: 创建目录
5. cd:切换目录
6. touch: 创建文件
7. mv [file] [dest]: 移动文件
8. cp [file] [newfile]: 复制文件
9. vim [file]: 用 vim 编辑器编辑文件
1.3 vim
1. i: 写入模式
2. Esc: 只读模式
3. w: 保存文件
4. q: 退出编辑器
4
PE
NG
1.4 Practice
5
PE
NG
2 Week 2
2.1 Data Types
python:
four basic data types:
int, float, bool, str
2.2 Variable Names
• Must start with a letter
• Must not have spaces.
• Case sensitive! X and x refers two different variables.
• Avoid reserved keywords –words that have special purpose.
• Name variables to describe purpose of value.
• All CAPS indicate constants e.g. FLAT_RATE
• Good practice to use lowercase with words separated by underscores (_) to improve readability.
Reserved Keywords
6
PE
NG2.3 Type AnnotationThe type is determine at runtime. However, type annotation can be used to indicate the variable type or type of datathat the variable contains.eg. x: int = 102.4 Type Casting
Explicit conversion 显示转换
• str() -> convert to type str
• int() -> convert into type int
• float() -> convert into type float
Implicit conversion 隐式转换
• Division between a float and int
• Division between two int
• Addition between an int and float
7
PE
NG
2.5 Data Type - bool
Boolean operators in Python: not, and, or
2.6 Data Type - str
• str.format() returns a copy where each replacement field if replace with string value of the corresponding argument.
8
PE
NG
• str.lower() returns a copy of str converted to all lowercase.
• str.capitalize() returns a copy of str with first character capitalize
• str.upper() returns a copy of str converted to all uppercase.
str format
9
PE
NG
10
PE
NG
11
PE
NG
2.7 Operator
12
PE
NG
2.8 Practice
13
PE
NG
14
PE
NG3 Week 33.1 Debugging using pdbhttps://zhuanlan.zhihu.com/p/37294138
3.2 main() function
In Python, the programmer is not changing the point of entry by doing this but just directing attention to where to begin.
15
PE
NG3.3 FunctionFour parts1. The function name (what it’s called)
2. The function arguments (the information/variables we pass it)
3. The return type (what kind of thing is returned by the function
4. The function body (the actual code that does the work)
16
PE
NG
Keyword return
1. If a return statement is omitted, Python by default returns the value None.
2. If your function is intended to return a value then you must include an expression or variable in the return statement
3. return statement is NOT the same as print statement
4. Can only return ONE data type. Multiple values can be pass out of the function by creating a tuple.
3.4 Why use Functions?
1. Separate tasks within a program.
17
PE
NG
(a) Big problems can be broken into smaller pieces.
(b) A function can be written to solve each piece of problem and later assembled into a working whole.
2. Reuse codes.
(a) Don’t re-invent the wheel each time.
(b) Code once and re-use by calling function with new data.
3. Reduce chance of errors.
(a) Process of copy, paste and modify codes introduces human error.
(b) Define general implementation once
(c) If a change is made to the implementation, you only have to fix it once!
3.5 practice
18
PE
NG
4 Week 4
4.1 Control Flow: if
4.2 __name__
• This is a special variable.
• The __name__ attribute will be set to __main__ if the module is considered to be the main program.
• When a Python module or package is imported, __name__ is set to the module’s name. Usually, this is the name
of the Python file itself without the .py extension
19
PE
NG
4.3 Control Flow: if...else
4.4 if...elif
• elif abbreviation of else-if.
• When you have more than two possibilities and need more than two branches.
• No limit on the number of elif statements.
• If there is an else clause, it has to be at the end. Not syntactically needed to have one.
• Condition is checked in order of appearance. If the first is false, the next is checked and so on. Only the first true
branch runs.
20
PE
NG
5 Week 5
5.1 Control Flow: while
Applications of Loops
• Repeat set of actions
• Summation –calculate running total of a set of values.
• Iterating through elements in a collection
• Count occurrences –find and tally occurrences.
21
PE
NG
5.2 practice
22
PE
NG
6 Week 6
6.1 string
• A string of characters only make sense when they are together.
• A string variable contains the memory address that contains all characters.
• We can access each character (element or item) in a string variable by referring to its index
6.2 List
len() function
• you can access the length of an array (total number of elements) using Python’s built-in function len()which returns
the number of elements.
• Important: Python is zero-based indexing, the last element of an array is always: len() –1
• With knowledge of the length of an array, you can easily access (and manipulate) the values in the array using loops.
6.3 Tuple
Tuples are basically lists, but with one important distinction: they’re immutable. This means that they cannot be
modified.
23
PE
NG
6.4 practice
7 Week 7
7.1 break
• Special reserved keyword
• When used in loops, it means ‘break’ from the current iteration of this loop.
• It is only allowed inside the body of a loop.
7.2 continue
• Another reserved keyword.
24
PE
NG
• When used in loops, it means ‘skip the rest of this iteration and go on to the next one’
• It is only allowed inside the body of a loop.
7.3 Test type
type(object) function: returns the class of the object.
isinstance(object, classinfo): check whether an object is an instance of a given class
7.4 Exception
raise statement
Handling Exceptions
• Put codes where you suspect an error may occur into the try block.
25
PE
NG
• The except block will only execute when an exception occurs.
• The general except block must be placed at the bottom.
The finally keyword is occasionally used after a try/except block to define behaviour that will always execute, regardless
of what exceptions are (or are not) raised.
26
PE
NG
7.5 practice
27
PE
NG
8 Week 8
8.1 Types of Tests
• End-to-end Tests
– Full functionality of system.
• Integration Tests
– Examines how different components of a system interact with one another.
• Unit Tests
– Fast and simple tests that target the smallest possible components of a system.
8.2 Test tools
Format
28
PE
NG8.3 End to end test demo9 Week 9
9.1 file input/output
open(file, mode
mode:
• r: 以只读方式打开文件。文件的指针将会放在文件的开头。
• w: 打开一个文件只用于写入。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果该文
件不存在,创建新文件。
• 打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有
内容之后。如果该文件不存在,创建新文件进行写入。
29
PE
NG
read/write
• read(size) size – 从文件中读取的字节数, 未给定则读取所有
• readline() 读取整行,包括
n 字符
• readlines() 读取所有行,并返回 list
• write(string) 用于向文件写入指定字符串
• writelines(list), 写入 list 进文件
9.2 File Output
print print 可以通过设置 file 的防止来将输出写入文件
9.3 os module
The os module provides functions for working with files and directories.
30
PE
NG
• os.getcwd() 返回当前目录
• os.listdir(path) 显示目标文件夹下所有文件
• os.chdir(目录) 切换目录
• os.open(文件,mode) 打开文件(底层操作)
• os.close(), 关闭文件
• os.fchdir() 修改当前目录
31
PE
NG
9.4 practice
32
PE
NG
10 Week 10
面向对象编程——Object Oriented Programming 简称 OOP
是一种程序设计思想。OOP 把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数。
面向过程的程序设计把计算机程序视为一系列的命令集合,即一组函数的顺序执行。为了简化程序设计,面向过程把函数
继续切分为子函数,即把大块函数通过切割成小块函数来降低系统的复杂度。
而面向对象的程序设计把计算机程序视为一组对象的集合,而每个对象都可以接收其他对象发过来的消息,并处理这些消
息,计算机程序的执行就是一系列消息在各个对象之间传递。
在 Python中,所有数据类型都可以视为对象,当然也可以自定义对象。自定义的对象数据类型就是面向对象中的类(Class)
的概念。
33
PE
NG
10.1 结构
34
PE
NG
10.2 Constructor
35
PE
NG
10.3 Instance Variables
改变某个对象的 Instance Variables 并不会影响其他同类对象
10.4 Instance Methods
36
PE
NG
10.5 class variable
改变 class variable 会影响到所有为这个类的对象
37
PE
NG
10.6 static methods
10.7 Special methods
__str__ 在 print 的时候更好看 (用户读取)
38
PE
NG
__repr__ 方便程序开发者读取
39
PE
NG
10.8 practice
11 Week 12
11.1 Recursion
在函数内部,可以调用其他函数。如果一个函数在内部调用自身本身,这个函数就是递归函数。
40
PE
NG
递归函数的优点是定义简单,逻辑清晰。理论上,所有的递归函数都可以写成循环的方式,但循环的逻辑
不如递归清晰。
11.2 practice
41
PE
NG
42
PE
NG
43
PE
NG
12 Week 13
12.1 for loop
44
PE
NG
12.2 range
45
PE
NG
12.3 ndarray
12.4 practice
二元数组的乘法
46


essay、essay代写