The prototype of the map function is map(function, iterable, …), and its return result is a list.
The function is a function name, and iterable is an iterable object (can be a list, string, tuple~~~~~)
For example:
a=(1,2,3,4,5)
b=[1,2,3,4,5]
c="NBA"
Q=map(str,a)
A=map(str,b)
W=map(str,c)
print(Q)
print(A)
print(W)
输出:
['1', '2', '3', '4', '5']
['1', '2', '3', '4', '5']
['N','B','A']
For example, to calculate the sum of the digits of a natural number:
num = input(' 请输入一个自然数:')#输入一个自然数
print(sum(map(int,num))) #把自然数的每一位都变成int形式,在用sum()函数求和