导图社区 python基础课笔记
Mindmap Menu:Chapter 1、Chapter 2、Chapter 3、Chapter 4、Chapter 5、Q&A
编辑于2022-03-26 00:08:06Mindmap Menu
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 7
Q&A
python基础课 笔记
Part 1
1.2 Hardware and Software
Hardware
The Central Processing Unit (CPU)
Use for Run Programs
Early (Electromechanical CPU): the ENIAC (vacuum tubes and switches)
Now: Microprocessors
Main Memory
Random-Access Memory (RAM) Memory chips
A volatile type of memory, only for temporary storage
When the computer turn off, RAM will be erase
Secondary Storage Devices
Hold data for long periods of time
Disk drive
Traditional disk drives
spin circular disk
Solid-state drives
store in solid-state memory
USB drives
Flash menory
Memory sticks
Flash drives
CD (compact disc)
DVD (digital versatile disc)
Input Devices
Input is any data the computer collects from people and from other devices
keyboard, mouse, touchscreen...
Output Devices
Output is any data the computer produces for people or for other devices
video displays, printers...
Software
System Software
Operating System
Controls the internal operation of the computer
Utility Programs
enhances the computer's operation and safeguards data
virus scanners, file compression programs, backup programs
Software Development Tools
programmers use to create, modify, and test software
Application Software
E.g. Microsoft Word, PowerPoint
Ch1 Review
A(n) compiler program translates a high-level language program into a separate machine language program.
1.5 Using Python
Python is an interpreted language Python是一种解释型语言
interactive mode and script mode 交互模式和脚本模式
交互:输入并实习
脚本式:运行Python文件
1.4 How a program work
well-known:Intel, AMD, and Motorola Each brand of microprocessor has its own unique instruction set, which is typically understood only by microprocessors of the same brand.
程序运作流程
CPU在程序中执行指令时,参与fetch-decode-execute cycle的过程 (取-解码-执行)
An assembler translates an assembly language program to a machine language program 汇编语言转机器语言
编译并执行高级程序
使用解释器执行高级程序
syntax rules 语法错误
Part 2
2.1 Designing a Program
一个程序的制作流程
制作程序的两个步骤
Understand the task that the program is to perform. 了解程序要执行的任务。
咨询客户
Determine the steps that must be taken to perform the task. 确定执行任务必须采取的步骤。
Begin by breaking down the task into a series of steps
Pseudocode 伪代码 (pronounced “sue doe code”)
Don’t have to worry about syntax errors
Flowcharts
Ovals 椭圆
Terminal symbols
Begin and end point
Parallelograms 平行四边形
input symbols and output symbols
represent steps in which the program reads input or displays output
Rectangles 矩形
processing symbols 处理符号
represent steps in which the program performs some process on data, such as a mathematical calculation
2.4 Comments
#
2.5 Variables
Assignment Statements 赋值
variable = expression equal sign (=) is known as the assignment operator
Variable Naming Rules 变量命名规则
No: Variable Naming Rules
cannot contain spaces
The first character must be one of the letters a through z, A through Z, or an underscore character (_)
After the first character you may use the letters a through z or A through Z, the digits 0 through 9, or underscores
Uppercase and lowercase characters are distinct. This means the variable name ItemsOrdered is not the same as itemsordered.
常见命名方法
下划线字符来表示空格
gross_pay pay_rate hot_dogs_sold_today
camelCase命名
变量名称以小写字母开头
第二个单词和后续单词的第一个字符以大写形式书写
grossPay payRate hotDogsSoldToday
Numeric Data Types and Literals
int
以整数形式书写且不带小数点的数字文字,将被视为int。例子是7,124和- 9
float
用小数点写的数字文字被认为是float。例子是1.5,3.14159和5.0
str
内存中存储字符串
用type来确定数据类型
2.2 Input, Processing, and Output
The input, processing, and output of the pay calculating program
2.3 Displaying Output with the print Function
Strings and String Literals
single-quote marks (') or a set of double-quote marks (")
triple quotes (either """ or ''')
Can contain both single quotes and double quotes as part of the string
Also be used to surround multiline strings, something for which single and double quotes cannot be used
2.6 Reading Input from the Keyboard
variable = input(prompt)
字符串中引号内的最后一个字符是一个空格
数据转换
int(item)
float(item)
2.7 Performing Calculations
算数运算符
+ 加成 两个数相加
− 减法 从另一个减去一个数字
* 乘法 一个数字乘以另一个
/ 除法 将一个数除以另一个,并将结果作为浮点数给出
注意是浮点数
// 整数除法 将一个数除以另一个,得到结果为整数
当结果为正数时,它将被截断,这意味着其小数部分将被丢弃。
如果结果为负,则将其从零舍入到最接近的整数。
% 余 将一个数除以另一个,得到余数,求余不求商
** 指数 幂数
运算优先级
求幂: **
乘法,除法和余数: * / // %
乘法(*),浮点除法(/),整数除法(//)和余数(%)运算符具有相同的优先级
加减: + −
加(+)和减(−)运算符也具有相同的优先级
当两个具有相同优先级的运算符共享一个操作数时,这些运算符从左到右执行。
从左到右规则有一个例外。当两个**运算符共享一个操作数时,这些运算符从右到左执行。例如,表达式2**3**4的计算结果为2**(3**4)
用括号分组强制优先
将数学公式转换为编程语句
operation
2 int
int
2 float
float
1 int and 1 float
float
将长语句分成多行
换行符(反斜杠(\))将语句分成多行
2.8 More About Data Output
禁止print函数的结尾换行符, end=' '传递空格
2.9 Named Constants 命名常量
所有大写字母书写
避免magic number
2.10 Turtle graphics 海龟图形
import turtle
turtle.showturtle() 显示图标
turtle.forward(200)
Turning the Turtle
turtle.setheading(angle)
turtle.heading() 查询现在角度
turtle.penup() turtle.pendown() 线显示与否
turtle.circle(radius)
turtle.dot()
turtle.pensize(width) 线条粗细
turtle.pencolor(color)
'red', 'green', 'blue', 'yellow', and 'cyan'
turtle.bgcolor(color) 更改背景图片颜色
turtle.reset()
删除当前显示在图形窗口中的所有图形,将图形颜色重置为黑色,并将乌龟重置为屏幕中心的原始位置。此命令不会重置图形窗口的背景色
turtle.clear()
仅删除当前显示在图形窗口中的所有图形。它不会更改乌龟的位置,绘图颜色或图形窗口的背景颜色
turtle.clearscreen()
删除图形窗口中当前出现的所有图形,将图形颜色重置为黑色,将图形窗口的背景颜色重置为白色,并将乌龟重置为其在图形窗口中心的原始位置
turtle.setup(width, height) 指定图形窗口的大小
turtle.goto(x, y) 将海龟移到特定位置
turtle.pos() 获取海龟的当前位置
turtle.speed(speed) 控制海龟的动画速度
turtle.hideturtle() 隐藏turtle
turtle.showturtle() 显示turtle
turtle.write(text)
填充形状
turtle.begin_fill()
turtle.end_fill()
turtle.done() 保持图形窗口打开
Part 3
if statment
3.3 Comparing Strings 比较字符串
3.5 Logical Operators
Relational operators
Boolean expressions using relational operators
3.4 Multiple Nested Decision Structures
Part 4
4.1 Repetition Structures 重复结构
condition-controlled loop 条件控制循环 (while)
true/false condition to control the number of times that it repeats
count-controlled loop 计数控制循环 (for)
repeats a specific number of times
4.2 The while Loop
Condition-Controlled Loop 条件控制循环
A condition-controlled loop causes a statement or set of statements to repeat as long as a condition is true.
Each execution of the body of a loop is known as an iteration (迭代). In the sample run, the loop iterated three times.
Pretest Loop 前测试循环
提前在4行运行 keep_going = 'y', 第一次赋值True
后面才可以开始迭代 while keep_going == 'y':
Infinite Loops
需要按Ctrl+C停止
4.3 The for Loop: A Count-Controlled Loop
iterates a specific number of times 迭代特定次数
target variable is assign to the beginning of each loop iteration.
range的使用*
range(1, 10, 2)
"1",: starting value
"10": ending limit
"2": step value
printout: [1, 3, 5, 7, 9]
print('Number\tSquare') \t 用于数字和平方之间的转义序列
**是指数运算符
\t 转义序列会使数字在输出的两列中对齐
控制循环迭代
从最高到最低的可迭代序列
计算运行总计的逻辑
Augmented assignment operators 扩充赋值运算符
Floating Topic
Part 5
5.1 介绍Function的功能
2/14/2021, 8:19:07 PM
5.1 Introduction to Functions.html
5.2 介绍Function的示例
5.2 Defining and Calling a Void Function with answers.html
5.3 编程设计
flowcharts 流程图
Top-Down Design
Hierarchy Charts (structure chart) 结构图
5.4 Local Variables 局部变量
Part 6
访问顺序
Part 7
list
mutable 易变的
可以有不同的类型,如字符串,整数和浮点数
built-in function list(), 可以使用这个把range()转化成列表
list * n 可重复运行n次
在python无法建立arrays, 但是lists功能和它相当
添加lists的索引 index
my_list = [10, 20, 30, 40]
print(my_list[0], my_list[1], my_list[2], my_list[3])
循环打印
my_list = [10, 20, 30, 40] print(my_list[−1], my_list[−2], my_list[−3], my_list[−4])
40 30 20 10
negative indexes 负索引值 (从后到前取值)
"IndexError"的错误代码
因为不存在index[5], 发生报错
while前需要store the index先
len() 可以用来返回list的长度
使用len()预防indexerror
每个元素分配99
concatenating lists 串联列表
output
Example
Example 1
ans
Example 2
ans
Example 3
list slicing 切片
如果start在切片表达式中省略索引,默认0作为起始索引 省略end,默认end作为起始索引 (最后end不包括自己) 省略start和end, [:], 默认全部列表
Example
Example 2
in 检索是否在列表里
example
example 2
example 3
example 4
sequences
只能保存一组数据
tuples
元组
数据生成后无法改变
useful built-in function
commend
append
index 列举, 询问修改特定值
插入insert
my_list.sort() 从小到大排列 数字和字母皆可
output
remove
reverse 颠倒数据
output
del,在index里面删除值
output
max, min 最大最小值
example insert()
copy the list
example
example2
列表处理 7.7
计算总数
算平均列表的值
example
返回值
7.8 二维表格
Part 8
串联
加空格串联
加等于串联
start和end Index
无效的索引不会导致切片表达式引发异常
如果end索引指定的位置超出字符串的末尾,则Python将改用字符串的长度。
如果start索引指定了字符串开头之前的位置,则Python将改用0。
如果start索引大于end索引,则切片表达式将返回一个空字符串。
字符串测试
字符串修改
转大写
判断小写为真执行
搜索和替换
Subtopic 1
8.3 重复*
8.3 split分割
其他
Week2
input('value') 里面的值默认是str类型
print的时候空格问题: 第一种+ :变量和字符串相互相加默认不留空 第二种, : 变量和字符串相互相用逗号隔开默认留空
变量间互相转换问题 i, j = j, i
Chapter 1
1.2 Hardware and Software
Hardware
The Central Processing Unit (CPU)
Use for Run Programs
Early (Electromechanical CPU): the ENIAC (vacuum tubes and switches)
Now: Microprocessors
Main Memory
Random-Access Memory (RAM) Memory chips
A volatile type of memory, only for temporary storage
When the computer turn off, RAM will be erase
Secondary Storage Devices
Hold data for long periods of time
Disk drive
Traditional disk drives
spin circular disk
Solid-state drives
store in solid-state memory
USB drives
Flash menory
Memory sticks
Flash drives
CD (compact disc)
DVD (digital versatile disc)
Input Devices
Input is any data the computer collects from people and from other devices
keyboard, mouse, touchscreen...
Output Devices
Output is any data the computer produces for people or for other devices
video displays, printers...
Software
System Software
Operating System
Controls the internal operation of the computer
Utility Programs
enhances the computer's operation and safeguards data
virus scanners, file compression programs, backup programs
Software Development Tools
programmers use to create, modify, and test software
Application Software
E.g. Microsoft Word, PowerPoint
Ch1 Review
A(n) compiler program translates a high-level language program into a separate machine language program.
1.5 Using Python
Python is an interpreted language Python是一种解释型语言
interactive mode and script mode 交互模式和脚本模式
交互:输入并实习
脚本式:运行Python文件
1.4 How a program work
well-known:Intel, AMD, and Motorola Each brand of microprocessor has its own unique instruction set, which is typically understood only by microprocessors of the same brand.
程序运作流程
CPU在程序中执行指令时,参与fetch-decode-execute cycle的过程 (取-解码-执行)
An assembler translates an assembly language program to a machine language program 汇编语言转机器语言
编译并执行高级程序
使用解释器执行高级程序
syntax rules 语法错误
Chapter 2
2.1 Designing a Program
一个程序的制作流程
制作程序的两个步骤
Understand the task that the program is to perform. 了解程序要执行的任务。
咨询客户
Determine the steps that must be taken to perform the task. 确定执行任务必须采取的步骤。
Begin by breaking down the task into a series of steps
Pseudocode 伪代码 (pronounced “sue doe code”)
Don’t have to worry about syntax errors
Flowcharts
Ovals 椭圆
Terminal symbols
Begin and end point
Parallelograms 平行四边形
input symbols and output symbols
represent steps in which the program reads input or displays output
Rectangles 矩形
processing symbols 处理符号
represent steps in which the program performs some process on data, such as a mathematical calculation
2.4 Comments
#
2.5 Variables
Assignment Statements 赋值
variable = expression equal sign (=) is known as the assignment operator
Variable Naming Rules 变量命名规则
No: Variable Naming Rules
cannot contain spaces
The first character must be one of the letters a through z, A through Z, or an underscore character (_)
After the first character you may use the letters a through z or A through Z, the digits 0 through 9, or underscores
Uppercase and lowercase characters are distinct. This means the variable name ItemsOrdered is not the same as itemsordered.
常见命名方法
下划线字符来表示空格
gross_pay pay_rate hot_dogs_sold_today
camelCase命名
变量名称以小写字母开头
第二个单词和后续单词的第一个字符以大写形式书写
grossPay payRate hotDogsSoldToday
Numeric Data Types and Literals
int
以整数形式书写且不带小数点的数字文字,将被视为int。例子是7,124和- 9
float
用小数点写的数字文字被认为是float。例子是1.5,3.14159和5.0
str
内存中存储字符串
用type来确定数据类型
2.2 Input, Processing, and Output
The input, processing, and output of the pay calculating program
2.3 Displaying Output with the print Function
Strings and String Literals
single-quote marks (') or a set of double-quote marks (")
triple quotes (either """ or ''')
Can contain both single quotes and double quotes as part of the string
Also be used to surround multiline strings, something for which single and double quotes cannot be used
2.6 Reading Input from the Keyboard
variable = input(prompt)
字符串中引号内的最后一个字符是一个空格
数据转换
int(item)
float(item)
2.7 Performing Calculations
算数运算符
+ 加成 两个数相加
− 减法 从另一个减去一个数字
* 乘法 一个数字乘以另一个
/ 除法 将一个数除以另一个,并将结果作为浮点数给出
注意是浮点数
// 整数除法 将一个数除以另一个,得到结果为整数
当结果为正数时,它将被截断,这意味着其小数部分将被丢弃。
如果结果为负,则将其从零舍入到最接近的整数。
% 余 将一个数除以另一个,得到余数,求余不求商
** 指数 幂数
运算优先级
求幂: **
乘法,除法和余数: * / // %
乘法(*),浮点除法(/),整数除法(//)和余数(%)运算符具有相同的优先级
加减: + −
加(+)和减(−)运算符也具有相同的优先级
当两个具有相同优先级的运算符共享一个操作数时,这些运算符从左到右执行。
从左到右规则有一个例外。当两个**运算符共享一个操作数时,这些运算符从右到左执行。例如,表达式2**3**4的计算结果为2**(3**4)
用括号分组强制优先
将数学公式转换为编程语句
operation
2 int
int
2 float
float
1 int and 1 float
float
将长语句分成多行
换行符(反斜杠(\))将语句分成多行
2.8 More About Data Output
禁止print函数的结尾换行符, end=' '传递空格
2.9 Named Constants 命名常量
所有大写字母书写
避免magic number
2.10 Turtle graphics 海龟图形
import turtle
turtle.showturtle() 显示图标
turtle.forward(200)
Turning the Turtle
turtle.setheading(angle)
turtle.heading() 查询现在角度
turtle.penup() turtle.pendown() 线显示与否
turtle.circle(radius)
turtle.dot()
turtle.pensize(width) 线条粗细
turtle.pencolor(color)
'red', 'green', 'blue', 'yellow', and 'cyan'
turtle.bgcolor(color) 更改背景图片颜色
turtle.reset()
删除当前显示在图形窗口中的所有图形,将图形颜色重置为黑色,并将乌龟重置为屏幕中心的原始位置。此命令不会重置图形窗口的背景色
turtle.clear()
仅删除当前显示在图形窗口中的所有图形。它不会更改乌龟的位置,绘图颜色或图形窗口的背景颜色
turtle.clearscreen()
删除图形窗口中当前出现的所有图形,将图形颜色重置为黑色,将图形窗口的背景颜色重置为白色,并将乌龟重置为其在图形窗口中心的原始位置
turtle.setup(width, height) 指定图形窗口的大小
turtle.goto(x, y) 将海龟移到特定位置
turtle.pos() 获取海龟的当前位置
turtle.speed(speed) 控制海龟的动画速度
turtle.hideturtle() 隐藏turtle
turtle.showturtle() 显示turtle
turtle.write(text)
填充形状
turtle.begin_fill()
turtle.end_fill()
turtle.done() 保持图形窗口打开
Chapter 3
if statment
3.3 Comparing Strings 比较字符串
3.5 Logical Operators
Relational operators
Boolean expressions using relational operators
3.4 Multiple Nested Decision Structures
Chapter 4
4.1 Repetition Structures 重复结构
condition-controlled loop 条件控制循环 (while)
true/false condition to control the number of times that it repeats
count-controlled loop 计数控制循环 (for)
repeats a specific number of times
4.2 The while Loop
Condition-Controlled Loop 条件控制循环
A condition-controlled loop causes a statement or set of statements to repeat as long as a condition is true.
Each execution of the body of a loop is known as an iteration (迭代). In the sample run, the loop iterated three times.
Pretest Loop 前测试循环
提前在4行运行 keep_going = 'y', 第一次赋值True
后面才可以开始迭代 while keep_going == 'y':
Infinite Loops
需要按Ctrl+C停止
4.3 The for Loop: A Count-Controlled Loop
iterates a specific number of times 迭代特定次数
target variable is assign to the beginning of each loop iteration.
range的使用*
range(1, 10, 2)
"1",: starting value
"10": ending limit
"2": step value
printout: [1, 3, 5, 7, 9]
print('Number\tSquare') \t 用于数字和平方之间的转义序列
**是指数运算符
\t 转义序列会使数字在输出的两列中对齐
控制循环迭代
从最高到最低的可迭代序列
计算运行总计的逻辑
Augmented assignment operators 扩充赋值运算符
Floating Topic
Chapter 5
5.1 介绍Function的功能
2/14/2021, 8:19:07 PM
5.1 Introduction to Functions.html
5.2 介绍Function的示例
5.2 Defining and Calling a Void Function with answers.html
5.3 编程设计
flowcharts 流程图
Top-Down Design
Hierarchy Charts (structure chart) 结构图
5.4 Local Variables 局部变量
Chapter 6 File
访问顺序
Chapter 7 Lists and Tuples
list
mutable 易变的
可以有不同的类型,如字符串,整数和浮点数
built-in function list(), 可以使用这个把range()转化成列表
list * n 可重复运行n次
在python无法建立arrays, 但是lists功能和它相当
添加lists的索引 index
my_list = [10, 20, 30, 40]
print(my_list[0], my_list[1], my_list[2], my_list[3])
循环打印
my_list = [10, 20, 30, 40] print(my_list[−1], my_list[−2], my_list[−3], my_list[−4])
40 30 20 10
negative indexes 负索引值 (从后到前取值)
"IndexError"的错误代码
因为不存在index[5], 发生报错
while前需要store the index先
len() 可以用来返回list的长度
使用len()预防indexerror
每个元素分配99
concatenating lists 串联列表
output
Example
Example 1
ans
Example 2
ans
Example 3
list slicing 切片
如果start在切片表达式中省略索引,默认0作为起始索引 省略end,默认end作为起始索引 (最后end不包括自己) 省略start和end, [:], 默认全部列表
Example
Example 2
in 检索是否在列表里
example
example 2
example 3
example 4
sequences
只能保存一组数据
tuples
元组
数据生成后无法改变
useful built-in function
commend
append
index 列举, 询问修改特定值
插入insert
my_list.sort() 从小到大排列 数字和字母皆可
output
remove
reverse 颠倒数据
output
del,在index里面删除值
output
max, min 最大最小值
example insert()
copy the list
example
example2
列表处理 7.7
计算总数
算平均列表的值
example
返回值
7.8 二维表格
Chapter 8
串联
加空格串联
加等于串联
start和end Index
无效的索引不会导致切片表达式引发异常
如果end索引指定的位置超出字符串的末尾,则Python将改用字符串的长度。
如果start索引指定了字符串开头之前的位置,则Python将改用0。
如果start索引大于end索引,则切片表达式将返回一个空字符串。
字符串测试
字符串修改
转大写
判断小写为真执行
搜索和替换
Subtopic 1
8.3 重复*
8.3 split分割
Chapter 9
Q&A
Week2
input('value') 里面的值默认是str类型
print的时候空格问题: 第一种+ :变量和字符串相互相加默认不留空 第二种, : 变量和字符串相互相用逗号隔开默认留空
变量间互相转换问题 i, j = j, i