Python study notes: regular expressions, logical operators, lamda, binary tree traversal rules, class judgment

1. How to write regular expressions?

serial number Example illustrate
1 . Matches any character (except newline)
2 \d Equivalent to [0-9], matching numbers
3 \D Equivalent to [^0-9], matches non-digits
4 \s Equivalent to [\t\r\n\f], matches space characters
5 \S Equivalent to [^\t\r\n\f], matches non-space characters
6 \w Equivalent to [A-Za-z0-9], matches single-word characters
7 \W Equivalent to [^A-Za-z0-9], matches non-single-word characters
8 [ab]cdef match acdef or bcdef
9 abc[de] Match abcd or abce
10 [0-9]\[a-z] Match numbers 0-9\lowercase letters az
11 [^0-9]\[^a-z] Match non-digits/non-lowercase letters
12 abc? Match ab or abc
13 abc* Match ab/abc/abcc...0 or more c
14 abc+ ab+1 or more c
15 \d{3} Exactly match 3 digits
16 \d{3,5} Match 3 digits, 4 digits, 5 digits
17 \d{3,} Match more than 3 digits

Boundary matching: does not consume characters in the matched string

serial number Example illustrate
1 ^ Match the starting position. Multi-line pattern matches the beginning of each line. For example, ^abc matches abc at the starting position.
2 $ Match the end position, such as .*sh$, match agfiasfush at the end position
3 \b er\b can match er in never, but cannot match er in verb
4 \B er\B can match er in verb, but cannot match er in never.
5 \A Match the starting position, ignoring multi-line patterns
6 \Z Match the end position, ignore multi-line mode

Greedy matching and lazy matching: take <div>hello world</div> as an example

serial number Example illustrate
1 <.> Greedy matching mode, the result is <div>hello world</div>
2 <.?> Lazy matching mode, the result is <div> or </div>

 2. Commonly used regular expression examples

serial number Example illustrate
username ^[a-z0-9_-]{3,16}$ Can only contain lowercase letters, numbers, underscores, -, at least 3 digits and up to 16 digits
password ^[a-z0-9_-]{3,18}$ Can only contain lowercase letters, numbers, underscores, -, at least 3 digits and up to 18 digits
hexadecimal value ^[0-9A-F]{6}|[0-9A-F]{3}$ Starting with #, 0-F, total 6 or 3 digits
E-mail ^([0-9a-zA-Z_-.])@([0-9a-z]).([az.]{2,6})$

3.Python’s logical operators

3.1 not

not True:False

3.2 and

True and False : False

and is a short-circuit operator, parsed from left to right, and stops when the result is confirmed.

0 and 18:0

15 and 18:18

4 and 3: 3

3.3 or

    1. True or True : True
    2. True or False : True
    3. False or True : True
    4. False or False : False

 Similarly, or is also a short-circuit operator. It will stop when the result is confirmed.

4 or 3 : 4

3 or 0 : 3

0 or 3 : 3

4.lamda expression

A lambda expression is an anonymous function that can be passed as a parameter to other functions or called directly.

add = lambda x, y: x + y
print(add(5, 3))

 5. Binary tree traversal

  • Preorder traversal: output the parent node first, then traverse the left subtree, and then traverse the right subtree: ABDEGCF
  • In-order traversal: first traverse the left subtree, then output the parent node, and then traverse the right subtree: DBGEACF
  • Subsequent traversal: first traverse the left subtree, then traverse the right subtree, and finally output the parent node: DGEBFCA

6.The difference between Type() and isinstance()

isinstance(object, classinfo) passes in two parameters and determines whether object belongs to the class corresponding to classinfo

type(object) generally only passes in one parameter and outputs the class corresponding to object <class'__main__.A'>

type(object) == A 实现与isinstance类似的功能但是区别在于:

        1.type只接收一个参数,不但可以判断变量是否属于某个类型,而且可以得到未知的参数变量所属的类型;而isinstance只能判断是否属于某个已知类型,不能直接得到变量未知的所属的类型

        2.isinstance可以判断子类实例对象是属于父类的;而type会判断子类实例对象和父类类型不一样

Guess you like

Origin blog.csdn.net/YGZ11113/article/details/132520625