本文将围绕Python的基本知识点进行详细阐述,包括数据类型、流程控制、函数、模块和文件操作等方面。
一、数据类型
1、整型
a = 10 print(type(a)) # 输出:
2、浮点型
b = 3.14 print(type(b)) # 输出:
3、字符串
c = 'Hello, world!' print(type(c)) # 输出:
二、流程控制
1、条件语句
age = 18 if age >= 18: print("已成年") else: print("未成年")
2、循环语句
for i in range(1, 6): print(i)
三、函数
1、定义函数
def add(a, b): return a + b result = add(3, 5) print(result) # 输出:8
2、函数参数
def greet(name): print("Hello, " + name + "!") greet("Alice") # 输出:Hello, Alice!
3、递归函数
def factorial(n): if n == 1: return 1 else: return n * factorial(n - 1) result = factorial(5) print(result) # 输出:120
四、模块
1、导入模块
import math print(math.sqrt(25)) # 输出:5.0
2、自定义模块
# math_operations.py def add(a, b): return a + b def subtract(a, b): return a - b # main.py import math_operations print(math_operations.add(3, 5)) # 输出:8
五、文件操作
1、打开文件
file = open("data.txt", "w") file.write("Hello, world!") file.close()
2、读取文件
file = open("data.txt", "r") data = file.read() print(data) # 输出:Hello, world! file.close()
通过以上几个方面的介绍,对Python的基本知识点进行了备忘。希望能够帮助你在开发过程中更好地运用Python语言。