PythonPython
2025-04-08 12:00阅读:21评论:0
Python之基础知识
字符串前面带有f作用 例如:f"Hello, {name}!" 其含义是使用 f - 字符串将变量值嵌入到字符串中,通熟易懂就是将字符串和变量拼接起来,如下 在上述代码中,f"Hello, {name}!" 是一个 f - 字符串,{name} 是一个表达式,Python 会计算 name 的值(也
_PROTECTED2_PROTECTED3_PROTECTED1__
- 其含义是使用 f - 字符串将变量值嵌入到字符串中,通熟易懂就是将字符串和变量拼接起来,如下
1
2
3
4
5
6
# 定义一个变量name = "Python"# 使用 f - 字符串将变量值嵌入到字符串中message = f"Hello, {name}!"print(message) - 在上述代码中,f"Hello, {name}!" 是一个 f - 字符串,{name} 是一个表达式,Python 会计算 name 的值(也就是 "Python"),然后把这个值嵌入到字符串里,最终输出 "Hello, Python!"。
1
2
3
4
5
6
7
print( random.randint(1,10) ) # 产生 1 到 10 的一个整数型随机数print( random.random() ) # 产生 0 到 1 之间的随机浮点数print( random.uniform(1.1,5.4) ) # 产生 1.1 到 5.4 之间的随机浮点数,区间可以不是整数print( random.choice('tomorrow') ) # 从字符串中随机选取一个字符print(random.choice(["AA", "BB", "CC"])) # 从数组中随机选取一个元素print( random.randrange(1,100,2) ) # 生成从1到100的间隔为2的随机整数 _PROTECTED1_PROTECTED2_PROTECTED0__循环
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
user_list = [row[0] for row in rows] # 这里的 row[0] 实际就是 for row in rows 循环 得到的row,再从row中取出第一个元素放入user_list中 等价于 user_list2 = [] # 准备一个空列表 for row in rows: # 遍历rows中的每一个元素 user = row[0] # 取出当前row的第一个元素 user_list2.append(user) # 添加到列表中 示例 rowList = [ [ { "password": None, "created_by": None, "id": 1, "updated_by": None, "created_time": None, "name": "张三", "updated_time": None, "is_delete": None } ], [ { "password": None, "created_by": None, "id": 2, "updated_by": None, "created_time": None, "name": "张三2", "updated_time": None, "is_delete": None } ], [ { "password": None, "created_by": None, "id": 3, "updated_by": None, "created_time": None, "name": "张三3", "updated_time": None, "is_delete": None } ] ] user_list2 = [row[0] for row in rowList] print(user_list2)打印结果[{'password': None, 'created_by': None, 'id': 1, 'updated_by': None, 'created_time': None, 'name': '张三', 'updated_time': None, 'is_delete': None}, {'password': None, 'created_by': None, 'id': 2, 'updated_by': None, 'created_time': None, 'name': '张三2', 'updated_time': None, 'is_delete': None}, {'password': None, 'created_by': None, 'id': 3, 'updated_by': None, 'created_time': None, 'name': '张三3', 'updated_time': None, 'is_delete': None}]