Python is the most commonly used module for advanced python


  • math.ceil(a): used to return the smallest integer ≥ a
  • math.floor(a): used to return the maximum integer ≤ a

round(a [,b])

  • If there is no parameter b, only a, round() is used for rounding
  • If there is parameter B, the function of round() is to keep b decimal places for a
print(round(100.1234))  # Rounded to 100
print(round(100.1234, 2))  # Keep 2 decimal places for 100.1234, and the result is 100.12
>>> 100
>>> 100.12



  • random.random(): returns a random floating-point number whose range is greater than or equal to 0.0 and less than 1.0
  • random. Random range (stop): returns a random integer with step size of 1 in the range greater than or equal to 0 and less than stop
  • random. Random range (start, stop [, step]): returns a random integer whose range is greater than or equal to start and less than stop and whose step is step
  • random.randint(a, b): returns a random integer in the range greater than or equal to a and less than or equal to B


The date and time modules officially provided by Python mainly include time and datetime modules. Time focuses on the underlying platform. Most functions in the module will call the C link library on the local platform. Therefore, the results of some functions will be different on different platforms. The datetime module encapsulates the time module and provides a high-level API
The core classes of the datetime module are datetime, date and time classes

datetime class

A datetime object can represent date, time and other information. The following construction methods can be used to create a datetime object:

datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None)

Where year, month and day cannot be omitted; Tzinfo is the time zone parameter. The default value is None, which means that the time zone is not specified; Except for tzinfo, all other parameters are integers in a reasonable range. The specific value range is the same as that of time in life. For example, there is no 32 day in January. Here we explain that the value range of microsecond is: 0 ≤ microsecond<1000000
The datetime class provides the following methods

  • returns the current local date and time
  • returns the current local date and time. If the parameter tz=None or is not specified, it is equivalent to today()
  • datetime.utcnow(): returns the current UTC date and time
  • datetime. From timestamp (timestamp, TZ = none): returns the local date and time corresponding to the UNIX timestamp
  • Datetime. Utcfrom timestamp (timestamp): returns the UTC date and time corresponding to the UNIX timestamp
import time
from datetime import datetime

# timestamp to datemine
timestamp = time.time()
dt = datetime.fromtimestamp(timestamp)
print(dt)dt =
>>> 1609041543.103592
>>> 2020-12-27 11:59:03.103592

# datetime to timestamp
dt =
timestamp = datetime.timestamp(dt)
>>> 2020-12-27 11:57:22.330620
>>> 1609041442.33062

Note: in the Python language, the timestamp unit is "second", so it will have a decimal part. For other languages, such as Java, the unit is "milliseconds". When cross platform computing time, you need to pay attention to this difference

Practical examples

# Requirement: convert the timestamp generated by python into java format to match your company's java backend
timestamp = str(int(round(time.time(), 3) * 1000))  # One line of code is easy to solve
Parsing process: 1.implement time.time()Get current timestamp
               2.implement round(time.time(), 3)python By default, 6 decimal places are reserved, and 3 decimal places are reserved here because python The timestamp is in seconds, java It's milliseconds,
               3.The results obtained in step 2 int(),Make sure it is int Type, multiplied by 1000 to convert the timestamp unit to milliseconds
               4.Last use str(),ensure timestamp The type of is string type


date class

  • returns the current local date
  • date. From timestamp: returns the local date corresponding to the UNIX timestamp

time class

datetime.time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None)


Date time formatting

  • str to datetime
    Many times, the date and time entered by the user is a string. To process the date and time, you must first convert str to datetime. The conversion method is through datetime Strptime() implementation requires a formatted string of date and time:
from datetime import datetime
t = datetime.strptime('2018-4-1 00:00','%Y-%m-%d %H:%M')
>>> 2018-04-01 00:00:00


  • datetime to str
    If you already have a datetime object, you need to convert it into str if you want to format it into a string and display it to the user. The conversion method is implemented through strftime(). You also need a formatted string of date and time:
from datetime import datetime
now =
print(now.strftime('%a, %b %d %H:%M'))
>>> Mon, May 05 16:28

Posted by lou28 on Sat, 16 Apr 2022 08:00:53 +0930