python练手小题(二)

为元组的每个元素命名,提高程序的可读性

student = ('Tom',16,'man','[email protected]')

# F1  类似于C里面定义常量,用常量代替索引
NAME,AGE,SEX,EMAIL = range(4)
print(student[NAME])

#F2  利用collections类
from collections import namedtuple

Student = namedtuple('Student',['name','age','sex','email']) 
s=Student('Tom2',16,'man','[email protected]')
print(s)
print(isinstance(s,tuple))

猜你喜欢

转载自blog.csdn.net/qq_37622608/article/details/81435734