Python Learning Diary 5

Source: Internet
Author: User

One, recursive function

1 #in the list L, to find one of the values, the dichotomy can greatly save the query time2l=[2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88]3     #If you want to find a 66 position without using index4 defSearch (num,l,start=none,end=None):5Start = StartifStartElse06End = EndifEnd isNoneElseLen (L)-17Mid = (End-start)//2 +Start8     ifStart >End:9         returnNoneTen     elifL[mid] >Num: One         returnSearch (num,l,start,mid-1) A     elifL[mid] <Num: -         returnSearch (num,l,mid+1, end) -     elifL[mid] = =Num: the         returnMid -  -  - #binary method can only find ordered series
Binary Method Search
menu = {    'Beijing': {        'Haidian': {            'Five crossing': {                'Soho': {},                'NetEase': {},                'Google': {}            },            'Zhongguancun': {                'Iqiyi Art': {},                'Autohome': {},                'Youku': {},            },            'on the ground': {                'Baidu': {},            },        },        'changping': {            'Shahe': {                'old boy': {},                'Beihang University': {},            },            'Tin Tong Court': {},            'Huilongguan': {},        },        'Chaoyang': {},        'Dongcheng': {},    },    'Shanghai': {        'Minhang': {            "People's square": {                'Fried Chicken Shop': {}            }        },        'Zhabei': {            'Train Warfare': {                'Ctrip': {}            }        },        'Pudong': {},    },    'Shandong': {},}menu
Level Three menu
defThreelm (DIC): whileTrue: forKinchDic:Print(k) Key= Input ('input>>'). Strip ()ifKey = ='b' orKey = ='Q':returnKeyelifKeyinchDic.keys () andDic[key]: ret=Threelm (Dic[key])ifRET = ='Q':return 'Q'Threelm (menu) Recursive function implementation level three menu
using Recursive Functions

Two, module

Concept: A module is a file that contains Python definitions and declarations

Common modules, divided into four general categories:

1 code written using Python (. py file)

2 C or C + + extensions that have been compiled as shared libraries or DLLs

3 packages for a set of modules

4 built-in modules written and linked to the Python interpreter using C

On the basis of the built-in data type (DICT, list, set, tuple),The collections module also provides several additional data types: Counter, deque, Defaultdict, Namedtuple, and Ordereddict.

1.namedtuple: Generate a tuple that can access the content of an element by using a name

2.deque: Double-ended queue to quickly append and eject objects from the other side

3.Counter: Counter, mainly used to count

4.OrderedDict: Ordered Dictionary

5.defaultdict: Dictionary with default values

Three, Time module


Three ways to represent time

In Python, there are usually three ways to represent time: timestamp, tuple (struct_time), formatted time string:

1 timestamp (timestamp): Typically, a timestamp represents an offset that is calculated in seconds, starting January 1, 1970 00:00:00. We run "type (Time.time ())" and return the float type.

2 formatted time string (format string): ' 1999-12-06 '

%y Two-digit year represents (00-99)%y Four-digit year (000-9999)%m Month (01-12) the dayof the month (0-31)%H 24-hour hours (0-23)%I 12-hour hours (01-12)%M minutes (00=59)%s seconds (00-59)
Common time representation

3 tuples (struct_time): Struct_time A total of 9 elements total nine elements: (year, month, day, time, minute, second, week of the year, Day of the year, etc.)

Four, random module

Import Random def V_code ():     "'     for  in range (5):        num=random.randint (0,9)        Alf=CHR (Random.randint (65,90))        Add=random.choice ([num,alf])        code="". Join ([Code,str (add)])     return  codePrint(V_code ())
Generate Random Series

An OS module is an interface that interacts with the operating system

SYS module is an interface that interacts with the Python interpreter

Five, re module

The various characters that may appear in the same position make up a group of characters, and in regular expressions the characters are divided into classes, such as numbers, letters, punctuation, and so on.

Greedy match: Matches the string as long as possible when matching matches, by default, greedy match

is any character * to take 0 to infinity length? non-greedy mode. Where together is to take as few as possible any character, generally do not write so alone, he mostly used in:. *X is the character of any length preceding it, until an X appears

^ ((https|http|ftp|rtsp|mms)?: \ /\/) [^\s]+     URL url\w[-\w.+]*@ ([a-za-z0-9][-a-za-z0-9]+\.) +[a-za-z]{2,14}   mailbox \d{17}[\d|x]|\d{15}    Social Security Number [A-za-z0-9_\-\u4e00-\u9fa5]+    user name
Regular Test

Python Learning Diary 5

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: 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.