Python 笔记 — 数据类型转换

目录

  • 一、转换为字符串类型
    • 1、str
    • 2、f-string
    • 3、repr
    • 4、format
    • 5、字符串连接
  • 二、转换为数字类型
    • 1、转换为整数类型
    • 2、转换为浮点类型
    • 3、转换为布尔类型
  • 三、转换为列表类型
    • 1、list
    • 2、列表解析
    • 3、扩展运算符 *
    • 4、extend
    • 5、append
  • 四、转换为元组类型
    • 1、tuple
    • 2、zip
    • 3、使用 () 将数据括起来
  • 五、转换为字典类型
    • 1、dict
    • 2、字典推导式
  • 六、转换为集合类型
    • 1、set
    • 2、集合推导式
    • 3、{}
  • 七、总结
    • 1、字符串类型(str)
    • 2、数字类型(int,float,complex)
    • 3、列表类型(list)
    • 4、元组类型(tuple)
    • 5、字典类型(dict)
    • 6、集合类型(set)

一、转换为字符串类型

1、str

使用内置的 str() 函数,通过 str() 函数可以将数字、列表、字典、元组、集合等类型转换为字符串类型。

# 数字类型转换为字符串类型
num = 42
str_num = str(num)
print(str_num) # 42
print(type(str_num)) # <class 'str'>

# 列表类型转换为字符串类型
my_list = [1, 2, 3]
str_list = str(my_list)
print(str_list) # [1, 2, 3]
print(type(str_list)) # <class 'str'>

# 字典类型转换为字符串类型
my_dict = {'name': 'Alice', 'age': 25}
str_dict = str(my_dict)
print(str_dict) # {'name': 'Alice', 'age': 25}
print(type(str_dict)) # <class 'str'>

# 元组类型转换为字符串类型
my_tuple = (1, 2, 3)
str_tuple = str(my_tuple)
print(str_tuple) # (1, 2, 3)
print(type(str_tuple)) # <class 'str'>

# 集合类型转换为字符串类型
my_set = {1, 2, 3}
str_set = str(my_set)
print(str_set) # {1, 2, 3}
print(type(str_set)) # <class 'str'>

2、f-string

使用格式化字符串(f-string),使用 f”{变量}” 的形式可以将数字、列表、字典、元组、集合等类型转换为字符串类型。

# 数字类型转换为字符串类型
num = 42
str_num = f"{num}"
print(str_num) # 42
print(type(str_num)) # <class 'str'>

# 列表类型转换为字符串类型
my_list = [1, 2, 3]
str_list = f"{my_list}"
print(str_list) # [1, 2, 3]
print(type(str_list)) # <class 'str'>

# 字典类型转换为字符串类型
my_dict = {'name': 'Alice', 'age': 25}
str_dict = f"{my_dict}"
print(str_dict) # {'name': 'Alice', 'age': 25}
print(type(str_dict)) # <class 'str'>

# 元组类型转换为字符串类型
my_tuple = (1, 2, 3)
str_tuple = f"{my_tuple}"
print(str_tuple) # (1, 2, 3)
print(type(str_tuple)) # <class 'str'>

# 集合类型转换为字符串类型
my_set = {1, 2, 3}
str_set = f"{my_set}"
print(str_set) # {1, 2, 3}
print(type(str_set)) # <class 'str'>

3、repr

使用内置的 repr() 函数,通过 repr() 函数可以将数字、列表、字典、元组、集合等类型转换为字符串类型。

# 数字类型转换为字符串类型
num = 42
str_num = repr(num)
print(str_num) # 42
print(type(str_num)) # <class 'str'>

# 列表类型转换为字符串类型
my_list = [1, 2, 3]
str_list = repr(my_list)
print(str_list) # [1, 2, 3]
print(type(str_list)) # <class 'str'>

# 字典类型转换为字符串类型
my_dict = {'name': 'Alice', 'age': 25}
str_dict = repr(my_dict)
print(str_dict) # {'name': 'Alice', 'age': 25}
print(type(str_dict)) # <class 'str'>

# 元组类型转换为字符串类型
my_tuple = (1, 2, 3)
str_tuple = repr(my_tuple)
print(str_tuple) # (1, 2, 3)
print(type(str_tuple)) # <class 'str'>

# 集合类型转换为字符串类型
my_set = {1, 2, 3}
str_set = repr(my_set)
print(str_set) # {1, 2, 3}
print(type(str_set)) # <class 'str'>

4、format

使用字符串格式化方法,使用 “{}”.format(变量) 的形式可以将数字、列表、字典、元组、集合等类型转换为字符串类型。

# 数字类型转换为字符串类型
num = 42
str_num = "{}".format(num)
print(str_num) # 42
print(type(str_num)) # <class 'str'>

# 列表类型转换为字符串类型
my_list = [1, 2, 3]
str_list = "{}".format(my_list)
print(str_list) # [1, 2, 3]
print(type(str_list)) # <class 'str'>

# 字典类型转换为字符串类型
my_dict = {'name': 'Alice', 'age': 25}
str_dict = "{}".format(my_dict)
print(str_dict) # {'name': 'Alice', 'age': 25}
print(type(str_dict)) # <class 'str'>

# 元组类型转换为字符串类型
my_tuple = (1, 2, 3)
str_tuple = "{}".format(my_tuple)
print(str_tuple) # (1, 2, 3)
print(type(str_tuple)) # <class 'str'>

# 集合类型转换为字符串类型
my_set = {1, 2, 3}
str_set = "{}".format(my_set)
print(str_set) # {1, 2, 3}
print(type(str_set)) # <class 'str'>

5、字符串连接

使用字符串连接,通过将变量与空字符串 “” 进行连接操作,可以将数字、列表、字典、元组、集合等类型转换为字符串类型。

# 数字类型转换为字符串类型
num = 42
str_num = "" + str(num)
print(str_num) # 42
print(type(str_num)) # <class 'str'>

# 列表类型转换为字符串类型
my_list = [1, 2, 3]
str_list = "" + str(my_list)
print(str_list) # [1, 2, 3]
print(type(str_list)) # <class 'str'>

# 字典类型转换为字符串类型
my_dict = {'name': 'Alice', 'age': 25}
str_dict = "" + str(my_dict)
print(str_dict) # {'name': 'Alice', 'age': 25}
print(type(str_dict)) # <class 'str'>

# 元组类型转换为字符串类型
my_tuple = (1, 2, 3)
str_tuple = "" + str(my_tuple)
print(str_tuple) # (1, 2, 3)
print(type(str_tuple)) # <class 'str'>

# 集合类型转换为字符串类型
my_set = {1, 2, 3}
str_set = "" + str(my_set)
print(str_set) # {1, 2, 3}
print(type(str_set)) # <class 'str'>

二、转换为数字类型

1、转换为整数类型

# 浮点数类型转换为整数类型
num_float = 3.14
int_result = int(num_float)
print(int_result) # 3
print(type(int_result)) # <class 'int'>

# 布尔类型转换为整数类型
bool_value = True
int_result = int(bool_value)
print(int_result) # 1
print(type(int_result)) # <class 'int'>

# 复数类型转换为整数类型
num_complex = 2 + 3j
int_result = int(num_complex.real)
print(int_result) # 2
print(type(int_result)) # <class 'int'>

# 字符串类型转换为整数类型
str_value = "42"
int_result = int(str_value)
print(int_result) # 42
print(type(int_result)) # <class 'int'>

2、转换为浮点类型

# 整数类型转换为浮点数类型
num_int = 42
float_result = float(num_int)
print(float_result) # 42.0
print(type(float_result)) # <class 'float'>

# 布尔类型转换为浮点数类型
bool_value = True
float_result = float(bool_value)
print(float_result) # 1.0
print(type(float_result)) # <class 'float'>

# 复数类型转换为浮点数类型(保留实数部分)
num_complex = 2 + 3j
float_result = float(num_complex.real)
print(float_result) # 2.0
print(type(float_result)) # <class 'float'>

# 字符串类型转换为浮点数类型
str_value = "3.14"
float_result = float(str_value)
print(float_result) # 3.14
print(type(float_result)) # <class 'float'>

3、转换为布尔类型

# 整型转换为布尔值
num_int = 42
bool_value = bool(num_int)
print(bool_value) # True
print(type(bool_value)) # <class 'bool'>

num_int = 0
bool_value = bool(num_int)
print(bool_value) # False
print(type(bool_value)) # <class 'bool'>

# 浮点型转换为布尔值
num_float = 3.14
bool_value = bool(num_float)
print(bool_value) # True
print(type(bool_value)) # <class 'bool'>

num_float = 0.0
bool_value = bool(num_float)
print(bool_value) # False
print(type(bool_value)) # <class 'bool'>

# 字符串类型转换为布尔值
str_val = "True"
bool_value = bool(str_val)
print(bool_value) # True
print(type(bool_value)) # <class 'bool'>

str_val = "False"
bool_value = bool(str_val)
print(bool_value) # True
print(type(bool_value)) # <class 'bool'>

str_val = ""
bool_value = bool(str_val)
print(bool_value) # False
print(type(bool_value)) # <class 'bool'>

# 列表类型转换为布尔值
my_list = [1, 2, 3]
bool_value = bool(my_list)
print(bool_value) # True
print(type(bool_value)) # <class 'bool'>

my_list = []
bool_value = bool(my_list)
print(bool_value) # False
print(type(bool_value)) # <class 'bool'>

# 元组类型转换为布尔值
my_tuple = (1, 2, 3)
bool_value = bool(my_tuple)
print(bool_value) # True
print(type(bool_value)) # <class 'bool'>

my_tuple = ()
bool_value = bool(my_tuple)
print(bool_value) # False
print(type(bool_value)) # <class 'bool'>

# 字典类型转换为布尔值
my_dict = {"key": "value"}
bool_value = bool(my_dict)
print(bool_value) # True
print(type(bool_value)) # <class 'bool'>

my_dict = {}
bool_value = bool(my_dict)
print(bool_value) # False
print(type(bool_value)) # <class 'bool'>

# 集合类型转换为布尔值
my_set = {1, 2, 3}
bool_value = bool(my_set)
print(bool_value) # True
print(type(bool_value)) # <class 'bool'>

my_set = set()
bool_value = bool(my_set)
print(bool_value) # False
print(type(bool_value)) # <class 'bool'>

# None类型转换为布尔值
none_value = None
bool_value = bool(none_value)
print(bool_value) # False
print(type(bool_value)) # <class 'bool'>

为 True 的数据:非零整数和非零浮点数、非空的字符串、列表、元组、字典和集合、非空的迭代对象。

为 False 的数据:数字类型中的零值,包括0和0.0、空字符串、列表、元组、字典和集合、空的迭代对象、None类型。

三、转换为列表类型

1、list

使用内置的 list() 函数,可以将可迭代对象转换为列表类型。

# 整数类型转换为列表类型
num_int = 42
list_val = list(str(num_int))
print(list_val) # ['4', '2']
print(type(list_val)) # <class 'list'>

# 浮点数类型转换为列表类型
num_float = 3.14
list_val = list(str(num_float))
print(list_val) # ['3', '.', '1', '4']
print(type(list_val)) # <class 'list'>

# 布尔类型转换为列表类型
bool_val = True
list_val = [bool_val]
print(list_val) # [True]
print(type(list_val)) # <class 'list'>

bool_val = False
list_val = [bool_val]
print(list_val) # [False]
print(type(list_val)) # <class 'list'>

# 复数类型转换为列表类型
num_complex = 2 + 3j
list_val = list(str(num_complex))
print(list_val) # ['(', '2', '+', '3', 'j', ')']
print(type(list_val)) # <class 'list'>

# 字符串类型转换为列表类型
str_val = "Hello, World!"
list_val = list(str_val)
print(list_val) # ['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!']
print(type(list_val)) # <class 'list'>

# 元组类型转换为列表类型
tuple_val = (1, 2, 3)
list_val = list(tuple_val)
print(list_val) # [1, 2, 3]
print(type(list_val)) # <class 'list'>

# 字典类型转换为列表类型(获取键、值或键值对的列表)
dict_val = {'a': 1, 'b': 2, 'c': 3}
keys_list = list(dict_val.keys())
print(keys_list) # ['a', 'b', 'c']
print(type(keys_list)) # <class 'list'>

values_list = list(dict_val.values())
print(values_list) # [1, 2, 3]
print(type(values_list)) # <class 'list'>

items_list = list(dict_val.items())
print(items_list) # [('a', 1), ('b', 2), ('c', 3)]
print(type(items_list)) # <class 'list'>

# 集合类型转换为列表类型
set_val = {1, 2, 3}
list_val = list(set_val)
print(list_val) # [1, 2, 3]
print(type(list_val)) # <class 'list'>

2、列表解析

可以使用列表解析来将其它类型的数据转换为列表类型。

# 整数类型转换为列表类型
num_int = 42
list_int = [num_int]
print(list_int) # [42]
print(type(list_int)) # <class 'list'>

# 浮点数类型转换为列表类型
num_float = 3.14
list_float = [num_float]
print(list_float) # [3.14]
print(type(list_float)) # <class 'list'>

# 布尔类型转换为列表类型
bool_value = True
list_bool = [bool_value]
print(list_bool) # [True]
print(type(list_bool)) # <class 'list'>

# 复数类型转换为列表类型
num_complex = 2 + 3j
list_complex = [num_complex]
print(list_complex) # [(2+3j)]
print(type(list_complex)) # <class 'list'>

# 字符串类型转换为列表类型
str_val = "Hello"
list_str = [char for char in str_val]
print(list_str) # ['H', 'e', 'l', 'l', 'o']
print(type(list_str)) # <class 'list'>

# 元组类型转换为列表类型
tuple_val = (1, 2, 3)
list_tuple = [item for item in tuple_val]
print(list_tuple) # [1, 2, 3]
print(type(list_tuple)) # <class 'list'>

# 集合类型转换为列表类型
set_val = {1, 2, 3}
list_set = [item for item in set_val]
print(list_set) # [1, 2, 3]
print(type(list_set)) # <class 'list'>

# 字典类型转换为列表类型(获取键或值的列表)
dict_val = {'a': 1, 'b': 2, 'c': 3}
keys_list = [key for key in dict_val]
print(keys_list) # ['a', 'b', 'c']
print(type(keys_list)) # <class 'list'>

values_list = [dict_val[key] for key in dict_val]
print(values_list) # [1, 2, 3]
print(type(values_list)) # <class 'list'>

3、扩展运算符 *

可以使用扩展运算符将可迭代对象中的元素拆解并添加到列表中。

# 整数类型转换为列表类型
num_int = 42
list_int = [*str(num_int)]
print(list_int) # ['4', '2']
print(type(list_int)) # <class 'list'>

# 浮点数类型转换为列表类型
num_float = 3.14
list_float = [*str(num_float)]
print(list_float) # ['3', '.', '1', '4']
print(type(list_float)) # <class 'list'>

# 布尔类型转换为列表类型
bool_value = True
list_bool = [bool_value]
print(list_bool) # [True]
print(type(list_bool)) # <class 'list'>

# 复数类型转换为列表类型
num_complex = 2 + 3j
list_complex = [*str(num_complex)]
print(list_complex) # ['(', '2', '+', '3', 'j', ')']
print(type(list_complex)) # <class 'list'>

# 字符串类型转换为列表类型
str_val = "Hello"
list_str = [*str_val]
print(list_str) # ['H', 'e', 'l', 'l', 'o']
print(type(list_str)) # <class 'list'>

# 元组类型转换为列表类型
tuple_val = (1, 2, 3)
list_tuple = [*tuple_val]
print(list_tuple) # [1, 2, 3]
print(type(list_tuple)) # <class 'list'>

# 集合类型转换为列表类型
set_val = {1, 2, 3}
list_set = [*set_val]
print(list_set) # [1, 2, 3]
print(type(list_set)) # <class 'list'>

# 字典类型转换为列表类型(获取键或值的列表)
dict_val = {'a': 1, 'b': 2, 'c': 3}
keys_list = [*dict_val]
print(keys_list) # ['a', 'b', 'c']
print(type(keys_list)) # <class 'list'>

values_list = [*dict_val.values()]
print(values_list) # [1, 2, 3]
print(type(values_list)) # <class 'list'>

4、extend

对于多个元素或可迭代对象,可以使用 extend() 方法将其添加到列表末尾。

# 整数类型转换为列表类型
num_int = 42
list_int = []
list_int.extend([num_int])
print(list_int) # [42]
print(type(list_int)) # <class 'list'>

# 浮点数类型转换为列表类型
num_float = 3.14
list_float = []
list_float.extend([num_float])
print(list_float) # [3.14]
print(type(list_float)) # <class 'list'>

# 布尔类型转换为列表类型
bool_value = True
list_bool = []
list_bool.extend([bool_value])
print(list_bool) # [True]
print(type(list_bool)) # <class 'list'>

# 复数类型转换为列表类型
num_complex = 2 + 3j
list_complex = []
list_complex.extend([num_complex])
print(list_complex) # [(2+3j)]
print(type(list_complex)) # <class 'list'>

# 字符串类型转换为列表类型
str_val = "Hello"
list_str = []
list_str.extend(str_val)
print(list_str) # ['H', 'e', 'l', 'l', 'o']
print(type(list_str)) # <class 'list'>

# 元组类型转换为列表类型
tuple_val = (1, 2, 3)
list_tuple = []
list_tuple.extend(tuple_val)
print(list_tuple) # [1, 2, 3]
print(type(list_tuple)) # <class 'list'>

# 集合类型转换为列表类型
set_val = {1, 2, 3}
list_set = []
list_set.extend(set_val)
print(list_set) # [1, 2, 3]
print(type(list_set)) # <class 'list'>

# 字典类型转换为列表类型(获取键或值的列表)
dict_val = {'a': 1, 'b': 2, 'c': 3}
keys_list = []
values_list = []
keys_list.extend(dict_val.keys())
print(keys_list) # ['a', 'b', 'c']
print(type(keys_list)) # <class 'list'>

values_list.extend(dict_val.values())
print(values_list) # [1, 2, 3]
print(type(values_list)) # <class 'list'>

5、append

对于单个元素,可以使用append()方法将其添加到空列表中。

# 整数类型转换为列表类型
num_int = 42
list_int = []
list_int.append(num_int)
print(list_int) # [42]
print(type(list_int)) # <class 'list'>

# 浮点数类型转换为列表类型
num_float = 3.14
list_float = []
list_float.append(num_float)
print(list_float) # [3.14]
print(type(list_float)) # <class 'list'>

# 布尔类型转换为列表类型
bool_value = True
list_bool = []
list_bool.append(bool_value)
print(list_bool) # [True]
print(type(list_bool)) # <class 'list'>

# 复数类型转换为列表类型
num_complex = 2 + 3j
list_complex = []
list_complex.append(num_complex)
print(list_complex) # [(2+3j)]
print(type(list_complex)) # <class 'list'>

# 字符串类型转换为列表类型
str_val = "Hello"
list_str = []
for char in str_val:
    list_str.append(char)
print(list_str) # ['H', 'e', 'l', 'l', 'o']
print(type(list_str)) # <class 'list'>

# 元组类型转换为列表类型
tuple_val = (1, 2, 3)
list_tuple = []
for item in tuple_val:
    list_tuple.append(item)
print(list_tuple) # [1, 2, 3]
print(type(list_tuple)) # <class 'list'>

# 集合类型转换为列表类型
set_val = {1, 2, 3}
list_set = []
for item in set_val:
    list_set.append(item)
print(list_set) # [1, 2, 3]
print(type(list_set)) # <class 'list'>

# 字典类型转换为列表类型(获取键或值的列表)
dict_val = {'a': 1, 'b': 2, 'c': 3}
keys_list = []
values_list = []
for key in dict_val:
    keys_list.append(key)
    values_list.append(dict_val[key])
print(keys_list) # ['a', 'b', 'c']
print(type(keys_list)) # <class 'list'>

print(values_list) # [1, 2, 3]
print(type(values_list)) # <class 'list'>

四、转换为元组类型

1、tuple

# 整数类型转换为元组类型
num_int = 42
tuple_result = tuple([num_int])
print(tuple_result) # (42,)
print(type(tuple_result)) # <class 'tuple'>
# 当使用 tuple() 函数将单个元素转换为元组时,如果没有在方括号中将元素包裹起来,Python 会将元素视为独立的值而不是元组的一部分。为了避免这种歧义,将元素包裹在方括号中以确保它被视为单个元素的列表,从而形成一个元组。
# 在这种情况下,[num_int] 创建了一个只包含一个元素的列表,即整数42。当将这个列表传递给 tuple() 函数时,该函数会将列表转换为元组,并在结果中显示逗号,以指示这是一个包含单个元素的元组。这种情况下的结果(42,)是为了区分单个元素的元组和普通的数值表达式。

# 浮点数类型转换为元组类型
num_float = 3.14
tuple_result = tuple([num_float])
print(tuple_result) # (3.14,)
print(type(tuple_result)) # <class 'tuple'>

# 布尔类型转换为元组类型
bool_value = True
tuple_result = tuple([bool_value])
print(tuple_result) # (True,)
print(type(tuple_result)) # <class 'tuple'>

# 复数类型转换为元组类型
complex_num = 2 + 3j
tuple_result = tuple([complex_num])
print(tuple_result) # ((2+3j),)
print(type(tuple_result)) # <class 'tuple'>

# 字符串类型转换为元组类型
str_value = "Hello"
tuple_result = tuple(str_value)
print(tuple_result) # ('H', 'e', 'l', 'l', 'o')
print(type(tuple_result)) # <class 'tuple'>

# 列表类型转换为元组类型
my_list = [1, 2, 3]
tuple_result = tuple(my_list)
print(tuple_result) # (1, 2, 3)
print(type(tuple_result)) #  <class 'tuple'>

# 集合类型转换为元组类型
my_set = {4, 5, 6}
tuple_result = tuple(my_set)
print(tuple_result) # (4, 5, 6)
print(type(tuple_result)) # <class 'tuple'>

# 字典类型转换为元组类型
my_dict = {"key1": 1, "key2": 2}
tuple_result = tuple(my_dict.items())
print(tuple_result) # (('key1', 1), ('key2', 2))
print(type(tuple_result)) # <class 'tuple'>

2、zip

# 整数类型转换为元组
num_int = 42
tuple_result = tuple(zip([num_int]))
print(tuple_result) # ((42,),)
print(type(tuple_result)) # <class 'tuple'>

# 浮点数类型转换为元组
num_float = 3.14
tuple_result = tuple(zip([num_float]))
print(tuple_result) # ((3.14,),)
print(type(tuple_result)) # <class 'tuple'>

# 布尔类型转换为元组
bool_value = True
tuple_result = tuple(zip([bool_value]))
print(tuple_result) # ((True,),)
print(type(tuple_result)) # <class 'tuple'>

# 复数类型转换为元组
complex_num = 1 + 2j
tuple_result = tuple(zip([complex_num]))
print(tuple_result) # (((1+2j),),)
print(type(tuple_result)) # <class 'tuple'>

# 字符串类型转换为元组
str_val = "Hello"
tuple_result = tuple(zip([str_val]))
print(tuple_result) # (('Hello',),)
print(type(tuple_result)) # <class 'tuple'>

# 列表类型转换为元组
my_list = [1, 2, 3]
tuple_result = tuple(zip([my_list]))
print(tuple_result) # (([1, 2, 3],),)
print(type(tuple_result)) # <class 'tuple'>

# 集合类型转换为元组
my_set = {1, 2, 3}
tuple_result = tuple(zip([my_set]))
print(tuple_result) # (({1, 2, 3},),)
print(type(tuple_result)) # <class 'tuple'>

# 字典类型转换为元组
my_dict = {"key": "value"}
tuple_result = tuple(zip([my_dict]))
print(tuple_result) # (({'key': 'value'},),)
print(type(tuple_result)) # <class 'tuple'>

# 多个可迭代对象的元素按索引配对
my_list = [1, 2, 3]
my_str = "abc"
my_set = {4, 5, 6}

tuple_result = tuple(zip(my_list, my_str, my_set))
print(tuple_result) # ((1, 'a', 4), (2, 'b', 5), (3, 'c', 6))
print(type(tuple_result)) # <class 'tuple'>

num_int = 42
num_float = 3.14
bool_value = True
num_complex = 1 + 2j
str_value = "Hello"
my_list = [1, 2, 3]
my_set = {4, 5, 6}
my_dict = {'a': 7, 'b': 8, 'c': 9}

tuple_result = tuple(zip(
    (num_int, num_float, bool_value, num_complex, str_value, my_list, my_set, my_dict),
    (10, 20, False, 2 + 3j, "World", [4, 5, 6], {7, 8, 9}, {'x': 10, 'y': 11, 'z': 12}),
    ("abc", [1.1, 2.2, 3.3], True, 4 - 5j, 100, {1, 2, 3}, {'a': 1, 'b': 2, 'c': 3}, [1, 2, 3, 4])
))

print(tuple_result)
# ((42, 10, 'abc'), (3.14, 20, [1.1, 2.2, 3.3]), (True, False, True), ((1+2j), (2+3j), (4-5j)), ('Hello', 'World', 100), ([1, 2, 3], [4, 5, 6], {1, 2, 3}), ({4, 5, 6}, {8, 9, 7}, {'a': 1, 'b': 2, 'c': 3}), ({'a': 7, 'b': 8, 'c': 9}, {'x': 10, 'y': 11, 'z': 12}, [1, 2, 3, 4]))
print(type(tuple_result)) # <class 'tuple'>

3、使用 () 将数据括起来

num_int = 42
num_float = 3.14
bool_value = True
num_complex = 1 + 2j
str_value = "Hello"
my_list = [1, 2, 3]
my_set = {4, 5, 6}
my_dict = {'a': 7, 'b': 8, 'c': 9}

tuple_result = (
    (num_int, num_float, bool_value, num_complex, str_value, my_list, my_set, my_dict),
    (10, 20, False, 2 + 3j, "World", [4, 5, 6], {7, 8, 9}, {'x': 10, 'y': 11, 'z': 12}),
    ("abc", [1.1, 2.2, 3.3], True, 4 - 5j, 100, {1, 2, 3}, {'a': 1, 'b': 2, 'c': 3}, [1, 2, 3, 4])
)

print(tuple_result)
# (
#   (42, 3.14, True, (1+2j), 'Hello', [1, 2, 3], {4, 5, 6}, {'a': 7, 'b': 8, 'c': 9}),
#   (10, 20, False, (2+3j), 'World', [4, 5, 6], {8, 9, 7}, {'x': 10, 'y': 11, 'z': 12}),
#   ('abc', [1.1, 2.2, 3.3], True, (4-5j), 100, {1, 2, 3}, {'a': 1, 'b': 2, 'c': 3}, [1, 2, 3, 4])
# )

print(type(tuple_result)) # <class 'tuple'>

五、转换为字典类型

1、dict

# 整数类型转换为字典类型
num_int = 42
dict_result = dict([(num_int, 'integer')])
print(dict_result) # {42: 'integer'}
print(type(dict_result)) # <class 'dict'>

# 浮点数类型转换为字典类型
num_float = 3.14
dict_result = dict([(num_float, 'float')])
print(dict_result) # {3.14: 'float'}
print(type(dict_result)) # <class 'dict'>

# 布尔类型转换为字典类型
bool_value = True
dict_result = dict([(bool_value, 'boolean')])
print(dict_result) # {True: 'boolean'}
print(type(dict_result)) # <class 'dict'>

# 复数转换为字典类型
num_complex = 1 + 2j
dict_result = dict([(num_complex, 'complex')])
print(dict_result) # {(1+2j): 'complex'}
print(type(dict_result)) # <class 'dict'>

# 字符串类型转换为字典类型
str_value = "Hello"
dict_result = dict([(str_value, 'string')])
print(dict_result) # {'Hello': 'string'}
print(type(dict_result)) # <class 'dict'>

# 列表类型转换为字典类型
my_list = [1, 2, 3]
dict_result = dict([(tuple(my_list), 'list')])
print(dict_result) # {(1, 2, 3): 'list'}
print(type(dict_result)) # <class 'dict'>

# 集合类型转换为字典类型
my_set = {4, 5, 6}
dict_result = dict([(frozenset(my_set), 'set')])
print(dict_result) # {frozenset({4, 5, 6}): 'set'}
print(type(dict_result)) # <class 'dict'>

# 元组类型转换为字典类型
my_tuple = (7, 8, 9)
dict_result = dict([(my_tuple, 'tuple')])
print(dict_result) # {(7, 8, 9): 'tuple'}
print(type(dict_result)) # <class 'dict'>

2、字典推导式

# 整数类型转换为字典类型
num_int = 42
dict_result = {num_int: num_int}
print(dict_result) # {42: 42}
print(type(dict_result)) # <class 'dict'>

# 浮点数类型转换为字典类型
num_float = 3.14
dict_result = {num_float: num_float}
print(dict_result) # {3.14: 3.14}
print(type(dict_result)) # <class 'dict'>

# 布尔类型转换为字典类型
bool_value = True
dict_result = {bool_value: bool_value}
print(dict_result) # {True: True}
print(type(dict_result)) # <class 'dict'>

# 复数类型转换为字典类型
num_complex = 2 + 3j
dict_result = {num_complex: num_complex}
print(dict_result) # {(2+3j): (2+3j)}
print(type(dict_result)) # <class 'dict'>

# 字符串类型转换为字典类型
str_value = "Hello"
dict_result = {char: char for char in str_value}
print(dict_result) # {'H': 'H', 'e': 'e', 'l': 'l', 'o': 'o'}
print(type(dict_result)) # <class 'dict'>

# 列表类型转换为字典类型
my_list = [1, 2, 3]
dict_result = {index: value for index, value in enumerate(my_list)}
print(dict_result) # {0: 1, 1: 2, 2: 3}
print(type(dict_result)) # <class 'dict'>

# 集合类型转换为字典类型
my_set = {4, 5, 6}
dict_result = {value: value for value in my_set}
print(dict_result) # {4: 4, 5: 5, 6: 6}
print(type(dict_result)) # <class 'dict'>

# 元组类型转换为字典类型
my_tuple = (7, 8, 9)
dict_result = {value: value for value in my_tuple}
print(dict_result) # {7: 7, 8: 8, 9: 9}
print(type(dict_result)) # <class 'dict'>

六、转换为集合类型

1、set

# 整数类型转换为集合类型
num_int = 42
set_result = set([num_int])
print(set_result) # {42}
print(type(set_result)) # <class 'set'>

# 浮点数类型转换为集合类型
num_float = 3.14
set_result = set([num_float])
print(set_result) # {3.14}
print(type(set_result)) # <class 'set'>

# 布尔类型转换为集合类型
bool_value = True
set_result = set([bool_value])
print(set_result) # {True}
print(type(set_result)) # <class 'set'>

# 复数类型转换为集合类型
num_complex = 2 + 3j
set_result = set([num_complex])
print(set_result) # {(2+3j)}
print(type(set_result)) # <class 'set'>

# 字符串类型转换为集合类型
str_value = "Hello"
set_result = set(str_value)
print(set_result) # {'o', 'H', 'l', 'e'}
print(type(set_result)) # <class 'set'>

# 列表类型转换为集合类型
my_list = [1, 2, 3]
set_result = set(my_list)
print(set_result) # {1, 2, 3}
print(type(set_result)) # <class 'set'>

# 字典类型转换为集合类型
my_dict = {"key": "value"}
set_result = set(my_dict)
print(set_result) # {'key'}
print(type(set_result)) # <class 'set'>

# 元组类型转换为集合类型
my_tuple = (4, 5, 6)
set_result = set(my_tuple)
print(set_result) # {4, 5, 6}
print(type(set_result)) # <class 'set'>

2、集合推导式

# 整数类型转换为集合类型
num_int = 42
set_result = {num for num in [num_int]}
print(set_result) # {42}
print(type(set_result)) # <class 'set'>

# 浮点数类型转换为集合类型
num_float = 3.14
set_result = {num for num in [num_float]}
print(set_result) # {3.14}
print(type(set_result)) # <class 'set'>

# 布尔类型转换为集合类型
bool_value = True
set_result = {bool_val for bool_val in [bool_value]}
print(set_result) # {True}
print(type(set_result)) # <class 'set'>

# 复数类型转换为集合类型
num_complex = 2 + 3j
set_result = {num for num in [num_complex]}
print(set_result) # {(2+3j)}
print(type(set_result)) # <class 'set'>

# 字符串类型转换为集合类型
str_value = "Hello"
set_result = {char for char in str_value}
print(set_result) # {'o', 'H', 'l', 'e'}
print(type(set_result)) # <class 'set'>

# 列表类型转换为集合类型
my_list = [1, 2, 3]
set_result = {num for num in my_list}
print(set_result) # {1, 2, 3}
print(type(set_result)) # <class 'set'>

# 字典类型转换为集合类型
my_dict = {"key": "value"}
set_result = {key for key in my_dict}
print(set_result) # {'key'}
print(type(set_result)) # <class 'set'>

# 元组类型转换为集合类型
my_tuple = (4, 5, 6)
set_result = {num for num in my_tuple}
print(set_result) # {4, 5, 6}
print(type(set_result)) # <class 'set'>

3、{}

# 整数类型转换为集合类型
num_int = 42
set_result = {num_int}
print(set_result) # {42}
print(type(set_result)) # <class 'set'>

# 浮点数类型转换为集合类型
num_float = 3.14
set_result = {num_float}
print(set_result) # {3.14}
print(type(set_result)) # <class 'set'>

# 布尔类型转换为集合类型
bool_value = True
set_result = {bool_value}
print(set_result) # {True}
print(type(set_result)) # <class 'set'>

# 复数类型转换为集合类型
num_complex = 2 + 3j
set_result = {num_complex}
print(set_result) # {(2+3j)}
print(type(set_result)) # <class 'set'>

七、总结

1、字符串类型(str)

  • 是由字符组成的不可变的序列。
  • 支持索引和切片操作,可以通过索引访问单个字符或使用切片获取子字符串。
  • 是不可变的,无法直接修改字符串中的字符,但可以通过字符串方法和操作创建新的字符串。

2、数字类型(int,float,complex)

  • 用于表示数值,包括整数(int)、浮点数(float)和复数(complex)。
  • 整数类型(int)用于表示整数值,浮点数类型(float)用于表示带有小数部分的数值,复数类型(complex)用于表示具有实部和虚部的数值。
  • 支持基本的数学运算,如加法、减法、乘法和除法等。
  • 可以进行类型转换,如将整数转换为浮点数或浮点数转换为整数。

3、列表类型(list)

  • 可以存储多个元素,元素之间有序且可重复。
  • 可以通过索引访问和修改元素。
  • 支持切片操作。
  • 使用方括号 [] 定义,元素之间用逗号分隔。
  • 可以进行修改、添加和删除元素。
  • 适用于有序的数据集合,需要频繁地进行添加、删除和修改操作。

4、元组类型(tuple)

  • 可以存储多个元素,元素之间有序且可重复。
  • 可以通过索引访问元素。
  • 支持切片操作。
  • 使用圆括号 () 或不使用括号定义,元素之间用逗号分隔。
  • 不可修改,是不可变的。
  • 适用于不需要修改元素的数据集合,可以用作不可变的常量集合。

5、字典类型(dict)

  • 存储键值对(key-value)数据,其中键唯一且不可重复。
  • 键和值之间没有顺序关系。
  • 使用花括号 {} 定义,键值对使用冒号 : 分隔,键值对之间用逗号分隔。
  • 可以通过键来访问和修改值,但不支持索引访问。
  • 可以动态地添加、修改和删除键值对。
  • 适用于键值对的数据集合,通过键快速访问对应的值。

6、集合类型(set)

  • 存储多个元素,元素之间无序且不可重复。
  • 支持集合间的常见操作,如交集、并集、差集等。
  • 使用花括号 {} 或 set() 函数定义,元素之间用逗号分隔。
  • 不支持索引访问和切片操作。
  • 可以动态地添加和删除元素。
  • 适用于唯一性元素的数据集合,可以进行集合运算和去重操作。

字符串类型是不可变的,用于处理文本数据。
数字类型用于表示数值,包括整数、浮点数和复数。
列表类型是可变的序列,可以包含不同类型的元素。
元组类型是不可变的序列,具有固定的长度。
字典类型是无序的键-值对集合,通过键来访问、添加、修改和删除对应的值。
集合类型是无序的、不重复的元素的集合,支持集合运算。

记录学习过程,欢迎讨论交流,尊重原创,转载请注明出处~

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
青葱年少的头像青葱年少普通用户
上一篇 2023年10月18日
下一篇 2023年10月18日

相关推荐