vegetables = {'cabbage', 'potato', 'tomato', 'cucumber'}
fruits = {'apple', 'orange', 'banana', 'tomato', 'cucumber'}

for fruit in fruits:
    print(fruit)

输出结果:

apple
orange
banana
tomato
cucumber

fruits.add('peach')

现在fruits集合中包含 'peach' 元素。

集合操作①定义集合。vegetables = cabbage potato tomato cucumberfruits = apple orange banana tomato cucumber②遍历集合。输出fruits所有元素。③添加集合元素。添加 peach到fruits。python编写

原文地址: https://www.cveoy.top/t/topic/fK3h 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录