0

I tried to make a datetime object from 6 am to midnight by 30 minutes. And I wrote the fllowing code.

from datetime import datetime, timedelta
dt = datetime(2017,8,14,6,0,0)
lst = []
while dt.hour != 00 or dt.minute != 00:
    lst.append(dt)
    dt = dt + timedelta(minutes=30)

It gave me the result I want but I noticed I accidentally put 00 instead of 0. It seems like 00 is converted to 0 in Python. I checked the type by using type(). As a result, type(00) and type(0) are both int.

Then is there any difference between both? If no difference, why Python allow this format?

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
steve
  • 127
  • 12
  • I didn't know that! Then is there any difference? Which one should I use? – steve Aug 14 '17 at 10:49
  • Possible duplicate of https://stackoverflow.com/questions/13431324/how-does-python-interpret-numbers-with-leading-zeroes As can be seen in the duplicate, literals starting with `0` are interpreted as base-8 (octal). In Python 3 this is invalid syntax and one must use `0o` prefix to denote base-8 (much like `0x` denotes base-16, and `0b` denotes base-2). – DeepSpace Aug 14 '17 at 10:53
  • OK! Now it's clear. Thanks alot! – steve Aug 14 '17 at 12:36

0 Answers0