当前位置:首页>python>Python(七) 类型转换与构造函数

Python(七) 类型转换与构造函数

  • 2026-08-18 23:10:39
Python(七) 类型转换与构造函数
Python(七) 类型转换与构造函数
一、为什么要学习类型转换与构造函数

在 Python 程序中,数据都有自己的类型。

例如:

age = 18
price = 9.9
name = "小明"
is_passed = True

这里:

18 是整数类型 int
9.9 是浮点数类型 float
"小明" 是字符串类型 str
True 是布尔类型 bool

不同类型的数据能做的事情不同。

例如:

print(10 + 20)
print("10" + "20")

输出:

30
1020

第一行是数字加法,第二行是字符串拼接。

再看:

age = input("请输入年龄:")
if age >= 18:
print("已成年")

这段代码会出错,因为 input() 得到的是字符串,而 18 是整数,字符串不能直接和整数比较大小。

正确写法是:

age = int(input("请输入年龄:"))
if age >= 18:
print("已成年")

这里的 int() 就是把字符串转换成整数。

所以,学习类型转换与构造函数,可以帮助学生解决这些问题:

用户输入的数据为什么不能直接计算?
"18" 和 18 有什么区别?
怎样把字符串变成数字?
怎样把数字变成字符串?
怎样把列表、元组、集合互相转换?
为什么有些转换会报错?
int()float()str() 这些看起来像函数的东西到底是什么?

二、类型转换的定义

类型转换,指的是把一个数据从一种类型转换成另一种类型。

通俗地说:

类型转换就是给数据“换一种形式”。

例如,把字符串 "18" 转换成整数 18

age_text = "18"
age = int(age_text)
print(age)
print(type(age))

输出:

18
<class 'int'>

再比如,把整数 18 转换成字符串 "18"

age = 18
age_text = str(age)
print(age_text)
print(type(age_text))

输出:

18
<class 'str'>

注意:

屏幕上看到的内容可能一样,但类型可能不同。

print(18)
print("18")

输出看起来都是:

18
18

但它们的类型不同。

print(type(18))
print(type("18"))

输出:

<class 'int'>
<class 'str'>

三、构造函数的定义

构造函数是用来创建对象的函数。

在 Python 基础语法中,很多类型名本身就可以像函数一样调用,用来创建对应类型的数据。

例如:

int()
float()
str()
bool()
list()
tuple()
set()
dict()

这些都可以看作常见内置类型的构造函数。

例如:

number = int("123")
text = str(123)
items = list("abc")

这里:

int("123") 创建了一个整数对象。
str(123) 创建了一个字符串对象。
list("abc") 创建了一个列表对象。

在基础教学中,可以先这样理解:

构造函数就是用来“造出某种类型数据”的工具。


四、类型转换和构造函数的关系

类型转换和构造函数关系很密切。

很多时候,我们说“类型转换”,实际就是在调用某个类型的构造函数。

例如:

int("18")

既可以说:

把字符串 "18" 转换成整数 18。

也可以说:

调用 int 构造函数,创建一个整数对象。

再比如:

list("abc")

既可以理解为:

把字符串 "abc" 转换成列表。

也可以理解为:

调用 list 构造函数,创建一个列表对象。

也可以换种思维:

类型转换是目的,构造函数是常用手段。

五、常见类型转换函数和构造函数

Python 基础阶段常见的类型转换和构造函数如下:

构造函数
作用
示例
int()
创建整数
int("18")
float()
创建浮点数
float("3.14")
str()
创建字符串
str(100)
bool()
创建布尔值
bool(1)
list()
创建列表
list("abc")
tuple()
创建元组
tuple([1, 2, 3])
set()
创建集合
set([1, 1, 2])
dict()
创建字典
dict(name="小明", age=18)

六、查看类型:type()

学习类型转换时,经常需要查看转换前后的类型。

可以使用 type()

value = "123"
print(type(value))
number = int(value)
print(type(number))

输出:

<class 'str'>
<class 'int'>

这说明:

转换前,value 是字符串。
转换后,number 是整数。

type() 很适合演示,可以看清楚“看起来一样”和“类型一样”不是一回事。


七、隐式类型转换

隐式类型转换指的是 Python 自动完成的类型转换。

例如,整数和浮点数一起计算时,Python 通常会自动把结果变成浮点数。

result = 10 + 2.5
print(result)
print(type(result))

输出:

12.5
<class 'float'>

这里:

10

是整数。

2.5

是浮点数。

它们相加后,结果是浮点数。

再如:

print(10 / 2)
print(type(10 / 2))

输出:

5.0
<class 'float'>

即使 10 / 2 能整除,普通除法 / 的结果也是浮点数。


八、显式类型转换

显式类型转换指的是程序员主动调用构造函数进行转换。

例如:

age = int("18")
price = float("9.9")
text = str(100)

显式转换的好处是清楚、可控。

例如,处理用户输入时,我们通常需要显式转换:

age = int(input("请输入年龄:"))

因为 input() 得到的永远是字符串。


九、int():转换或创建整数

int() 用来创建整数对象,也常用于把其他数据转换成整数。


1. 不传参数
number = int()
print(number)
print(type(number))

输出:

0
<class 'int'>

不传参数时,int() 会创建整数 0


2. 字符串转整数
number = int("123")
print(number)
print(type(number))

输出:

123
<class 'int'>

注意:

字符串内容必须是合法整数。

正确:

int("123")
int("-123")

错误:

int("12.3")
int("abc")
int("一百")

这些都会报错。


3. 浮点数转整数
print(int(3.14))
print(int(9.99))
print(int(-3.14))

输出:

3
9
-3

注意:

int() 转换浮点数时,不是四舍五入,而是去掉小数部分。

如果需要四舍五入,可以使用 round()

print(round(3.14))
print(round(3.6))

输出:

3
4

提醒:

int() 是截掉小数部分,不是四舍五入。

4. 带进制转换的 int()

int() 还可以把指定进制的字符串转换成十进制整数。

print(int("1010"2))
print(int("12"8))
print(int("A"16))

输出:

10
10
10

说明:

int("1010", 2) 表示把二进制字符串 "1010" 转成十进制整数。
int("12", 8) 表示把八进制字符串 "12" 转成十进制整数。
int("A", 16) 表示把十六进制字符串 "A" 转成十进制整数。

这部分可以作为扩展内容,不需要初学时讲太深。


5. int() 常见应用

最常见应用是处理整数输入。

age = int(input("请输入年龄:"))
if age >= 18:
print("已成年")
else:
print("未成年")

计算两个整数的和:

a = int(input("请输入第一个整数:"))
b = int(input("请输入第二个整数:"))
print(f"和是{a + b}")

十、float():转换或创建浮点数

float() 用来创建浮点数对象,也常用于把其他数据转换成浮点数。


1. 不传参数
number = float()
print(number)
print(type(number))

输出:

0.0
<class 'float'>

2. 字符串转浮点数
price = float("9.9")
print(price)
print(type(price))

输出:

9.9
<class 'float'>

也可以转换整数形式的字符串:

print(float("10"))

输出:

10.0

错误示例:

float("abc")

这会报错,因为 "abc" 不是合法数字。


3. 整数转浮点数
number = float(10)
print(number)
print(type(number))

输出:

10.0
<class 'float'>

4. float() 常见应用

输入商品价格:

price = float(input("请输入商品单价:"))
count = int(input("请输入购买数量:"))
total = price * count
print(f"总价是{total:.2f}元")

输入身高体重:

height = float(input("请输入身高,单位米:"))
weight = float(input("请输入体重,单位千克:"))
bmi = weight / (height ** 2)
print(f"BMI 是{bmi:.2f}")

十一、str():转换或创建字符串

str() 用来创建字符串对象,也常用于把其他数据转换成字符串。


1. 不传参数
text = str()
print(text)
print(type(text))

输出中第一行是空白,因为 str() 得到的是空字符串 ""

<class 'str'>

2. 数字转字符串
age = 18
age_text = str(age)
print(age_text)
print(type(age_text))

输出:

18
<class 'str'>

常见场景:字符串拼接。

age = 18
print("年龄:" + str(age))

输出:

年龄:18

更推荐使用 f-string:

age = 18
print(f"年龄:{age}")

3. 其他类型转字符串
print(str(True))
print(str([123]))
print(str({"name""小明"}))

输出:

True
[1, 2, 3]
{'name': '小明'}

str() 会尽量把对象转换成适合人阅读的字符串形式。


4. str() 和 repr() 的简单区别

基础阶段可以简单了解:

str() 更偏向给人看。
repr() 更偏向给开发者看,通常更接近对象的代码形式。

例子:

text = "Python\n基础"
print(str(text))
print(repr(text))

输出类似:

Python
基础
'Python\n基础'

十二、bool():转换或创建布尔值

bool() 用来创建布尔值。

布尔值只有两个:

True
False

1. 不传参数
value = bool()
print(value)
print(type(value))

输出:

False
<class 'bool'>

2. 数字转布尔值
print(bool(0))
print(bool(1))
print(bool(-1))
print(bool(3.14))

输出:

False
True
True
True

规则:

数字 0 转成 False
非 0 数字转成 True

3. 字符串转布尔值
print(bool(""))
print(bool("hello"))
print(bool("False"))
print(bool("0"))

输出:

False
True
True
True

注意:

bool("False")

结果是 True,因为 "False" 是一个非空字符串。

bool("0")

结果也是 True,因为 "0" 也是非空字符串。

判断字符串内容是否表示 yes 或 no,应该自己写判断逻辑。

answer = input("是否继续?请输入 yes 或 no:").strip().lower()
if answer == "yes":
    is_continue = True
else:
    is_continue = False
print(is_continue)

4. 容器类型转布尔值
print(bool([]))
print(bool([123]))
print(bool({}))
print(bool({"name""小明"}))
print(bool(set()))
print(bool({123}))

输出:

False
True
False
True
False
True

规则:

空容器转成 False
非空容器转成 True

容器包括列表、元组、字典、集合等。


5. None 转布尔值
print(bool(None))

输出:

False

6. bool() 常见应用

判断列表是否为空:

students = []
if students:
print("有学生")
else:
print("没有学生")

这里 if students: 本质上就是根据列表转换成布尔值后的结果来判断。

判断字符串是否为空:

name = input("请输入姓名:").strip()
if name:
print(f"你好,{name}")
else:
print("姓名不能为空")

十三、list():转换或创建列表

list() 用来创建列表对象,也可以把可迭代对象转换成列表。

可迭代对象可以简单理解为:

可以一个一个取出元素的对象。

例如:

字符串
元组
集合
字典
range 对象

1. 不传参数
items = list()
print(items)
print(type(items))

输出:

[]
<class 'list'>

2. 字符串转列表
chars = list("Python")
print(chars)

输出:

['P', 'y', 't', 'h', 'o', 'n']

注意:

list("Python") 会把字符串拆成一个个字符。

如果想把一整段字符串作为列表中的一个元素,应该写:

items = ["Python"]
print(items)

输出:

['Python']

3. 元组转列表
numbers_tuple = (123)
numbers_list = list(numbers_tuple)
print(numbers_list)

输出:

[1, 2, 3]

这在需要修改数据时很常见,因为元组不能修改,列表可以修改。


4. range 转列表
numbers = list(range(16))
print(numbers)

输出:

[1, 2, 3, 4, 5]

5. 字典转列表
student = {"name""小明""age"18}
print(list(student))

输出:

['name', 'age']

注意:

字典直接转列表时,得到的是字典的键。

如果想得到值:

print(list(student.values()))

输出:

['小明', 18]

如果想得到键值对:

print(list(student.items()))

输出:

[('name', '小明'), ('age', 18)]

十四、tuple():转换或创建元组

tuple() 用来创建元组对象,也可以把其他可迭代对象转换成元组。


1. 不传参数
items = tuple()
print(items)
print(type(items))

输出:

()
<class 'tuple'>

2. 列表转元组
numbers = [123]
numbers_tuple = tuple(numbers)
print(numbers_tuple)

输出:

(1, 2, 3)

元组不能修改,所以适合保存不希望被改变的数据。


3. 字符串转元组
chars = tuple("abc")
print(chars)

输出:

('a', 'b', 'c')

4. 一个元素的元组

这是元组教学中的重点坑。

a = tuple([10])
print(a)

输出:

(10,)

如果直接写字面量,单元素元组必须带逗号:

a = (10,)
print(type(a))

输出:

<class 'tuple'>

如果写成:

a = (10)

它不是元组,而是整数。


十五、set():转换或创建集合

set() 用来创建集合对象。

集合的特点:

元素不重复。
元素没有固定顺序。

1. 不传参数
items = set()
print(items)
print(type(items))

输出:

set()
<class 'set'>

注意:

空集合必须用 set() 创建。

a = {}
print(type(a))

输出:

<class 'dict'>

空大括号 {} 表示空字典,不是空集合。


2. 列表转集合:去重
numbers = [122333]
unique_numbers = set(numbers)
print(unique_numbers)

输出可能是:

{1, 2, 3}

集合会自动去掉重复元素。

如果需要再转回列表:

unique_list = list(set(numbers))
print(unique_list)

注意:

集合不保证原来的顺序,所以转回列表后的顺序不一定和原来一样。


3. 字符串转集合
chars = set("banana")
print(chars)

输出可能是:

{'b', 'a', 'n'}

重复字符会被去掉,顺序也不固定。


4. 集合常见应用

判断是否有重复数据:

names = ["小明""小红""小明"]
iflen(names) == len(set(names)):
print("没有重复姓名")
else:
print("有重复姓名")

解释:

len(names) 是原列表长度。
len(set(names)) 是去重后的长度。
如果两个长度不同,说明原来有重复元素。

十六、dict():转换或创建字典

dict() 用来创建字典对象。

字典用于保存键值对数据。


1. 不传参数
student = dict()
print(student)
print(type(student))

输出:

{}
<class 'dict'>

2. 使用关键字参数创建字典
student = dict(name="小明", age=18, score=95)
print(student)

输出:

{'name': '小明', 'age': 18, 'score': 95}

注意:

这种写法中,键名要符合变量名规则。

例如可以写:

dict(name="小明")

但不能直接写:

dict("student-name"="小明")

这种写法是错误的。

如果键名包含特殊字符,可以使用普通字典写法:

student = {"student-name""小明"}

3. 使用键值对列表创建字典
pairs = [("name""小明"), ("age"18), ("score"95)]
student = dict(pairs)
print(student)

输出:

{'name': '小明', 'age': 18, 'score': 95}

这里 pairs 中的每个元素都是一个长度为 2 的元组。

第一个值作为键,第二个值作为值。


4. 使用 zip() 创建字典
keys = ["name""age""score"]
values = ["小明"1895]
student = dict(zip(keys, values))
print(student)

输出:

{'name': '小明', 'age': 18, 'score': 95}

这适合把两个列表组合成一个字典。


5. dict() 转换时的常见错误

错误示例:

dict(["name""age"])

这会报错,因为 dict() 需要的是一组键值对,而不是普通字符串列表。

正确写法:

dict([("name""小明"), ("age"18)])

十七、complex():转换或创建复数

复数不是基础阶段最常用的类型,但可以作为了解。

complex() 用来创建复数。

number = complex(12)
print(number)
print(type(number))

输出:

(1+2j)
<class 'complex'>

这里 1 是实部,2 是虚部。

也可以把字符串转换成复数:

print(complex("1+2j"))

输出:

(1+2j)

基础教学中,知道有这个类型即可,不需要深入讲复数运算。


十八、input() 与类型转换

input() 是学习类型转换时最重要的场景。

无论用户输入什么,input() 得到的都是字符串。

value = input("请输入内容:")
print(value)
print(type(value))

即使用户输入:

123

类型仍然是:

<class 'str'>

1. 输入整数
age = int(input("请输入年龄:"))
print(age)
print(type(age))

2. 输入小数
height = float(input("请输入身高:"))
print(height)
print(type(height))

3. 输入多个整数

分步写法:

a, b = input("请输入两个整数,用空格分隔:").split()
a = int(a)
b = int(b)
print(a + b)

简写:

a, b = map(intinput("请输入两个整数,用空格分隔:").split())
print(a + b)

对于初学者,建议先讲分步写法,再讲 map()


4. 输入 yes 或 no 转成布尔值

不要直接使用:

is_continue = bool(input("是否继续?"))

因为只要用户输入非空字符串,结果就是 True

例如用户输入 "no"bool("no") 也是 True

推荐:

answer = input("是否继续?请输入 yes 或 no:").strip().lower()
if answer == "yes":
    is_continue = True
else:
    is_continue = False
print(is_continue)

也可以写得更简洁:

answer = input("是否继续?请输入 yes 或 no:").strip().lower()
is_continue = answer == "yes"
print(is_continue)

十九、转换失败与异常

不是所有数据都能成功转换。

例如:

int("abc")

会报错。

float("hello")

也会报错。

常见错误是 ValueError


1. 转换前做简单判断

如果只判断正整数,可以使用 isdigit()

text = input("请输入年龄:").strip()
if text.isdigit():
    age = int(text)
print(f"年龄是{age}")
else:
print("输入错误,请输入整数")

注意:

isdigit() 适合判断普通正整数。

例如:

"18".isdigit()

结果是 True

但是:

"-18".isdigit()
"3.14".isdigit()

结果都是 False

所以它不能完整判断负数或小数。


2. 使用 try...except 处理转换错误

更通用的方式是使用异常处理。

try:
    age = int(input("请输入年龄:"))
print(f"年龄是{age}")
except ValueError:
print("输入错误,请输入整数")

这段代码的意思是:

1.
尝试把输入转换成整数。
2.
如果转换成功,就继续执行。
3.
如果转换失败,就输出错误提示,而不是让程序直接崩溃。

基础阶段可以先让学生知道有这种写法,等讲异常处理时再深入。


二十、可迭代对象和容器转换

list()tuple()set() 经常用于容器之间的转换。

常见转换:

list("abc")          # ['a', 'b', 'c']
tuple([123])     # (1, 2, 3)
set([112])       # {1, 2}
list(range(3))       # [0, 1, 2]

可以简单总结:

如果一个对象可以被一个一个取出元素,它通常就可以传给 list()tuple()set()


1. 列表、元组、集合互相转换
numbers_list = [1223]
numbers_tuple = tuple(numbers_list)
numbers_set = set(numbers_list)
print(numbers_tuple)
print(numbers_set)

输出可能是:

(1, 2, 2, 3)
{1, 2, 3}

说明:

转成元组后,元素保留。
转成集合后,重复元素会被去掉。

2. 转换可能改变数据特点

列表转集合:

items = ["A""B""A""C"]
items_set = set(items)
print(items_set)

可能输出:

{'A', 'C', 'B'}

注意:

重复的 "A" 被去掉了。
顺序可能改变。

所以不能在需要保留顺序和重复数据的场景中随便转成集合。


二十一、构造函数和字面量写法

创建对象有两种常见方式:

1.
使用字面量。
2.
使用构造函数。

1. 字面量写法

字面量就是直接写出数据本身。

number = 18
text = "Python"
items = [123]
student = {"name""小明"}

这种写法简单直观,是最常见的写法。


2. 构造函数写法
number = int("18")
text = str(18)
items = list("abc")
student = dict(name="小明")

构造函数更适合:

需要转换类型时。
需要从已有数据创建新对象时。
需要动态创建数据结构时。

3. 对比示例

创建空列表:

a = []
b = list()

两者都可以。

创建空字典:

a = {}
b = dict()

两者都可以。

创建空集合:

a = set()

注意,空集合不能写成 {},因为 {} 是空字典。


二十二、自定义类中的构造函数简单了解

前面讲的 int()str()list() 等,是 Python 内置类型的构造函数。

以后学习面向对象时,也会遇到自定义类的构造函数。

例如:

classStudent:
def__init__(self, name, age):
self.name = name
self.age = age
student = Student("小明"18)

这里:

Student("小明"18)

是在创建一个 Student 对象。

而:

def__init__(self, name, age):

是对象创建时用来初始化数据的方法。

基础阶段可以先简单理解:

int() 创建整数对象。
list() 创建列表对象。
Student() 创建学生对象。

也就是说,构造函数的核心作用是“创建对象”。


二十三、类型转换的常见注意事项
1. input() 得到的永远是字符串
age = input("请输入年龄:")

即使用户输入 18age 也是字符串 "18"

需要计算或比较大小时,要转换:

age = int(input("请输入年龄:"))

2. int() 不能直接转换小数字符串

错误:

int("3.14")

如果字符串是小数形式,要先用 float()

number = float("3.14")

如果确实想再转整数:

number = int(float("3.14"))
print(number)

输出:

3

注意,这会去掉小数部分。


3. int() 转浮点数不是四舍五入
print(int(3.9))

输出:

3

如果要四舍五入:

print(round(3.9))

输出:

4

4. bool("False") 是 True
print(bool("False"))

输出:

True

原因:

非空字符串转布尔值都是 True

所以不能用 bool() 直接理解字符串内容的真假含义。


5. list("abc") 会拆成字符
print(list("abc"))

输出:

['a', 'b', 'c']

如果想得到包含整个字符串的列表,要写:

print(["abc"])

输出:

['abc']

6. set() 会去重,也可能打乱顺序
items = ["A""B""A""C"]
print(set(items))

输出顺序不一定固定。

所以如果既要去重又要保留顺序,不能简单依赖 set()

可以使用:

items = ["A""B""A""C"]
unique_items = []
for item in items:
if item notin unique_items:
        unique_items.append(item)
print(unique_items)

输出:

['A', 'B', 'C']

7. dict() 需要键值对

错误:

dict(["name""age"])

正确:

dict([("name""小明"), ("age"18)])

8. 转换不一定会改变原对象
numbers = (123)
numbers_list = list(numbers)
print(numbers)
print(numbers_list)

输出:

(1, 2, 3)
[1, 2, 3]

list(numbers) 会创建一个新的列表,不会把原来的元组本身变成列表。


二十四、常见错误对照表
错误现象
常见原因
修改方法
输入 10 和 20,相加得到 1020
input()
 得到的是字符串
使用 int() 或 float() 转换
int("abc")
 报错
字符串不是合法整数
转换前检查,或用 try...except
int("3.14")
 报错
"3.14"
 是小数字符串
使用 float("3.14")
int(3.9)
 得到 3
int()
 去掉小数部分
四舍五入用 round()
bool("False")
 得到 True
非空字符串都是真
手动判断字符串内容
list("abc")
 得到字符列表
字符串是可迭代对象
想保留整体用 ["abc"]
{}
 不是空集合
空大括号是空字典
空集合用 set()
dict(["name", "age"])
 报错
dict()
 需要键值对
使用 [("name", "小明")]
set()
 去重后顺序变了
集合无固定顺序
需要保序去重时用循环

二十五、综合示例
示例:学生成绩录入与判断
name = input("请输入学生姓名:").strip()
age = int(input("请输入学生年龄:"))
score = float(input("请输入学生成绩:"))
is_passed = score >= 60
student = dict(
    name=name,
    age=age,
    score=score,
    is_passed=is_passed
)
print("学生信息:")
print(f"姓名:{student['name']}")
print(f"年龄:{student['age']}")
print(f"成绩:{student['score']:.2f}")
if student["is_passed"]:
print("结果:及格")
else:
print("结果:不及格")

这段代码中用到了:

input():接收用户输入。
strip():去掉输入前后的空格。
int():把年龄转成整数。
float():把成绩转成浮点数。
比较表达式 score >= 60:得到布尔值。
dict():创建学生信息字典。
f-string:格式化输出。

可以引导学生分析:

1.
用户输入的数据先是字符串。
2.
年龄需要转换成整数。
3.
成绩需要转换成浮点数。
4.
判断是否及格会得到布尔值。
5.
最后用字典组织学生信息。

二十六、练习
练习 1:字符串转整数

请把字符串 "100" 转换成整数,并输出它的类型。

参考答案:

text = "100"
number = int(text)
print(number)
print(type(number))

练习 2:输入两个整数求和

请让用户输入两个整数,输出它们的和。

参考答案:

a = int(input("请输入第一个整数:"))
b = int(input("请输入第二个整数:"))
print(f"和是{a + b}")

练习 3:输入商品单价和数量

请让用户输入商品单价和数量,计算总价,保留两位小数。

参考答案:

price = float(input("请输入商品单价:"))
count = int(input("请输入购买数量:"))
total = price * count
print(f"总价是{total:.2f}元")

练习 4:判断 bool() 的结果

请判断下面代码的输出。

print(bool(0))
print(bool(1))
print(bool(""))
print(bool("False"))
print(bool([]))
print(bool([1]))

参考答案:

False
True
False
True
False
True

练习 5:列表、元组、集合转换

给定列表:

numbers = [122333]

请分别把它转换成元组和集合。

参考答案:

numbers = [122333]
numbers_tuple = tuple(numbers)
numbers_set = set(numbers)
print(numbers_tuple)
print(numbers_set)

练习 6:字符串转列表

请把字符串 "Python" 转换成列表。

参考答案:

chars = list("Python")
print(chars)

输出:

['P', 'y', 't', 'h', 'o', 'n']

练习 7:创建字典

请使用 dict() 创建一个学生字典,包含姓名、年龄和成绩。

参考答案:

student = dict(name="小明", age=18, score=95)
print(student)

练习 8:修正错误代码

下面代码有什么问题?请改正。

age = input("请输入年龄:")
if age >= 18:
print("已成年")

参考答案:

问题是 input() 得到的是字符串,不能直接和整数 18 比较。

正确写法:

age = int(input("请输入年龄:"))
if age >= 18:
print("已成年")

练习 9:处理转换异常

请让用户输入年龄,如果输入的不是整数,提示“输入错误”。

参考答案:

try:
    age = int(input("请输入年龄:"))
print(f"年龄是{age}")
except ValueError:
print("输入错误")

练习 10:保留顺序去重

给定列表:

items = ["A""B""A""C""B"]

请去重,并保留原来的第一次出现顺序。

参考答案:

items = ["A""B""A""C""B"]
unique_items = []
for item in items:
if item notin unique_items:
        unique_items.append(item)
print(unique_items)

输出:

['A', 'B', 'C']

二十七、总结

类型转换是把一种类型的数据转换成另一种类型。

构造函数是创建某种类型对象的函数。

在 Python 基础语法中,很多类型名本身就可以作为构造函数使用。

重点掌握:

1.
int() 用来创建整数,常用于把数字字符串转换成整数。
2.
float() 用来创建浮点数,常用于处理小数输入。
3.
str() 用来创建字符串,常用于把数字转换成文本。
4.
bool() 用来创建布尔值,但非空字符串都会转成 True
5.
list() 用来创建列表,可以把字符串、元组、range 等转换成列表。
6.
tuple() 用来创建元组,适合保存不希望修改的数据。
7.
set() 用来创建集合,可以去重,但不保证顺序。
8.
dict() 用来创建字典,需要键值对数据。
9.
input() 得到的一定是字符串,需要计算时要先转换。

一句话概括:

类型转换解决“数据形式不合适”的问题,构造函数负责“创建某种类型的数据”。

最新文章

随机文章

基本 文件 流程 错误 SQL 调试
  1. 请求信息 : 2026-08-21 23:28:02 HTTP/2.0 GET : https://f.mffb.com.cn/a/505260.html
  2. 运行时间 : 0.432592s [ 吞吐率:2.31req/s ] 内存消耗:4,640.53kb 文件加载:140
  3. 缓存信息 : 0 reads,0 writes
  4. 会话信息 : SESSION_ID=15ffac7898e2018c0996418602a5c746
  1. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/public/index.php ( 0.79 KB )
  2. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/autoload.php ( 0.17 KB )
  3. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/autoload_real.php ( 2.49 KB )
  4. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/platform_check.php ( 0.90 KB )
  5. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/ClassLoader.php ( 14.03 KB )
  6. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/autoload_static.php ( 4.90 KB )
  7. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper.php ( 8.34 KB )
  8. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-validate/src/helper.php ( 2.19 KB )
  9. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/helper.php ( 1.47 KB )
  10. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/stubs/load_stubs.php ( 0.16 KB )
  11. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Exception.php ( 1.69 KB )
  12. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-container/src/Facade.php ( 2.71 KB )
  13. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/deprecation-contracts/function.php ( 0.99 KB )
  14. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/polyfill-mbstring/bootstrap.php ( 8.26 KB )
  15. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/polyfill-mbstring/bootstrap80.php ( 9.78 KB )
  16. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/var-dumper/Resources/functions/dump.php ( 1.49 KB )
  17. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-dumper/src/helper.php ( 0.18 KB )
  18. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/var-dumper/VarDumper.php ( 4.30 KB )
  19. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/App.php ( 15.30 KB )
  20. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-container/src/Container.php ( 15.76 KB )
  21. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/container/src/ContainerInterface.php ( 1.02 KB )
  22. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/provider.php ( 0.19 KB )
  23. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Http.php ( 6.04 KB )
  24. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper/Str.php ( 7.29 KB )
  25. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Env.php ( 4.68 KB )
  26. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/common.php ( 0.03 KB )
  27. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/helper.php ( 18.78 KB )
  28. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Config.php ( 5.54 KB )
  29. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/app.php ( 0.95 KB )
  30. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/cache.php ( 0.78 KB )
  31. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/console.php ( 0.23 KB )
  32. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/cookie.php ( 0.56 KB )
  33. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/database.php ( 2.48 KB )
  34. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/Env.php ( 1.67 KB )
  35. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/filesystem.php ( 0.61 KB )
  36. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/lang.php ( 0.91 KB )
  37. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/log.php ( 1.35 KB )
  38. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/middleware.php ( 0.19 KB )
  39. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/route.php ( 1.89 KB )
  40. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/session.php ( 0.57 KB )
  41. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/trace.php ( 0.34 KB )
  42. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/view.php ( 0.82 KB )
  43. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/event.php ( 0.25 KB )
  44. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Event.php ( 7.67 KB )
  45. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/service.php ( 0.13 KB )
  46. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/AppService.php ( 0.26 KB )
  47. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Service.php ( 1.64 KB )
  48. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Lang.php ( 7.35 KB )
  49. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/lang/zh-cn.php ( 13.70 KB )
  50. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/Error.php ( 3.31 KB )
  51. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/RegisterService.php ( 1.33 KB )
  52. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/services.php ( 0.14 KB )
  53. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/PaginatorService.php ( 1.52 KB )
  54. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/ValidateService.php ( 0.99 KB )
  55. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/ModelService.php ( 2.04 KB )
  56. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/Service.php ( 0.77 KB )
  57. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Middleware.php ( 6.72 KB )
  58. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/BootService.php ( 0.77 KB )
  59. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/Paginator.php ( 11.86 KB )
  60. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-validate/src/Validate.php ( 63.20 KB )
  61. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/Model.php ( 23.55 KB )
  62. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/Attribute.php ( 21.05 KB )
  63. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/AutoWriteData.php ( 4.21 KB )
  64. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/Conversion.php ( 6.44 KB )
  65. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/DbConnect.php ( 5.16 KB )
  66. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/ModelEvent.php ( 2.33 KB )
  67. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/RelationShip.php ( 28.29 KB )
  68. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/contract/Arrayable.php ( 0.09 KB )
  69. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/contract/Jsonable.php ( 0.13 KB )
  70. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/contract/Modelable.php ( 0.09 KB )
  71. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Db.php ( 2.88 KB )
  72. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/DbManager.php ( 8.52 KB )
  73. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Log.php ( 6.28 KB )
  74. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Manager.php ( 3.92 KB )
  75. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/log/src/LoggerTrait.php ( 2.69 KB )
  76. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/log/src/LoggerInterface.php ( 2.71 KB )
  77. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Cache.php ( 4.92 KB )
  78. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/simple-cache/src/CacheInterface.php ( 4.71 KB )
  79. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper/Arr.php ( 16.63 KB )
  80. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/cache/driver/File.php ( 7.84 KB )
  81. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/cache/Driver.php ( 9.03 KB )
  82. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/CacheHandlerInterface.php ( 1.99 KB )
  83. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/Request.php ( 0.09 KB )
  84. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Request.php ( 55.78 KB )
  85. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/middleware.php ( 0.25 KB )
  86. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Pipeline.php ( 2.61 KB )
  87. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/TraceDebug.php ( 3.40 KB )
  88. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/middleware/SessionInit.php ( 1.94 KB )
  89. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Session.php ( 1.80 KB )
  90. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/session/driver/File.php ( 6.27 KB )
  91. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/SessionHandlerInterface.php ( 0.87 KB )
  92. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/session/Store.php ( 7.12 KB )
  93. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Route.php ( 23.73 KB )
  94. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleName.php ( 5.75 KB )
  95. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Domain.php ( 2.53 KB )
  96. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleGroup.php ( 22.43 KB )
  97. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Rule.php ( 26.95 KB )
  98. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleItem.php ( 9.78 KB )
  99. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/route/app.php ( 1.72 KB )
  100. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/Route.php ( 4.70 KB )
  101. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/dispatch/Controller.php ( 4.74 KB )
  102. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Dispatch.php ( 10.44 KB )
  103. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/controller/Index.php ( 4.81 KB )
  104. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/BaseController.php ( 2.05 KB )
  105. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/facade/Db.php ( 0.93 KB )
  106. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/connector/Mysql.php ( 5.44 KB )
  107. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/PDOConnection.php ( 52.47 KB )
  108. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Connection.php ( 8.39 KB )
  109. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/ConnectionInterface.php ( 4.57 KB )
  110. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/builder/Mysql.php ( 16.58 KB )
  111. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Builder.php ( 24.06 KB )
  112. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/BaseBuilder.php ( 27.50 KB )
  113. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Query.php ( 15.71 KB )
  114. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/BaseQuery.php ( 45.13 KB )
  115. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/TimeFieldQuery.php ( 7.43 KB )
  116. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/AggregateQuery.php ( 3.26 KB )
  117. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ModelRelationQuery.php ( 20.07 KB )
  118. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ParamsBind.php ( 3.66 KB )
  119. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ResultOperation.php ( 7.01 KB )
  120. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/WhereQuery.php ( 19.37 KB )
  121. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/JoinAndViewQuery.php ( 7.11 KB )
  122. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/TableFieldInfo.php ( 2.63 KB )
  123. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/Transaction.php ( 2.77 KB )
  124. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/log/driver/File.php ( 5.96 KB )
  125. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/LogHandlerInterface.php ( 0.86 KB )
  126. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/log/Channel.php ( 3.89 KB )
  127. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/event/LogRecord.php ( 1.02 KB )
  128. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/Collection.php ( 16.47 KB )
  129. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/View.php ( 1.70 KB )
  130. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/View.php ( 4.39 KB )
  131. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Response.php ( 8.81 KB )
  132. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/response/View.php ( 3.29 KB )
  133. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Cookie.php ( 6.06 KB )
  134. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-view/src/Think.php ( 8.38 KB )
  135. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/TemplateHandlerInterface.php ( 1.60 KB )
  136. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/Template.php ( 46.61 KB )
  137. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/template/driver/File.php ( 2.41 KB )
  138. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/template/contract/DriverInterface.php ( 0.86 KB )
  139. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/runtime/temp/067d451b9a0c665040f3f1bdd3293d68.php ( 11.98 KB )
  140. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/Html.php ( 4.42 KB )
  1. CONNECT:[ UseTime:0.000786s ] mysql:host=127.0.0.1;port=3306;dbname=f_mffb;charset=utf8mb4
  2. SHOW FULL COLUMNS FROM `fenlei` [ RunTime:0.001173s ]
  3. SELECT * FROM `fenlei` WHERE `fid` = 0 [ RunTime:0.032467s ]
  4. SELECT * FROM `fenlei` WHERE `fid` = 63 [ RunTime:0.000613s ]
  5. SHOW FULL COLUMNS FROM `set` [ RunTime:0.001440s ]
  6. SELECT * FROM `set` [ RunTime:0.000551s ]
  7. SHOW FULL COLUMNS FROM `article` [ RunTime:0.001481s ]
  8. SELECT * FROM `article` WHERE `id` = 505260 LIMIT 1 [ RunTime:0.009829s ]
  9. UPDATE `article` SET `lasttime` = 1787326082 WHERE `id` = 505260 [ RunTime:0.026291s ]
  10. SELECT * FROM `fenlei` WHERE `id` = 66 LIMIT 1 [ RunTime:0.000681s ]
  11. SELECT * FROM `article` WHERE `id` < 505260 ORDER BY `id` DESC LIMIT 1 [ RunTime:0.002250s ]
  12. SELECT * FROM `article` WHERE `id` > 505260 ORDER BY `id` ASC LIMIT 1 [ RunTime:0.023131s ]
  13. SELECT * FROM `article` WHERE `id` < 505260 ORDER BY `id` DESC LIMIT 10 [ RunTime:0.082219s ]
  14. SELECT * FROM `article` WHERE `id` < 505260 ORDER BY `id` DESC LIMIT 10,10 [ RunTime:0.029673s ]
  15. SELECT * FROM `article` WHERE `id` < 505260 ORDER BY `id` DESC LIMIT 20,10 [ RunTime:0.066376s ]
0.437076s