# python入坑留念

Posted by Lazy Blog on June 15, 2017

This call fails, because you have a named argument followed by an unnamed (positional) argument, and that never works. Reading the argument list from left to right, once you have a single named argument, the rest of the arguments must also be named.

You can get a part of a list, called a “slice”, by specifying two indices. The return value is a new list containing all the items of the list, in order, starting with the first slice index (in this case a_list[1]), up to but not including the second slice index (in this case a_list[3]).

extend 和append的区别，一针见血

1
2
3
4
5
6
7
8
9
10
11
12
13
14
>>> a_list = ['a', 'b', 'c’]
>>> a_list.extend(['d', 'e', 'f']) ①
>>> a_list ['a', 'b', 'c', 'd', 'e', 'f’]
>>> len(a_list) ②
6
>>> a_list[-1]
'f’
>>> a_list.append(['g', 'h', 'i']) ③
>>> a_list ['a', 'b', 'c', 'd', 'e', 'f', ['g', 'h', 'i']]
>>> len(a_list) ④
7
>>> a_list[-1]
['g', 'h', 'i’]



As you might expect, the count() method returns the number of occurrences of a specific value in a list. 这么先进，貌似Objective-c 里面没这个方法吧

The index() method finds the first occurrence of a value in the list. In this case, ‘new’ occurs twice in the list, in a_list[2] and a_list[4], but the index() method will return only the index of the first occurrence.

1
2
3
4
5
6
>>> del a_list[1]         ①
>>> a_list.remove('new')  ①
>>> a_list.pop()   ①
>>> a_list.pop(1)  ②



In a boolean context, an empty list is false.

The major difference between tuples and lists is that tuples can not be changed. 都忘了swift 里面的元组是不是也是这样子。

• Tuples are faster than lists. If you’re defining a constant set of values and all you’re ever going to do with it is iterate through it, use a tuple instead of a list.
• It makes your code safer if you “write-protect” data that doesn’t need to be changed. Using a tuple instead of a list is like having an implied assert statement that shows this data is constant, and that special thought (and a specific function) is required to override that.
• Some tuples can be used as dictionary keys (specifically, tuples that contain immutable values like strings, numbers, and other tuples). Lists can never be used as dictionary keys, because lists are not immutable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
>>> def is_it_true(anything):
...   if anything:
...     print("yes, it's true”)
...   else:
...     print("no, it's false”)
... >>> is_it_true(()) ①
no, it's false
>>> is_it_true(('a', 'b')) ②
yes, it's true
>>> is_it_true((False,)) ③
yes, it's true
>>> type((False)) ④
<class 'bool’>
>>> type((False,)) <class 'tuple’>

①
In a boolean context, an empty tuple is false.
②
Any tuple with at least one item is true.
③
Any tuple with at least one item is true. The value of the items is irrelevant. But what’s that comma doing there?
④
To create a tuple of one item, you need a comma after the value. Without the comma, Python just assumes you have an extra pair of parentheses, which is harmless, but it doesn’t create a tuple.


v is a tuple of three elements, and (x, y, z) is a tuple of three variables. Assigning one to the other assigns each of the values of v to each of the variables, in order

The symmetric_difference() method returns a new set containing all the elements that are in exactly one of the sets. 集合操作，这个得看一下解释，别的都很直观

a_dict = {‘server’: ‘db.diveintopython3.org’, ‘database’: ‘mysql’} 这种语法糖都差不多啊

# Comprehensions

❝ Our imagination is stretched to the utmost, not, as in fiction, to imagine things which are not really there, but just to comprehend those things which are. ❞ — Richard Feynman

# Strings

For instance, you’re probably familiar with the asciiencoding, which stores English characters as numbers ranging from 0 to 127. (65 is capital “A”, 97 is lowercase “a”, &c.) English has a very simple alphabet, so it can be completely expressed in less than 128 numbers. For those of you who can count in base 2, that’s 7 out of the 8 bits in a byte. 这句话，最后一句看不太懂

The one difference is that, with the bytearray object, you can assign individual bytes using index notation. The assigned value must be an integer between 0–255.

bytes object 之前操作的比较少。