配列(タプル)の要素の和を求める
組み込み関数のsumを用いるとタプルの要素の和を求めることができます。以下が、sumの構文です。
以下が、実際のコードです。
sum(iterable)iterableは、リストのようなイテレータです。
以下が、実際のコードです。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
a = (10,20,20,40,50)
#和を求めます。
print a,sum(a)
#文字の和は求められません。
a = ('a','b','c','d')
print a,sum(a)
以下が実行結果です。
(10, 20, 20, 40, 50) 140
('a', 'b', 'c', 'd')
Traceback (most recent call last):
File "C:\2array_make.py", line 11, in <module>
print a,sum(a)
TypeError: unsupported operand type(s) for +: 'int' and 'str'

