Python Learning-Exercises (4)

Source: Internet
Author: User

"" "Title: Enter a certain day of the year, judging the day is the first of the year?     "" "Import datetimeimport timefrom functools import reducedef calculate1 (t):" "Calculates:p Aram T directly using the Python datetime module:    : Return: "" "Print (" Calculate One ", end=": ") Print (T.strftime ("%j ")) def calculate2 (t):" "" Manually Calculate:p Aram T: : Return: "" "Print (" Calculate Two ", end=": ") days = [31, +, +, +, +, +, +] Daysleap         (+)--------------------------------------year = t.year if year% = = 0 or (year% 4 = = 0 and year% 100! = 0): Print (sum (daysleap[:t.month-1]) + t.day) else:print (sum (days[:t.month-1]) + t.day) def calculate3 (t) : "" "Master simplified Calculate2:p Aram T:: Return:" "" Print ("Calculate three", end= ":") days = [31, 28, 31, 30, 31, 30, 3    1, +, +, (+) [] year = t.year if year% + = = 0 or (year% 4 = = 0 and year% = 0): days[1] = 29 Print (sum (days[0:t.month-1]) + t.day) def calculate4 (t): "" "uses the dictionary to calculate the:p Aram T:: Return:" "" PRInt ("Computed four", end= ":") daydict = {0:0, 1:31, 2:59, 3:90, 4:120, 5:151, 6:181, 7:212, 8:243, 9:273, 10:304, 11: 334, 12:365} year = T.year D = daydict[t.month-1] + t.day if year% (= = 0) or (year% 4 = = 0 and year% 100     ! = 0): D + = 1 print (d) def calculate5 (t): "" The time module is used to calculate, note and the DateTime module is differentiated:p Aram T:: Return: "" "     Print ("Calculate Five", end= ":") T = Time.strptime (T.strftime ("%y-%m-%d"), "%y-%m-%d") print (T[7]) def calculate6 (t): "" " Calculates the:p Aram T:: Return: "" "Print (" Calculation Six ", end=": ") T1 = Datetime.date (t.year, 1, 1) with datetime subtraction T2 = T-T1 Print (t2.days + 1) def calculate7 (t): "" "is calculated using time subtraction, attention is distinguished from datetime, it cannot be reduced directly, it needs to be converted to a timestamp to reduce the timestamp to 1970 is calculated for a base point, so the method can only calculate the time after 1970 (excluding 1970):p Aram T:: Return: "" "Print (" Calculated seven ", end=": ") T1 = Time.strptime (t.strft IME ("%y-01-01"), "%y-%m-%d") T1 = time.mktime (t1) t = time.strptime (T.strftime ("%y-%m-%d"), "%y-%m-%d") T = time. Mktime (t) t2 = T-T1    t2 = t2//(3600 *) print (int (t2) + 1) def calculate8 (t): "" is calculated using the reduce function, intermediate is useful to ternary operator in Python 3, reduce () function has been removed from the global namespace, it is now placed in the Fucntools module with the first introduction of the From Functools import reduce:p Aram T:: Return: "" "" "" "Print (" Calculation of Eight ", end=": ") year = t.year days = [0, 4 if year%, or if year% of the if year% else 29, 31, (+, +, +, +, +) (+) print (reduce (lambda A, b:a + B, days[0:t.month]) + t.day) def calculate 9 (t): "" "computes a leap year using bitwise operations: The parse year&3 is equivalent to year%4: because the binary-to-decimal is: 2**0+2**1+2**2+ ...  , visible 2**2 can be divisible by 4 year&15 equivalence year%16 According to the calculation rules of leap years we can know: The year cannot be divisible by 4 is certainly not a leap, but can be divisible by 4 and can be 25 integers but not 16 integers is not a leap, the rest is a leap year can be Year%4 or YEAR%16 and! Year%25 these are not leap years, and vice versa! (year%4 or year%16 and!) YEAR%25) converts a bitwise operation for a leap year! (Year&3 or year&15 and!)  (year%25)):p Aram T:: Return: "" "Print (" Calculate Nine ", end=": ") days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, Year = T.year if Not (year & 3 or Year & 15 (year)): days[1] = print (sum (days[0:t.month-1) + t.day) def answer (): "" "to determine the date entered by a try Whether it is correct: return: "" "Year = input (" Enter years: ")" if "= =" Q ": Return month = input (" Enter month: ") Day = input ("        Input days: ") try:t = datetime.date (int (year), Int. (month), Int (day)) calculate1 (t) calculate2 (t)        Calculate3 (t) calculate4 (t) calculate5 (t) calculate6 (t) calculate7 (t) calculate8 (t) Calculate9 (t) except Valueerror:print ("entered date error") Print ("Continue, or input Q launch") answer () answer ()

  

Python Learning-Exercises (4)

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.