from AcWing - Django Course simplification learning
Learning experience : Learning while checking , Read through the commonly used ones first
It's an array. , But the type can be different
Common functions :
list.append(x)
len(list)
List derivation
squares = [x**2 for x in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Two dimensional array
>>> matrix = [
... [1, 2, 3, 4],
... [5, 6, 7, 8],
... [9, 10, 11, 12],
... ]
Similar list , Just can't change , It can be defined without ()
In [13]: t = (1, 2, 3)
In [14]: t[0]= 4
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-14-a84d171bf4ef> in <module>
----> 1 t[0]= 4
TypeError: 'tuple' object does not support item assignment
In [15]: t = 1, 2, 3
In [16]: t
Out[16]: (1, 2, 3)
Tuple packing and sequence unpacking , For convenient exchange
Sequence unpacking
Python And support aggregate This data type . A collection is an unordered container of non repeating elements . Corresponding C++ Of set
. Basic usage includes member detection 、 Eliminate duplicate elements . Collection objects support collections 、 intersection 、 Difference set 、 Symmetrical difference and other mathematical operations .
Create a collection with curly braces or set() function . Be careful , Creating an empty collection can only use set(), Out-of-service {},{} An empty dictionary is created , The next section introduces the data structure : Dictionaries .
Here are some simple examples
>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>> print(basket) # show that duplicates have been removed
{'orange', 'banana', 'pear', 'apple'}
>>> 'orange' in basket # fast membership testing
True
>>> 'crabgrass' in basket
False
>>> # Demonstrate set operations on unique letters from two words
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a # unique letters in a
{'a', 'r', 'b', 'c', 'd'}
>>> a - b # letters in a but not in b
{'r', 'd', 'b'}
>>> a | b # letters in a or b or both
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b # letters in both a and b
{'a', 'c'}
>>> a ^ b # letters in a or b but not both
{'r', 'd', 'b', 'm', 'z', 'l'}
>>> tel = {'jack': 4098, 'sape': 4139}
>>> tel['guido'] = 4127
>>> tel
{'jack': 4098, 'sape': 4139, 'guido': 4127}
Flip reversed()
And sort sorted()
function
Python Many packages have been implemented , Directly install and call
When processing file objects , Best use with keyword . Advantage is , At the end of the clause body , The file will close correctly , Even if an exception is triggered . and , Use with Compared with the equivalent try-finally The code block is much shorter :
Write
>>> with open('workfile') as f:
... read_data = f.read()
>>> # We can check that the file has been automatically closed.
>>> f.closed
True
read
>>> def divide(x, y):
... try:
... result = x / y
... except ZeroDivisionError:
... print("division by zero!")
... else:
... print("result is", result)
... finally:
... print("executing finally clause")
...
>>> divide(2, 1)
result is 2.0
executing finally clause
>>> divide(2, 0)
division by zero!
executing finally clause
>>> divide("2", "1")
executing finally clause
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in divide
TypeError: unsupported operand type(s) for /: 'str' and 'str'
As shown above , In any case finally Clause .except Clause does not handle the... Triggered by the division of two strings TypeError, So it will finally Clause is triggered again after execution .
In a real application ,finally Clause for releasing external resources ( For example, file or network connection ) Very useful , Whether or not resources are used successfully .
Constructors , Constructors can also pass parameters , Even keyword correspondence , Unpacking operation
Define member functions
Inherit , Call the constructor of the base class