1. Global scope value can not be changed in function.

2. We can define a global variable in a function by using global.

3. Rules of using variable:

  • First start with the local scope, if any. If the variable is defined here, use the value.
  • Look at any enclosing scopes, starting with the innermost. These are "outside" local scopes. If the variable is defined in any of them, use the value.
  • Look in the global scope. If the variable is there, use the value.
  • Finally, look in the built-in functions.
  • If no value is found, an error will be thrown.

 

Regular Expression(for extract number from a long string):

1. Regular Expression is to define a pattern of characters, and find whether the target contains the pattern.

2. The special character "." is used to indicate that any character can be put in its place.(a.b = aob)

3. "^a" will match all strings that start with "a".(^a = ab)

4. "a$" will match all strings that end with "a".(a$ = sa)

5. re.search() function: re.search(regex, string). Regex is the regular expression, string is the target String. If we found in the target string, there is something match the regular expression. It will return a match, if not, it will return None.

6.[Rr]eddit means search for both capital and lower case raddit.

7. Excape special charactors "\"

8. All the logic is running in the regex.

9.if we were to call re.sub("yo", "hello", "yo world"), the function will replace "yo" with "hello" in "yo world", producing the result "hello world". The sub() function simply returns the original string if pattern is not found. Notice: here "yo" is regular expression, but "hello" is not.

10.findall() returns a list of substrings that match the provided regular expression. re.findall("[a-z]", "abc123") would return ["a", "b", "c"], since those are the substrings that match the regular expression.

posted on 2016-10-04 02:45  阿难1020  阅读(120)  评论(0编辑  收藏  举报