Skip to content

[Python] Tutorial(12) Import package: time, datetime, math

Last Updated on 2021-04-10 by Clay

Before we start, I need to explain the concept of "Package". Calling the "package" to "package" is my habit, if you feel my teaching is wrong, welcome to message me, very thanks. Because in the field of programming, I am just a little rookie.

"Package" is a concept of a kind of kit in Python. We browse the Internet and can see a large number of kits. Most of these kits are developed by others. Of course you can write them yourself and publish them yourself.

If we can make good use to the Internet to search for information, we can use a large number of other people's kits to complete our own current project. There is nothing wrong with it.

I don't think that every function has to be rooted from the backup.

Avoid repetitive the wheels. It's a very important thing in programming.

So, I will notes three packages in Python! Total of them are very useful!


Import Package

First, we have to import these packages.

import time
import datetime
import math

print(time.__name__)
print(datetime.__name__)
print(math.__name__)


Output:

time
datetime
math

The above, we use "import" command to import the package in Python.
__name__ it is an attribute in all package. It is the name of the package.

In addition to import the entire Module like this, you can just import the part you want to import:

import time
time.sleep(3)

from time import sleep
sleep(3)


Both of these are for the entire program to stop and sleep for 3 seconds, but the latter is only import "sleep".

In some case, this will make your program leaner and will not delay running when you call a large number of packages.

In addition, you can also import all the modules under this package into the process like this.

import time
time.sleep(3)

from time import *
sleep(3)


When we import the symbol "*", it meaning we import the all module. So we can use function "sleep", and never designate use the function "sleep" which under package "time".

By the way, if you dislike the package name is too long to code, you can also use as to decide what you want to call this package in your program.

import datetime as dt
print(dt.datetime.now())


Output:

2019-08-17 12:11:28.952152

For example, like calling a datetime package as dt.

Of course, you can import another script you wrote and the functions below it. You may wish to try this feature more!


Time

Enter "time", a very common package in Python.

A common place in time is to use time.time() to report time. If we save it as the program starting point and then execute the program —— when we finish, we call time.time() again, and subtract the time.time() of the current call from the original starting point, we can intuitively it's a few seconds to understand the program in the end.

import time
startTime = time.time()
print('start time:', startTime)

time.sleep(3)
for n in range(10):
    print(n)

print('end time:', time.time()-startTime)


Output:

start time: 1566015475.182165
0
1
2
3
4
5
6
7
8
9
end time: 3.0007588863372803

We can see the cost time is cost to make the system sleep. The part of for-loop is only a small part.

The above is the two functions commonly used in the time package: pause, timing.


datetime

Datetime, as its name suggests, is a package of "dates". This is a practical kit that I used to use for stock reptiles. (Perhaps I will start writing about the topic of "stocks" and "crawlers" after the basic teaching of Python ends)

First look at the paragraph sample code:

import datetime

print('Date:', datetime.datetime.now().date())
print('Time:', datetime.datetime.now().time())


Output:

Date: 2019-08-17
Time: 12:47:30.593110

This is the most basic use: call datetime.datetime.now() to tell us the current time.

I want to wait until the writing of Python practical teaching, this package is more often necessary to confirm the date in the crawler —— including today, yesterday, last month, or even ten years ago.


math

When it comes to the package "math", I start to feel overwhelmed. Not because my math is not good (although my math is really not very good), but the function in the package math is too much.

The following lists all the partial function in "math":

  • acosh(x)
  • acos(x)
  • asin(x)
  • asinh(x)
  • atan(x)
  • atan2(y, x)
  • atanh(x)
  • ceil(x)
  • cosh(x)
  • degrees(x)
  • e
  • erf(x)
  • erfc(x)
  • exp(x)
  • expm1(x)
  • fabs(x)
  • factorial(x)
  • floor(x)
  • fmod(x, y)
  • frexp(x)
  • fsum(iterable)
  • gamma(x)
  • gcd (a, b)
  • hypot(x, y)
  • ......

It's too much!

Of course, if you want to complement the backup of Machine Learning, I believe that you are definitely familiar with this piece. (Our boss often asks us to study the backup of processing, but I and RA always past it XDD)

Of course, I still welcome everyone to study and see those features! Most of the functions have been taught in high school, but it is directly written to us as a function, so we don't have to start from scratch when we need to use it!


References


Read More

Leave a ReplyCancel reply

Exit mobile version