Python 17th days --- time module, random module, python 17th days

Source: Internet
Author: User
Tags timedelta

Python 17th days --- time module, random module, python 17th days

Complete a assignment and start new learning:

I learned the following two modules from today's limited time. I will continue tomorrow!

 

Time module and random Module

 

Import time

1 #! Usr/bin/env python 2 #-*-coding: UTF-8-*-3 # Author calmyan 4 import time, datetime 5 print (time. process_time () # measure the processing time, excluding sleep time 6 7 print (time. altzone) # returns the time difference from UTC time. 8 print (time. asctime () # return time format "Fri May 19 11:12:23 2017" 9 print (time. time () # timestamp 10 print (time. localtime () # returns the struct_time object Format 11 print (time. localtime (time. time () + 3600) # returns the struct_time object format of the local time in 12 13 print (time. gmtime () # returns the struct_time object format of the UTC time in 14 print (time. gmtime (time. time () + 3600) # returns the struct_time object format 15 print (time. time () # current timestamp 16 print (time. asctime (time. localtime () # return time format Fri May 19 18:16:42 201717 18 print (time. ctime () # return time format Fri May 19 18:16:42 201719 20 print (time. strptime ('2017-05-19 18:16:42 ',' % Y-% m-% d % H: % M: % s ')) # convert the time and date string to the time object 21 22 string_2 = time. strptime ('2017-05-19 18:16:42 ',' % Y-% m-% d % H: % M: % s ') # convert a time string to a time object 23 stu_2 = time. mktime (string_2) # mktime can convert a time object to a timestamp 24 print (stu_2) 25 26 string_3 = time. gmtime (stu_2) # convert to time object 27 print (string_3) 28 string_4 = time. strftime ('% Y-% m-% d % H: % M: % s', string_3) # convert to time string 29 print (string_4) 30 31 print (datetime. datetime. now () # current time, local 20:16:52. 16596832 print (datetime. date. fromtimestamp (time. time () # The timestamp is directly converted to the date format 2017-05-1933 34 print (datetime. datetime. now () + datetime. timedelta (3) # current time + 3 days 35 print (datetime. timedelta (5.3) #5 days 36 print (datetime. timedelta (-6.4) #-6 days time 37 print (datetime. datetime. now () + datetime. timedelta (hours = 3) # current time + 3 hours 38 print (datetime. datetime. now () + datetime. timedelta (minutes = 30) # current time + 30 minutes 39 print (datetime. datetime. now () + datetime. timedelta (seconds = 40) # current time + 30 minutes 40 41 a_time = datetime. datetime. now () 42 print (a_time.replace (minute = 3, hour = 2) # Replace time with 43 print (a_time) 44 print (a_time.replace (minute = 3, hour = 2, second = 18, day = 4) # Time replacement
View Code

Import random

1 # random module 2 3 import random, string # character module 4 a = [1, 2, 3] 5. append (random. random () # random decimal number 6 a = random. random () # random decimal number 7 print (random. random () 8 print (random. randint () # randint random integer 9 print (random. randint (1, 8) 10 print (random. randrange (2, 3) # random integer without package ending number \ 11 12 print (random. sample (range (100), 4) # random number 13 14 str_list = string in the generator. digits + string. ascii_uppercase + string. ascii_lowercase # assign the ascii string to the variable 15 print (''. join (random. sample (str_list, 4) # concatenate 16 print (random. sample (str_list, 1) 17 # str_int = string. a18 # for I in range (0,255): 19 # print ('No.: % s % s' % (I, chr (I )))
View Code

A relatively low random code function is created:

1 def random_str (int = 4): # The default value is 4 bits, 2 str_int = string. digits # number 0-9 3 str_upp = string. ascii_uppercase # uppercase English 4 str_low = string. ascii_lowercase # lowercase letter 5 str_all = str_int + str_low + str_upp # common ascii characters 6 str_conut = [] 7 for I in range (int): 8 if I = 0: 9 str_conut.append (random. sample (str_upp, 1) [0]) # print (str_conut) 11 elif I = str_conut.append (random. sample (str_int, 1) [0]) # The second digit is the number 13 elif I = 2: 14 str_conut.append (chr (random. randint () # The third character is the special character 15 elif I = str_conut.append (random. sample (str_low, 1) [0]) # The fourth digit is the lowercase letter 17 else: 18 if len (str_conut) = int: 19 str_conut = ''. join (str_conut) 20 return str_conut21 else: 22 str_conut.append (random. sample (str_all, 1) [0]) # other digits are random characters 23 str_conut = ''. join (str_conut) 24 return str_conut

 

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.