Python: 1 Useful Build-In Methods 1
Python: 1 Useful Build-In Methods 1
2021 年 4 月 10 日
目录
Python indexing:
2. a slice goes up to but will not include the value at the second index.
3. negative indexing slicing: a[-4:-1]. You can also specify the step
size, like: s[4:1:-2] index in reverse order and -2 is the step size.
1
Delete one element value in a list: del spam[2] (notice the space in be-
tween.)
in and not in are operator. Like `cat` in spam return a Boolean value.
operator +=, -=, the math operator all comes before =, meaning
Key, values, items are method, such as A.keys(), A.values() and A.items().
Use these methods to return list-like values of the dictionary’s key, values
or both keys and values.
Dictionary have a get() method that takes two arguments, the key of the
value to retrieve and a fall back value to return if the key does not exist.
picnicItems.get('cup',0). picnicItems is a dictionary.
2
ing on list.
sorted(l) return sorted list, does not mutate list l. l.sort() mutate list
l as well, list is changed.
sort() uses ‘ASCIIbetical order’ rather than actual alphabetical order for
sorting strings. This means uppercase letters come before lowercase let-
ters. If you need to sort the values in regular alphabetical order, pass
str.lower for the ‘key‘ keyword argument in the sort() method call.
spam.sort(key=str.lower)
The any(...) function returns True if any item in an iterated item are
true, otherwise it returns False. The argument is a list of Boolean results.