python的列表(list)、元组(tuple)、字典(dict)

列表(list)、元组(tuple)、字典(dict)是python中内置的三种数据结构,也常常会在程序处理中用到,此处做个记录。

列表(list)
1)一组有序项目的集合。可变的数据类型【可进行增删改查
2)列表是以方括号“[]”包围的数据集合,不同成员以“,”分隔。
3)列表中可以包含任何数据类型,也可包含另一个列表
4)列表可通过序号访问其中成员
常用列表操作:
1)list.append() 追加成员,成员数据
2)list.pop() 删除成员,删除第i个成员
3)list.count(x) 计算列表中参数x出现的次数
4)list.remove() 删除列表中的成员,直接删除成员i
5)list.extend(L) 向列表中追加另一个列表L
6)list.reverse() 将列表中成员的顺序颠倒
7)list.index(x) 获得参数x在列表中的位置
8)list.sort() 将列表中的成员排序
9)list.insert() 向列表中插入数据insert(a,b)向列表中插入数据

元组(tuple)
1)不可变序列
2)元组是以圆括号“()”包围的数据集合,不同成员以“,”分隔
3)与列表不同:元组中数据一旦确立就不能改变
4)通过下标进行访问

字典(dict)
1)键值对的集合(map)
2)字典是以大括号“{}”包围的数据集合
3)与列表区别:字典是无序的,在字典中通过键来访问成员。
4)字典是可变的,可以包含任何其他类型

声明:
M={k1:v1,k2:v2},访问 M[k1]将得到v1

常用字典操作:
1) dic.clear() 清空字典
2) dic.keys() 获得键的列表
3) dic.values() 获得值的列表
4) dic.copy() 复制字典
5) dic.pop(k) 删除键k
6) dic.get(k) 获得键k的值
7) dic.update() 更新成员,若成员不存在,相当于加入
8) dic.items() 获得由键和值组成的列表
9) dic.popitem() 随机去除dic中键值对,并且返回这个键值对的值
10) dic.setdefault(‘name’,’new’) 如果dic中有name这个键,那么则返回dic中name对应的值,如果没有则返回new
11) dic.update(another_dic) 用一个字典去更形另外一个字典

输入三个整数x,y,z,请把这三个数由小到大输出

# -*- coding: utf-8 -*-

#输入三个整数x,y,z,请把这三个数由小到大输出。

def inputNumber():
    while True:
        try:
            x = input('input number please:')
            #if isinstance(x,(int,float)):
            break
        except NameError:
            print 'Error,input number please:'
    return x

def comNumber(x,y,z):
    if xy:
            print 'from small to big:',y,z,x
        else:
            print 'from small to big:',y,x,z

x = inputNumber()
y = inputNumber()
z = inputNumber()
print 'your input number is:',x,y,z

comNumber(x,y,z)

1、第一次了解了try,except在程序中的应用,实际上一开始没有把要判断异常的语句放在try模块中,导致总是无法执行出预期的结果。
2、comNumber部分的函数应该是凭自己感觉敲的代码,感觉是很挫,没有查资料。等这一轮基础过后再回头看有什么更好的实现方法。

输入某年某月某日,判断这一天是这一年的第几天

# -*- coding: utf-8 -*-

#输入某年某月某日,判断这一天是这一年的第几天?

import time,datetime

def input_date():

    leapYear = False

    while True:
        year = raw_input('input year(yyyy):')
        if len(year) == 4 and year.isdigit() and year > 0:
            if (int(year) % 400 == 0) or (int(year) % 100 != 0 and int(year) % 4 == 0):
                leapYear = True
                print 'It is leap year'
            else:
                print 'It is not leap year'
            print "your input year is:",year
            break
        else:
            print 'your input is error,input year(yyyy) please!'
    #4位,数字,大于o

    while True:
        month = input('input month(mm):')
        if 0<month<13:
            print "your input month is:",month
            break
        else:
            print 'your input is error,input year(0<mm<13)again please!'

    while True:
        date = input('input date(dd):')
        if month in [1,3,5,7,8,10,12]:
            if 1 <= date <= 31:
                bigMonth = True
                #print ('your input dates is:%s,%s,%s' % (year, month, date))
                break
            else:
                print 'your input is error,input date(1<=dd<=31) again please!'
        elif month in [4,6,9,11]:
            if 1<= date <= 30:
                #print ('your input dates is:%s,%s,%s' % (year, month, date))
                break
            else:
                print 'your input is error,input date(1<=dd<=30) again please!'
        elif month == 2:
            if leapYear:
                if 1 <= date <= 29:
                    #print ('your input dates is:%s,%s,%s' % (year, month, date))
                    break
                else:
                    print 'your input is error,input date(1<=dd<=29) again please!'
            else:
                if 1 <= date <= 28:
                    #print ('your input dates is:%s,%s,%s' % (year,month,date))
                    break
                else:
                    print 'your input is error,input date(1<=dd<=28) again please!'

    print ('your input dates is:%s/%s/%s' % (year, month, date))

    if month == 1:
        numDay = date
    elif month == 2:
        numDay = 31 + date
    elif month == 3:
        if leapYear:
            numDay = 31 + 29 + date
        else:
            numDay = 31 + 28 + date
    elif month == 4:
        if leapYear:
            numDay = 31 + 29 + 31 + date
        else:
            numDay = 31 + 28 + 31 + date
    elif month == 5:
        if leapYear:
            numDay = 31 + 29 + 31 + 30 + date
        else:
            numDay = 31 + 28 + 31 + 30 + date
    elif month == 6:
        if leapYear:
            numDay = 31 + 29 + 31 + 30 + 31 + date
        else:
            numDay = 31 + 28 + 31 + 30 + 31 + date
    elif month == 7:
        if leapYear:
            numDay = 31 + 29 + 31 + 30 + 31 + 30 + date
        else:
            numDay = 31 + 28 + 31 + 30 + 31 + 30 + date
    elif month == 8:
        if leapYear:
            numDay = 31 + 29 + 31 + 30 + 31 + 30 + 31 + date
        else:
            numDay = 31 + 28 + 31 + 30 + 31 + 30 + 31 + date
    elif month == 9:
        if leapYear:
            numDay = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + date
        else:
            numDay = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + date
    elif month == 10:
        if leapYear:
            numDay = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + date
        else:
            numDay = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + date
    elif month == 11:
        if leapYear:
            numDay = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + date
        else:
            numDay = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + date
    elif month == 12:
        if leapYear:
            numDay = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + date
        else:
            numDay = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + date

    print 'It is No.%s day' % numDay
#def calculate_date():

input_date()

主要考虑几点:
1、无论是年、月、日在输入过程中都判断是否合法,否则重新输入;
3、判断闰年;
2、在计算第几天的时候目前的实现应该还是很挫….