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.
- The raw string `print(r"c:\news")` will not be translated into a blank line.
- 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.
- 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`.
- Formatting output, str.format(), is a complex function; it's recommended to consult the documentation before using it.
- 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)`.
- 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.
- The methods for tuples, dictionaries, and sets are quite varied and require review when applying them in practice.
- Clarify the concepts: string, list, dict, tuple, set.
- Boolean judgment:
- An empty string returns false, and a blank string (string) returns true.
a = ' ' bool(a) // true a = '' bool(a) bool([]) bool({}) // false
- An empty string returns false, and a blank string (string) returns true.
- Ternary operator::A = Y if X else Z // X is true, A = Y otherwise A = Z
- 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]
- The default value for `print()` is `end = '\n'`, but you can use `print(x, end='xxx')` instead.
- 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)
- 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.
- zip(): can merge two arrays (length is determined by the shorter one), reverse zip(*x)
- `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
- Inline assignment: s = [x**2 for x in range(1,10)] // Output [1,4,9,16,25,36,49,64,81]
- Several commonly used modes of the open() function for opening files:
- w: Writable; if the file exists, it will be overwritten.
- a: Writable, writes from the end of the file, creates if it doesn't exist.
- With `open()`, you don't need to close the window; it also provides the functionality of `try final`.
- function
- The function `def foo(x, *arg)` will receive an empty tuple as `*arg`.
- The function `def foo(**arg)` returns a dictionary-type data structure.
- lambda – g= lambda x,y : x+y // g(3,4) == 7
- map(func, seq) – Puts the elements of seq into func in order.
- filter() – filter(lambda x: x>0, numbers)
- Inherited from class Person(FatherClass):
- Decorators @classmethod/@staticmethod/@property...
- The use of super in inheritance
- 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"