Smooth python and cookbook learning notes (5), pythoncookbook

Source: Internet
Author: User
Tags timedelta

Smooth python and cookbook learning notes (5), pythoncookbook
1. Random selection

Use the random module to generate random numbers in python.

1. Randomly select elements from the sequence and use random. choice ()

>>> import random>>> values = [1, 2, 3, 4, 5, 6]>>> random.choice(values)3>>> random.choice(values)3>>> random.choice(values)1>>> random.choice(values)1>>> random.choice(values)4

2. Retrieve the specified number of elements and use random. sample ()

>>> random.sample(values, 2)[1, 4]>>> random.sample(values, 2)[3, 5]>>> random.sample(values, 3)[5, 3, 2]>>> random.sample(values, 3)[1, 3, 2]

3. Disrupt the sequence, which can be used for shuffling. Use random. shuffle ()

>>> random.shuffle(values)>>> values[2, 4, 5, 3, 6, 1]>>> random.shuffle(values)>>> values[2, 6, 5, 4, 3, 1]

4. Generate a random integer using random. randint ()

>>> random.randint(1, 10)3>>> random.randint(1, 10)10>>> random.randint(1, 10)5

5. Generate a floating point number between 0 and 1 using random. random ()

>>> random.random()0.31720220264500265>>> random.random()0.8230452349376671>>> random.random()0.09307172325744872

6. generate random bitwise integers using random. getrandbits ()

>>> random.getrandbits(200)859899606181938256764615251875627706548045135119258688489931>>> random.getrandbits(200)582401031226834278134883678914218487507678688169321631685078

 

2. Time Conversion

1. Use the datetime module in python to convert the time.

>>> from datetime import timedelta>>> a = timedelta(days = 2, hours = 6)>>> b = timedelta(hours = 4.5)>>> c = a + b>>> c.days2>>> c.seconds37800>>> c.seconds / 360010.5>>> c.total_seconds() / 360058.5

Indicates a specific date and time.

>>> from datetime import datetime>>> a = datetime(2017, 9, 8)>>> print(a + timedelta(days=2))2017-09-10 00:00:00>>> b = datetime(2017, 9, 22)>>> d = b - a>>> d.days14>>> now = datetime.today()>>> print(now)2017-09-08 20:09:56.904169>>> print(now + timedelta(minutes=10))2017-09-08 20:19:56.904169

2. Use the dateutil module to process the days in different months. Datetime cannot process the month.

>>> from dateutil.relativedelta import relativedelta>>> a = datetime(2017, 9, 8)>>> a + relativedelta(months=1)datetime.datetime(2017, 10, 8, 0, 0)>>> a + relativedelta(months=4)datetime.datetime(2018, 1, 8, 0, 0)
>>> b = datetime(2017, 11, 11)>>> d = b - a>>> ddatetime.timedelta(64)
>>> d = relativedelta(b, a)>>> drelativedelta(months=+2, days=+3)>>> d.months2>>> d.days3

3. Convert the string to time, use datetime. strptime (), and convert the time to the string using datetime. strftime ()

>>> from datetime import datetime>>> text = '2017-9-8'>>> y = datetime.strptime(text, '%Y-%m-%d')>>> z = datetime.now()>>> diff = z - y>>> diffdatetime.timedelta(0, 73494, 826144)>>> ydatetime.datetime(2017, 9, 8, 0, 0)>>> zdatetime.datetime(2017, 9, 8, 20, 24, 54, 826144)>>> nice_z = datetime.strftime(z, '%A %B %d %Y')>>> nice_z'Friday September 08 2017'

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.