0. Programming
What is Programming Language? 什么是编程语言?
- Programming language like a code for writing the instructions the computer will follow
- 编程语言就像是编写计算机指令的代码
Syntax and Semantics 语法和语义
- Syntax语法: the precise form of every structure in programming language
- Semantics语义: the precise meaning of every structure in programming language
True or False? The syntax of a language is its meaning, and semantics is its form
Answer: False ❌ (Syntax is form, semantics is meaning)
1. High-level vs Low-level Languages 高级语言 vs 低级语言
High-level computer languages 高级计算机语言
- Designed to be used and understood by humans
- Example:
c = a + b
Low-level language 低级语言
- Computer hardware can only understand machine language
- Example: Load numbers, add in CPU, store result
选择题 Which one?
Computer languages designed to be used and understood by humans are:
- a) natural languages
- b) high-level computer languages ✅
- c) machine languages
- d) fetch-execute languages
2. Compilers vs Interpreters 编译器 vs 解释器
Compilers 编译器
- Convert programs into machine language all at once
- 一次性将程序转换为机器语言
- Compiled programs run faster 编译的程序运行更快
Interpreters 解释器
- Analyze and execute source code instruction by instruction
- 逐条分析和执行源代码
- More flexible programming environment 更灵活的编程环境
3. What is python? Python是啥?
python for python language and the interpreter
- python是一种解释型语言,使用python解释器执行代码
- python解释器可以在不同平台上运行,如windows、linux、mac等
- python解释器的版本有很多种,如python2、python3等
python is a general-purpose language
- python是一种通用型语言,可用于开发各种应用程序
- python的语法简单易学,适合初学者
- domain-specific language:HTML,JS,Matlab…
dynamic language or static language?
- python is a moden dynamic language
- python 既是动态语言,也是静态类型语言?
python is open source
- python是一种开源语言,用户可以自由使用、修改和分发python代码
features of Python - Syntax Characteristics句法特征
- Readability
- Simplicity
4. Python development environment Python开发环境
Python official website: https://www.python.org/
True or False? A programming environment refers to a place where programmers work.
Answer: True ✅
What is IDLE?
IDLE是python的集成开发环境,提供了一个简单的编辑器和调试器
Script Mode or Interactive Mode
IDLE提供了两种运行python代码的方式:脚本模式和交互式模式
脚本模式用于执行python脚本文件,交互式模式用于在IDLE中直接输入和执行python代码
Anaconda: 数据科学Python发行版
Anaconda是一个用于数据科学的python发行版,提供了许多常用的python库和工具
安装Anaconda后,用户可以直接使用其中的python解释器和开发环境
自带Jupyter Notebook
more IDE
除了IDLE,还有许多其他的python开发环境,如PyCharm、Visual Studio Code等
这些开发环境提供了更多的功能和工具,如代码自动补全、调试器、版本控制等
顺手即可
5. Python program
Function 函数
def <function_name>(<parameters>):
"""docstring"""
<statement(s)>- 函数定义以def关键字开头,后面跟着函数名、参数列表和冒号
- 函数体缩进4个空格
- 函数可以有参数,也可以没有参数
- 函数可以有返回值,也可以没有返回值
- 函数可以有文档字符串,用于描述函数的功能和参数('''或者"""均可)
Parameters 参数
>>> def greet(person):
print("Hello", person)
print("How are you?")
>>> greet("Terry")
Hello Terry
How are you?Comments 注释
#Single line comments 单行注释'''...'''or"""..."""Multiple line comments 多行注释
Indentation 缩进
- Uses whitespace to indicate code structure 使用空格表示代码结构
- Replaces traditional symbols like
{}
6. Basic methods of writing programs
IPO Framework IPO框架
- Input输入: the beginning of a program 程序开始
- Process处理: computes incoming data 计算输入数据
- Output输出: displays the result 显示结果
Software development process 软件开发流程
软件开发过程包括需求分析、设计、编码、测试和维护等阶段
- Analyze the Problem分析问题
- Determine Specifications确定规格 - what it will do
- Create a Design创建设计 - how it will work
- Implement the Design实现设计
- Test/Debug测试/调试
- Maintain维护
True or False Questions:
- The best way to write a program is to immediately type in some code and then debug it until it works.
False ❌ (应该先分析设计)
- An algorithm can be written without using a programming language.
True ✅ (可以用伪代码)
- Programs no longer require modification after they are written and debugged.
False ❌ (程序需要持续维护)
Variables and Identifiers 变量和标识符
Names are given to identify variables, functions, classes, etc. 标识符是用于标识变量、函数、类等的名称
Rules规则:
- Must begin with letter or underscore 必须以字母或下划线开头
- Followed by letters, digits, or underscores 后面可以是字母、数字或下划线
- Case sensitive 区分大小写
- 标识符不能使用python的保留字(reserved words/keywords) 例如:if、else、while、for、def、class、True、False、None等
- 标识符不能使用python的Built-in Functions 例如:print、input、eval、type、len等
Keywords 关键词
Reserved words that cannot be used as variable names 保留字,不能用作变量名
Expressions 表达式
Fragments of code that produce new data values 产生新数据值的代码片段
Components组成:
- Literals字面量:
3.9,1,"Hello" - Variables变量
- Operators运算符:
+,-,*,/,**
示例:
- 算术表达式:2 + 3、x * y
- 函数调用表达式:len(my_list)
Assignment赋值
x = 3.9 * x * (1-x)
fahrenheit = 9/5 * celsius + 32Variable assignment models变量赋值模型:
- Sticky-note model 便利贴模型 ✅
- Variable-as-box model 变量作为盒子模型
Simultaneous Assignment同时赋值
sum, diff = x+y, x-y
x, y = y, x # Swap values交换值7. Definite Loops(有限循环)
for i in range(10):
print(i)
for i in [1,2,3,4,5]:
print(i)
for i in "hello":
print(i)range()函数
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]8. Input and Output 输入输出(IO)
input()
a = input('Please enter a number:') # 返回字符串input()函数用于从用户获取输入
函数返回一个字符串
print()
print(3+4) # 7
print(3, 4, 3+4) # 3 4 7
print("The answer is", 3+4) # The answer is 7eval()函数
可以使用eval()函数将字符串转换为其他类型
- Evaluates strings as Python expressions
- 本质是将字符串作为Python表达式求值(有风险)
也可以使用int()、float()等函数将字符串转换为整数或浮点数
>>> eval("1+3")
4
>>> eval("print('hello')")
hello9. Summary总结
Key Concepts关键概念
- Rules of Python: comments, indentation Python规则:注释、缩进
- IPO framework IPO框架
- Software Development Process 软件开发流程
- Identifiers, Expressions, Statements 标识符、表达式、语句
- Loops 循环
- IO 输入输出