Python Date and Time Case demonstration, python case

Source: Internet
Author: User

Python Date and Time Case demonstration, python case

Case: Prepare 10 people's names, and then generate a random birthday for these 10 people [after 90]

1. We can find out who were born in the summer from June to August.

2. Maximum number of days longer than minimum

3. Who has the earliest birthday and who has the latest birthday?

Remarks: spring [3-5] summer [6-8] Autumn [9-11] Winter [12-2]

 

Demo:

FromDatetimeImportDate, timedelta

FromRandomImportRandint

 

 

DefBuild_birthday (list_person_name: list ):

#Initialize Storage"Name: Birthday"Dictionary

Name_birthday ={}. fromkeys (list_person_name)

#Generate birthday

ForKeyInName_birthday:

Temp_year = randint (1990,199 9)

Temp_month = randint (1, 12)

Temp_day = randint (1, 30)

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

#Return

ReturnName_birthday

 

DefPerson_birthday_summer (name_birthday: dict ):

#Used to store summer-bornKey

List_person = []

ForKeyInName_birthday:

IfName_birthday [key]. month> = 6AndName_birthday [key]. month <= 8:

List_person.append (key)

#Return

ReturnList_person

 

 

DefGet_person_year_max (name_birthday: dict ):

#Extract birthdate in the dictionary

Person_birth = list (name_birthday.values ())

#Get the maximum birthday

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

#Traversal

ForKeyInName_birthday:

IfName_birthday [key] = max_birthday:

ReturnKey

 

 

DefGet_person_year_min (name_birthday: dict ):

#Extract birthdate in the dictionary

Person_birth = list (name_birthday.values ())

#Get the minimum birthday

Min_birthday = sorted (person_birth) [0]

#Traversal

ForKeyInName_birthday:

IfName_birthday [key] = min_birthday:

ReturnKey

 

DefGet_person_days (name_birthday: dict ):

#Extract birthdate in the dictionary

Person_birth = list (name_birthday.values ())

#Get the maximum birthday

Min_birthday = sorted (person_birth) [0]

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

#Return days

ReturnMax_birthday-min_birthday. days

 

DefGet_person_early_birthday (name_birthday: dict ):

ForKeyInName_birthday:

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

 

Person_birth = list (name_birthday.values ())

Return(Sorted (person_birth) [0])

 

DefGet_person_later_birthday (name_birthday: dict ):

ForKeyInName_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 = ["Zhao Yi","Yang 'er","Zhang San","Li Si","Wang Wu","Zhao Liu","Ma Qi","Zheng Ba","Liu JIU","Hu Shi"]

#IsList_nameGenerate birthday for all students

Name_birthday = build_birthday (list_name)

Print (name_birthday)

#Call function module

Birthday_summer_list = person_birthday_summer (name_birthday)

IfLen (birthday_summer_list) = 0:

Print ("Nobody's birthday is in summer:")

Else:

Print ("There are:", Birthday_summer_list)#Requirement 1

#Requirement 2

Print ("The biggest birthday:", Get_person_year_max (name_birthday ))

Print ("Minimum birthday:", Get_person_year_min (name_birthday ))

Print ("Maximum ratio of days:", Get_person_days (name_birthday ))

#Requirement 3

Date_early = get_person_early_birthday (name_birthday)

Print ("The biggest birthday is:% DMonth% DDay"% (Date_early.month, date_early.day ))

 

Date_later = get_person_later_birthday (name_birthday)

Print ("The minimum birthday is:% DMonth% DDay"% (Date_later.month, date_early.day ))

 

Execution result:

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

{'Zhao yi': datetime. date (1992, 12, 30), 'yang 2': datetime. date (, 23), 'zhang san': datetime. date (1990, 6, 21), 'Li si': datetime. date (1991, 9, 29), 'wang 5': datetime. date (1996, 2, 26), 'zhao liu': datetime. date (1995, 9, 18), 'ma 7': datetime. date (1996, 7, 4), 'zheng Ba ': datetime. date (1990, 3, 5), 'Liu jiu': datetime. date (1992, 3, 3), 'hu 10': datetime. date (1992, 11, 6 )}

For the summer, ['yang 2', 'zhang san', 'ma 7']

The biggest birthday: Ma Qi

The youngest birthday: Zheng Ba

Maximum ratio of the minimum days: 2313

The biggest birthday is: February 26

The minimum birthday is: January 1, December 26

 

Process finished with exit code 0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.