案例:准备10个人姓名,然后为这10个人随机生成生日【都是90后】

1、统计出那些人是夏季【6月-8月】出生的。

2、最大的比最小的大多少天

3、谁的生日最早,谁的生日最晚

备注:春季【3-5】夏季【6-8】秋季【9-11】冬季【12-2】

演示:

from datetime import date, timedelta

from random import randint

def build_birthday(list_person_name:list):

初始化存储姓名:生日字典

    name_birthday ={}.fromkeys(list_person_name)

生成生日

    for key in name_birthday:

temp_year = randint(1990, 1999)

temp_month = randint(1, 12)

temp_day = randint(1, 30)

name_birthday[key] = date(temp_year, temp_month, temp_day)

返回

    return name_birthday

def person_birthday_summer(name_birthday:dict):

用于存储夏天出生的key

    list_person = []

for key in name_birthday:

if name_birthday[key].month >= 6 and name_birthday[key].month <= 8:

list_person.append(key)

返回

    return list_person

def get_person_year_max(name_birthday:dict):

在字典中提取出生日

    person_birth = list(name_birthday.values())

获取最大的生日

    max_birthday = sorted(person_birth)[len(person_birth)-1]

遍历

    for key in name_birthday:

if name_birthday[key] == max_birthday:

return key

def get_person_year_min(name_birthday:dict):

在字典中提取出生日

    person_birth = list(name_birthday.values())

获取最小的生日

    min_birthday = sorted(person_birth)[0]

遍历

    for key in name_birthday:

if name_birthday[key] == min_birthday:

return key

def get_person_days(name_birthday:dict):

在字典中提取出生日

    person_birth = list(name_birthday.values())

获取最大的生日

    min_birthday = sorted(person_birth)[0]

max_birthday = sorted(person_birth)[len(person_birth)- 1]

返回天数

    return (max_birthday-min_birthday).days

def get_person_early_birthday(name_birthday:dict):

for key in name_birthday:

name_birthday[key] = name_birthday[key].replace(year=1990)

person_birth = list(name_birthday.values())

return(sorted(person_birth)[0])

def get_person_later_birthday(name_birthday:dict):

for key in name_birthday:

name_birthday[key] = name_birthday[key].replace(year=1990)

person_birth = list(name_birthday.values())

return(sorted(person_birth)[len(person_birth)-1])

if __name__ == "__main__":

list_name = ["赵一""杨二""张三""李四""王五""赵六""马七""郑八""刘九","胡十"]

list_name中所有的学员生成生日

    name_birthday = build_birthday(list_name)

print(name_birthday)

调用功能模块

    birthday_summer_list =person_birthday_summer(name_birthday)

if len(birthday_summer_list) == 0:

print("没有人的生日是在夏天: ")

else:

print("生日为夏天的有:", birthday_summer_list)  需求一

    需求二

    print("生日最大的:", get_person_year_max(name_birthday))

print("生日最小的:", get_person_year_min(name_birthday))

print("最大比最小的天数:", get_person_days(name_birthday))

需求三

    date_early =get_person_early_birthday(name_birthday)

print("生日最大的是:%d%d"%(date_early.month,date_early.day))

date_later = get_person_later_birthday(name_birthday)

print("生日最小的是:%d%d% (date_later.month, date_early.day))

执行结果:

C:\python\python.exeC:/python/demo/file3.py

{'赵一':datetime.date(1992, 12, 30), '杨二': datetime.date(1995,6, 23), '张三': datetime.date(1990, 6, 21), '李四':datetime.date(1991, 9, 29), '王五':datetime.date(1996, 2, 26), '赵六':datetime.date(1995, 9, 18), '马七':datetime.date(1996, 7, 4), '郑八':datetime.date(1990, 3, 5), '刘九':datetime.date(1992, 3, 3), '胡十':datetime.date(1992, 11, 6)}

生日为夏天的有: ['杨二','张三','马七']

生日最大的: 马七

生日最小的: 郑八

最大比最小的天数: 2313

生日最大的是:2月26日

生日最小的是:12月26日

Process finished with exit code 0

Python中日期时间案例演示的更多相关文章

  1. [ Python入门教程 ] Python中日期时间datetime模块使用实例

    Python中datetime模块提供强大易用的日期处理功能,用于记录程序操作或修改时间.时间计算.日志时间显示等功能.datatime模块重新封装了time模块,提供的类包括date.time.da ...

  2. Python中日期和时间格式化输出的方法

    本文转自:https://www.jb51.net/article/62518.htm 本文实例总结了python中日期和时间格式化输出的方法.分享给大家供大家参考.具体分析如下: python格式化 ...

  3. Python中对时间日期的处理方法简单汇总

    这篇文章主要介绍了Python实用日期时间处理方法汇总,本文讲解了获取当前datetime.获取当天date.获取明天/前N天.获取当天开始和结束时间(00:00:00 23:59:59).获取两个d ...

  4. Python学习---日期时间

    在Python里面日期时间的功能主要由几个模块提供:time,calendar,datetime,date等 time主要用到的功能函数: #!/usr/bin/python3 # coding:ut ...

  5. Python实用日期时间处理方法汇总

    这篇文章主要介绍了Python实用日期时间处理方法汇总,本文讲解了获取当前datetime.获取当天date.获取明天/前N天.获取当天开始和结束时间(00:00:00 23:59:59).获取两个d ...

  6. python中的时间和时间格式转换

    1.python中的时间:要得到年月日时分秒的时间: import time #time.struct_time(tm_year=2012, tm_mon=9, tm_mday=15, tm_hour ...

  7. [转]JDBC中日期时间的处理技巧

    Java中用类java.util.Date对日期/时间做了封装,此类提供了对年.月.日.时.分.秒.毫秒以及时区的控制方法,同时也提供一些工具方法,比如日期/时间的比较,前后判断等. java.uti ...

  8. Python 中的时间处理包datetime和arrow

    Python 中的时间处理包datetime和arrow 在获取贝壳分的时候用到了时间处理函数,想要获取上个月时间包括年.月.日等 # 方法一: today = datetime.date.today ...

  9. 【Java 与数据库】JDBC中日期时间的处理技巧

    JDBC中日期时间的处理技巧 详谈Java.util.Date和Java.sql.Date 基础知识 Java中用类java.util.Date对日期/时间做了封装,此类提供了对年.月.日.时.分.秒 ...

随机推荐

  1. Deep learning with Python 学习笔记(6)

    本节介绍循环神经网络及其优化 循环神经网络(RNN,recurrent neural network)处理序列的方式是,遍历所有序列元素,并保存一个状态(state),其中包含与已查看内容相关的信息. ...

  2. Vue下拉刷新组件

    Examples examples Installation npm install vue-pull-to --save Use Setup <template> <div> ...

  3. [转]Magento Configurable Product

    本文转自:https://docs.magento.com/m1/ce/user_guide/catalog/product-configurable.html A configurable prod ...

  4. Tunnel Warfare(hdu1540 线段树)

    Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  5. 常见Java问题

    1.泛型的好处 保护了类型安全 避免了强制类型转化 2.final关键字的作用 final修饰的属性是常量 final修饰的方法不可被重写 final修饰的类不能被继承,如:String 3.静态变量 ...

  6. ajax jsonp的跨域请求

    1.页面ajax的请求 $.ajax({ async: false, url: 'http://localhost:8080/downloadVideos',//跨域的dns/document!sea ...

  7. HTTPS的安全性

    一.Https介绍 1. 什么是Https HTTPS(全称:Hypertext Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道, ...

  8. longing加载中实例

    利用图片播放 <div class="wrap" id="wrap" style="position: inherit; height: 604 ...

  9. CSS让DIV按照背景图片的比例缩放,并让背景图片填充整个元素(转)

    目的是:通过background的一系列属性,让DIV按照背景图片的比例缩放,并让背景图片填充整个DIV 首先我们需要让背景图片在指定的DIV中全部填充显示 之前看有用类似 background-at ...

  10. Nodejs 使用特定版本的SSL/TLS协议版本

    var options = { key: fs.readFileSync('./bin/privatekey.pem'), cert: fs.readFileSync('./bin/certifica ...