Python Learning Notes

Recently, while playing around with TensorFlow and writing web crawlers, I've frequently been recommended Python. I was initially indifferent to this language, but I've discovered its strong practicality and suddenly become interested. Since I had some time during my trip back to China, I bought two books to systematically study it. This document is a record of some commonly used methods and features during my learning process, for future reference.

  1. The raw string `print(r"c:\news")` will not be translated into a blank line.
  2. String slicing and indexing:
    • str.index("x") returns the position.
    • `str[2:9]` returns a slice of the string from 2 to 9. You can also use `str[:9]` or `str[2:]`. Note that the slice does not include the last digit; that is, it does not include 9.
  3. The `in` operator can be used to determine if a string is present in the string `str`. For example, `"p" in str` returns `true` or `false`.
  4. Formatting output, str.format(), is a complex function; it's recommended to consult the documentation before using it.
  5. Common string methods:
    • str.isalpha()
    • str.split(), splits by
    • str.strip() removes leading and trailing whitespace.
    • The `join()` function joins strings, such as `.".join(array)`.
  6. Common array methods:
    • array.extend(array2)
    • `array.append(array2)` performs the original modification; that is, it doesn't add `array2`, but inserts it as a whole.
    • array.insert(i,x) inserts an array at position i.
    • array.remove(x) returns nothing after deletion.
    • array.pop() removes the last element and returns the result, or array.pop([i]) removes the i-th element and returns the result.
  7. The methods for tuples, dictionaries, and sets are quite varied and require review when applying them in practice.
  8. Clarify the concepts: string, list, dict, tuple, set.
  9. Boolean judgment:
    • An empty string returns false, and a blank string (string) returns true.
      a = ' ' bool(a) // true a = '' bool(a) bool([]) bool({}) // false
  10. Ternary operator::A = Y if X else Z // X is true, A = Y otherwise A = Z
  11. for i in range(len(names)): print(names[i]) // Equivalent to range(x), for example, if the length of name is 5, it is equal to i in [0,1,2,3,4]
  12. The default value for `print()` is `end = '\n'`, but you can use `print(x, end='xxx')` instead.
  13. for loop:
    • Dictionary: for k,v in d.items(): print(k + v)
    • Numbers are not iterable
    • To determine if iterable is possible: isinstance(x, collections.Iterable)
  14. range: range(start, stop, step), start defaults to 0, stop must be specified (so range(9) is 0-9), and step defaults to 1 and cannot be 0.
  15. zip(): can merge two arrays (length is determined by the shorter one), reverse zip(*x)
  16. `enumerate()`: For i, string in enumerate(x), outputs the index and the data.
    week = ['monday', 'tuesday'] for (i, day) in enumerate(week): print(day + ' is ' + week(i) // monday is 0 // tuesday is 1
  17. Inline assignment: s = [x**2 for x in range(1,10)] // Output [1,4,9,16,25,36,49,64,81]
  18. Several commonly used modes of the open() function for opening files:
    1. w: Writable; if the file exists, it will be overwritten.
    2. a: Writable, writes from the end of the file, creates if it doesn't exist.
  19. With `open()`, you don't need to close the window; it also provides the functionality of `try final`.
  20. function
    1. The function `def foo(x, *arg)` will receive an empty tuple as `*arg`.
    2. The function `def foo(**arg)` returns a dictionary-type data structure.
    3. lambda – g= lambda x,y : x+y // g(3,4) == 7
    4. map(func, seq) – Puts the elements of seq into func in order.
    5. filter() – filter(lambda x: x>0, numbers)
  21. Inherited from class Person(FatherClass):
  22. Decorators @classmethod/@staticmethod/@property...
  23. The use of super in inheritance
  24. The variable in the double-slide pattern is a private variable and cannot be accessed externally.

This siteOriginal articleAll follow "Attribution-NonCommercial-ShareAlike 4.0 License (CC BY-NC-SA 4.0)Please retain the following annotations when sharing or adapting:

Original author:Jake Tao,source:"Python Learning Notes"

165
0 0 165

Post a reply

Log inYou can only comment after that.
Share this page
Back to top