Lecture 2 - Python
一、Python的执行方式
1. Executable file(可执行文件):生成可以直接运行的程序。(经过编译的程序)
2. Interpreter(解释器):Python 使用解释器逐行执行代码,而不是通过编译成机器代码来运行。
• Multiple Versions(多版本支持):Python可以有不同版本的解释器。
3. Environmental Configuration(环境配置):Python的环境需要正确配置,以确保代码能正确执行。
4. Linux-based logic (ported to MS Windows)(基于Linux的逻辑,移植到Windows):Python开发时一般基于Linux环境,但它已被移植到Windows系统。
编译与解释流程:
• Compiler(编译器):将源代码编译成机器代码,并生成可执行文件。
• Interpreter(解释器):逐行读取源代码并直接执行。
二、命令行和集成开发环境(IDE)
1. Command Line(命令行):
• 主要通过命令行执行脚本,可以解释并运行多个文件,输出可执行文件。
• Multiple files need to be interpreted together(多个文件需要一起解释):如果有多个文件依赖关系,需要一起在命令行中执行。
2. Integrated Development Environment (IDE)(集成开发环境):
• IDE结合了文本编辑器(text editor)、命令行、编译器或解释器,提供了一个更完整的开发环境。
• 包括:文件资源管理器(File Explorer)、变量资源管理器(Variable Explorer)、调试工具(Debug Tool)、AI助手(AI assistant)等。
三、浏览器基础IDE与Python应用
1. Browser-based IDE(基于浏览器的IDE):
• Deepnote 和 Jupyter 是基于浏览器的Python开发环境,适合在线开发和数据分析。
四、Python 数据类型
1. Text Type(文本类型):str
2. Numeric Types(数值类型):int, float, complex
3. Sequence Types(序列类型):list, tuple, range
4. Mapping Type(映射类型):dict
5. Set Types(集合类型):set, frozenset
frozenset 是不可变的集合,支持集合的常见操作,但不支持添加或删除元素
# 创建一个字节数组
data = bytearray([1, 2, 3, 4, 5])# 创建一个memoryview对象
mv = memoryview(data)# 修改memoryview中的数据
mv[0] = 10 # 修改第一个字节print(data)
# 输出: bytearray(b'\n\x02\x03\x04\x05')
6. Boolean Type(布尔类型):bool
7. Binary Types(二进制类型):bytes, bytearray, memoryview
memoryview 允许直接访问字节数据的内存,从而避免了复制数据,可以更高效地处理大数据。
8. None Type(空类型):NoneType
None 是Python中唯一的一个值类型,常用于表示空值或函数没有返回值。
x = "Hello World" #str
x = 20 #int
x = 20.5 #float
x = 1j # complex
x = ["apple", "banana", "cherry"] #list
x = ("apple", "banana", "cherry") #tuple
x = range(6) #range
x = {"name" : "John", "age" : 36} #dict
x = {"apple", "banana", "cherry"} #set
x = frozenset({"apple", "banana", "cherry"}) #frozenset
x = True #bool
x = b"Hello" #bytes
x = bytearray(5) #bytearray
x = memoryview(bytes(5)) #memoryview
x = None #NoneType
五、类型转换(Cast)
• 通过 int()、float() 等函数将一个数据类型转换为另一个类型。
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3x = float(1) # x will be 1.0
y = float(2.8) # y will be 2.8
z = float("3") # z will be 3.0
w = float("4.2") # w will be 4.2
六、String
a = "Hello"
b = "World"
c = a + b
print(c)txt = "We are the so-called \"Vikings\" from the north."
price = 59
txt = f"The price is {price:.2f} dollars"
print(txt)txt = f"The price is {20 * 59} dollars"
print(txt)
• f-string 是 Python 3.6 及以后版本引入的一种用于格式化字符串的方式。
• 通过在字符串前加上字母 f,可以在字符串中直接使用花括号 {} 嵌入变量或表达式,同时还可以应用格式说明符来控制输出的样式(如浮动小数点、整数等)
七、Operator
八、Python 控制流(循环)
1. For Loop
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
2. While Loop
break(退出循环)和 continue(跳过当前迭代)。
3. For Loop
• 用于遍历可迭代对象(如列表、字典等)中的每个元素,执行相关操作。
• 示例中使用 break 跳出循环。
七、Function
*kids 是一个可变参数。这意味着函数可以接受任意数量的位置参数,并将这些参数收集到一个元组 kids 中。这里的 * 表示将所有传递给函数的位置参数存储为一个元组。
八、Learning Material
- scikit-learn: [https://scikit-learn.org/stable/index.html](https://scikit-learn.org/stable/index.html)
- scipy: [https://scipy.org/](https://scipy.org/)
- numpy: [https://numpy.org/](https://numpy.org/)https://learnpython.org
https://www.youtube.com/watch?v=_uQrJ0TkZlc