以下を参照。
シーケンス (リスト・タプル・文字列) の演算
リスト
>>> a = [1,2,3] >>> 1 in a True >>> 10 in a False >>> 2 not in a False >>> 20 not in a True >>> b = [4,5,6] >>> a + b [1, 2, 3, 4, 5, 6] >>> a * 3 [1, 2, 3, 1, 2, 3, 1, 2, 3] >>> c = a * 3 >>> c [1, 2, 3, 1, 2, 3, 1, 2, 3] >>> c[4:7] [2, 3, 1] >>> c[4:7:2] [2, 1] >>> len(c) 9 >>> min(c) 1 >>> max(c) 3 >>> reversed(c) <listreverseiterator object at 0x6fbf0> >>> [i for i in reversed(c)] [3, 2, 1, 3, 2, 1, 3, 2, 1] >>> sorted(c) [1, 1, 1, 2, 2, 2, 3, 3, 3] >>> c [1, 2, 3, 1, 2, 3, 1, 2, 3]
文字列
>>> str = 'abc' >>> 'a' in str True >>> 'd' in str False >>> 'c' not in str False >>> str2 = 'def' >>> str + str2 'abcdef' >>> str3 = str *3 >>> str3 'abcabcabc' >>> str3[4:7] 'bca' >>> str3[4:7:2] 'ba' >>> len(str3) 9 >>> min(str3) 'a' >>> max(str3) 'c' >>> reversed(str3) <reversed object at 0x78d670> >>> [i for i in reversed(str3)] ['c', 'b', 'a', 'c', 'b', 'a', 'c', 'b', 'a'] >>> "".join([i for i in reversed(str3)]) 'cbacbacba' >>> sorted(str3) ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c'] >>> "".join(sorted(str3)) 'aaabbbccc' >>> str3 'abcabcabc'