Python3 built-in functions
Use command 'built-in_funtion.__doc__' to display the function help and usage.
*** therer is no '()' after the function name.
1. ', '. join(string)
-->>> print(', '.join('123456'))
'1,2,3,4,5, 6'
2. set()
>>> set(['1', '2','1','7','9','7'])
{'7', '9', '2', '1'}
>>> set('1213111415326761718')
{'3', '4', '7', '5', '2', '6', '8', '1'}
3. sum(num_list)
>>> sum(['1','2','3','4','5','6'])
Traceback (most recent call last): File "", line 1, insum(['1','2','3','4','5','6'])TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> sum([1,2,3,4,5,6]) ### list
21
>>> sum((1,2,3,4,5,6)) ###tuple
21
>>> sum([1],[3])
Traceback (most recent call last): File "", line 1, insum([1],[3])
TypeError: can only concatenate list (not "int") to list
>>> sum(1,2)
Traceback (most recent call last): File "", line 1, insum(1,2)
TypeError: 'int' object is not iterable
4. max()
>>> max([1,2,3],[4,5,6])
[4, 5, 6]
>>> max([9],[1,2])
[9]
>>> max([9],[1,2,9])
[9]
>>> max([9],[10,2,9])
[10, 2, 9]
>>> max([9],[9,2,1])
[9, 2, 1]
>>> max('str1','str2')
'str2'
>>> max('1str','str2')
'str2'
5. min()
>>> min('1str','str2')
'1str'
>>> min([9],[9,2,1])
[9]
>>> min([9],[1,2,9])
[1, 2, 9]
>>> min([1,2,3],[4,5,6])
[1, 2, 3]
6. id(): returns the 'identity' of an object, actually the memory address in memory
>>> id(1)
1850061888
>>> id(2)
1850061920
>>> id(3)
1850061952
>>> id(0)
1850061856
7. any(iterable): return 'True', if any emlement of the iterable is true.
>>> any(' ')
True
>>> any('')
False
>>> any('False')
True
>>> any('00000000000')
True
>>> any(' ')
True
>>> any('')
False
>>> any([])
False
>>> any([ ])
False
>>> any([''])
False
>>> any([' '])
True
8. round(number, [,ndigits]): returns the number nearest to ndigits, or the nearest integer if ndigits is none.
>>> round(2.51, 3)
2.51
>>> round(2.515, 3)
2.515
>>> round(2.675, 2)
2.67
>>> round(2.615,2)
2.62
>>> round(2.515, 2)
2.52
>>> round(2.51, 2)
2.51
9. locals(): Update and return a dictionary representing the current local symbol table.
>>> locals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__':, '__spec__': None, '__annotations__': {}, '__builtins__':, 'list1': ['1'], 'str2': '1-2-3-4-5-6-7-8-9', 'list_set': ['-', '1', '2', '3', '4', '5', '6', '7', '8', '9'], 'list_sort': ['-', '1', '2', '3', '4', '5', '6', '7', '8', '9']}
10. sorted(iterable) and list.sort()
1). sorted(iterable), not modify the iterable, but returns a new sorted list;
2). sort(), modify the list in-place, and return None.
3). sort() only accept list parameter, but sorted() accept any type of iterable parameter.
>>> list1=[1,5,8,2,3,9,4]
>>> list1
[1, 5, 8, 2, 3, 9, 4]
>>> sorted(list1)
[1, 2, 3, 4, 5, 8, 9]
>>> list1
[1, 5, 8, 2, 3, 9, 4]
>>> list1.sort()
None
>>> list1
[1, 2, 3, 4, 5, 8, 9]
>>> sorted({7: 'D', 2: 'B', 3: 'B', 9: 'E', 5: 'A', 6: 'Z'})
[2, 3, 5, 6, 7, 9]
>>> sorted("This is a test string from Andrew".split(), key=str.lower)
['a', 'Andrew', 'from', 'is', 'string', 'test', 'This']