Date Time


#1

Why do we need to put from datetime import datetime ? It really bother me if i don’t know how it works.


#2

we need to import the datetime class from the datetime module.

For example, if we have two python scripts named script1.py and script2.py and script2 contains a function named example we could do the following in script1.py:

from script2 import example

example can also be a class. You will learn more about this later.

you can write your own functions and classes, and import. Besides the ones python ships with (like datetime)

the problem with programming, we can’t throw everything at once at you


#3

I’m really new at programming, and thanks for replying my question. Even tho i dont understand your explanation, i will just keep continue my lesson. Thanks for your reply !


#4

But what is it you don’t understand then? Maybe i can clarify something

When a python program runs, it needs to be interpreted. This process shouldn’t take too long, if it did, no one would use the language

so a decision need to be made, what to include and what not. Things like print can’t be missed, so they are “included” by default.

but if you then need code which isn’t included by default, you can import it (like datetime).


#5

Oh now i understand. so the “datetime” doesnt included by default so we need to import it. Thanks


#6

Yes, think of it as importing another “file”. For example, we open a blank Word document with a blank page, you can open an existing file that already has stuff written in it. Same theory applied here.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.