Quantcast

how many days in one year ?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

how many days in one year ?

contro opinion
i want to know how many days in one year,
import time
import datetime
d1= datetime.datetime(2003, 1, 1)
d2=datetime.datetime(2003, 21, 31)
print  (d2-d1).days+1

i can get there are 365 days in the 2003,

is there other way,better way  to calculate  ?

--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: how many days in one year ?

Pavel Anossov
import calendar

print 366 if calendar.isleap(2003) else 365



On 22 April 2012 13:37, contro opinion <[hidden email]> wrote:

> i want to know how many days in one year,
> import time
> import datetime
> d1= datetime.datetime(2003, 1, 1)
> d2=datetime.datetime(2003, 21, 31)
> print  (d2-d1).days+1
>
> i can get there are 365 days in the 2003,
>
> is there other way,better way  to calculate  ?
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



--
С уважением, Аносов Павел
--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: how many days in one year ?

Dan Sommers
In reply to this post by contro opinion
On Sun, 22 Apr 2012 17:37:42 +0800
contro opinion <[hidden email]> wrote:

> i want to know how many days in one year,
> import time
> import datetime
> d1= datetime.datetime(2003, 1, 1)
> d2=datetime.datetime(2003, 21, 31)

ITYM datetime.datetime(2003, 12, 31).

> print  (d2-d1).days+1
>
> i can get there are 365 days in the 2003,
>
> is there other way,better way  to calculate  ?

I think that this way is less prone to typos like the one above:

import datetime
year = 2003
d1 = datetime.date(year, 1, 1)
d2 = datetime.date(year + 1, 1, 1)
print (d2 - d1).days

Dan
--
http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: how many days in one year ?

Temia Eszteri
In reply to this post by Pavel Anossov
Better yet, write them out as constants if they're referenced more
than once. In that case, if the planet gets knocked into a new orbital
and rotational pattern, you can update accordingly if you,
civilization, and Python all still exist.

~Temia

On Sun, 22 Apr 2012 13:44:15 +0400, you wrote:

>import calendar
>
>print 366 if calendar.isleap(2003) else 365
>
>
>
>On 22 April 2012 13:37, contro opinion <[hidden email]> wrote:
>> i want to know how many days in one year,
>> import time
>> import datetime
>> d1= datetime.datetime(2003, 1, 1)
>> d2=datetime.datetime(2003, 21, 31)
>> print? (d2-d1).days+1
>>
>> i can get there are 365 days in the 2003,
>>
>> is there other way,better way? to calculate? ?
--
When on earth, do as the earthlings do.
--
http://mail.python.org/mailman/listinfo/python-list
Loading...