Python中元组的使用及其示例代码,与列表类似但是更加的深入

#Filename:zoo.py
#元组:元组用来将多样的对象集合到一起。元组和列表十分
#的相似,只不过元组和字符串一样是不可变的,即你不能修改
#元组,组通过圆括号中用逗号分割项目
#例子,代码十分的简单
zoo=('monkey','elephant','snake')
print('Number of animals in the zoo is:',len(zoo))
new_zoo=('pig','panda',zoo)
print('Number of cages in the new zoo is:',len(new_zoo))
print('All animals in new zoo are:',new_zoo)
print('Animals brought from old zoo is:',new_zoo[2])
print('Last animal brought from old zoo is:',new_zoo[2][2])
print('Number of animals in the new zoo is:',len(new_zoo)-1+len(new_zoo[2]))

结果:

Number of animals in the zoo is: 3
Number of cages in the new zoo is: 3
All animals in new zoo are: ('pig', 'panda', ('monkey', 'elephant', 'snake'))
Animals brought from old zoo is: ('monkey', 'elephant', 'snake')
Last animal brought from old zoo is: snake
Number of animals in the new zoo is: 5

猜你喜欢

转载自blog.csdn.net/qq_41901915/article/details/82380433
今日推荐