Python---索引

Python索引类似于C/C++,JAVA中的数组,下标从0开始。和C++,java中的数组不同的是,Python中可以使用负数索引,当使用负数索引时,Python会从最后一个元素开始计数。如:str = 'hello',str[-1]就为'o'。下面来个示例展示一下:示例要求是根据输入的年月日输出相应的日期。

# coding=utf-8
#  根据给定的年月日以数字形式打印出日期
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
          'August', 'September', 'October', 'November', 'December']
# 17*['th']是给endings添加17个"th"字符串
endings = ['st', 'nd', 'rd'] + 17*['th']\
        + ['st', 'nd', 'rd'] + 7*['th']\
        + ['st']
year = raw_input('year:')
month = raw_input('month:')
day = raw_input('day:')
month_number = int(month)
day_number = int(day)
month_name = months[month_number-1]
ordinal = day + endings[day_number-1]
print month_name + " " + ordinal + " " + year + "\n"





你可能感兴趣的:(python)