示例:
num = 10
str_num = str(num)
print(str_num) # 輸出: "10"
示例:
str_num = "10"
num = int(str_num)
print(num) # 輸出: 10
示例:
str_num = "10.5"
num = float(str_num)
print(num) # 輸出: 10.5
示例:
str_nums = "12345"
nums = list(str_nums)
print(nums) # 輸出: ['1', '2', '3', '4', '5']
示例:
key_value_pairs = [("a", 1), ("b", 2), ("c", 3)]
my_dict = dict(key_value_pairs)
print(my_dict) # 輸出: {'a': 1, 'b': 2, 'c': 3}
示例:
my_list = [1, 2, 3, 3, 4, 5]
my_set = set(my_list)
print(my_set) # 輸出: {1, 2, 3, 4, 5}
示例:
my_list = [1, 2, 3]
my_tuple = tuple(my_list)
print(my_tuple) # 輸出: (1, 2, 3)
示例:
num = 0
is_zero = bool(num)
print(is_zero) # 輸出: False
示例:
num = 10
print(type(num)) # 輸出: <class 'int'>
示例:
my_list = [1, 2, 3, 4, 5]
print(len(my_list)) # 輸出: 5
這些是Python常見的工廠函數用法示例,可以根據需要選擇適合的函數進行對象轉換或其他操作。