是的,Python 的 print()
函數(shù)可以打印復(fù)雜數(shù)據(jù)結(jié)構(gòu),例如列表、元組、字典和集合等。對(duì)于嵌套的數(shù)據(jù)結(jié)構(gòu),print()
函數(shù)會(huì)遞歸地打印其內(nèi)容。
下面是一些示例:
my_list = [1, 2, 3, "hello", [4, 5]]
print(my_list)
輸出:
[1, 2, 3, 'hello', [4, 5]]
my_tuple = (1, 2, 3, "hello", (4, 5))
print(my_tuple)
輸出:
(1, 2, 3, 'hello', (4, 5))
my_dict = {"key1": 1, "key2": 2, "key3": [3, 4], "key4": {"inner_key": 5}}
print(my_dict)
輸出:
{'key1': 1, 'key2': 2, 'key3': [3, 4], 'key4': {'inner_key': 5}}
my_set = {1, 2, 3, "hello", (4, 5)}
print(my_set)
輸出:
{1, 2, 3, 'hello', (4, 5)}
請(qǐng)注意,集合是無(wú)序的,因此打印出來(lái)的元素順序可能與定義時(shí)的順序不同。