2016/04/23
かわら@okwrtdsh
Pythonの名前の由来はニシキヘビではない。 BBC のコメディシリーズ“Monty Python’s Flying Circus”が由来らしい。 (Why is it called Python?)
The Zen of Pythonより一部抜粋
False class finally is return None continue for lambda try True def from nonlocal while and del global not with as elif if or yield assert else import pass break except in raise
$ python3 >>> print('Hello World!') Hello World! >>> exit()
$ echo "print('Hello World!')" > hello_world.py $ python3 hello_world.py Hello World!
>>> 1 + 2 3 >>> 1 - 2 -1 >>> 2 * 3 6 >>> 2 ** 3 8 >>> 10 % 3 1 >>> 10 / 3 3.3333333333333335 >>> 10 // 3 3
>>> a = "hoge" >>> a 'hoge' >>> """hoge ... fuga ... """ 'hoge\nfuga\n' >>> a * 2 'hogehoge' >>> a[0] 'h' >>> a[-1] 'e' >>> len(a) 4
>>> a = [6, 1, 2] >>> a[0] 6 >>> a.append(3) >>> a [6, 1, 2, 3] >>> a.pop() 3 >>> a [6, 1, 2] >>> len(a) 3 >>> sum(a) 9
>>> t = (7, "hoge") >>> t (7, 'hoge') >>> (1) 1 >>> (1, ) (1,) >>> t[0] 7 >>> t[1] 'hoge' >>> i, v = t >>> i 7 >>> v 'hoge'
>>> d = { ... "key": "value", ... "num": 1, ... } >>> d["key"] 'value' >>> d["num"] 1 >>> d["num"] = 2 >>> d["num"] 2 >>> "key" in d.keys() True >>> "hoge" in d.keys() False
>>> "value" in d.values() True >>> "hoge" in d.values() False >>> d["hoge"] Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'hoge' >>> d.get("key") 'value' >>> d.get("hoge") # Noneが返る >>> d.get("hoge", "default") 'default'
>>> A = set("Hello") >>> A {'H', 'e', 'o', 'l'} >>> B = set("World") >>> B {'l', 'd', 'o', 'r', 'W'} >>> A - B {'H', 'e'} >>> A | B {'o', 'l', 'd', 'W', 'H', 'r', 'e'} >>> A & B {'o', 'l'} >>> A ^ B {'d', 'W', 'H', 'r', 'e'}
>>> n = 10 >>> if n > 0: ... print("positive number") ... elif n < 0: ... print("negative number") ... else: ... print("zero") ... positive number
>>> n = 10 >>> if n > 0 and n % 2 == 0: ... print("positive multiple of two") ... elif n % 2 == 0: ... print("negative multiple of two") ... elif n > 0: ... print("positive") ... else: ... print("negative") ... positive multiple of two
for 変数 in オブジェクト: 処理
while 条件式: 処理
range([start,] stop[, step])
>>> for i in [0, 1, 2]: ... print(i) ... 0 1 2 >>> for i in range(3): ... print(i) ... 0 1 2
>>> i = 0 >>> while i < 3: ... print(i) ... i += 1 ... 0 1 2
def 関数名(引数1, 引数2, ... ): 処理
lambda 引数1, 引数2, ... : 引数を使う式
関数名(引数1, 引数2, ... )
>>> def add(x, y): ... return x + y ... >>> add(2, 3) 5 >>> f = lambda x, y: x + y >>> f(2, 3) 5
>>> "2" + "3" '23' >>> int("2") + int("3") 5 >>> 2 * 3 6 >>> str(2) * 3 '222' >>> a = map(str, [0, 1, 2]) >>> a # python3からリストではなくmap object(iterator)が返る <map object at 0x109b11048> >>> list(a) ['0', '1', '2'] >>> b = map(int, ['0', '1', '2']) >>> list(b) [0, 1, 2]
>>> [i ** 2 for i in range(5)] [0, 1, 4, 9, 16] >>> [i for i in range(10) if i % 2 == 0] [0, 2, 4, 6, 8] >>> [i if i < 5 else 'L' for i in range(10)] [0, 1, 2, 3, 4, 'L', 'L', 'L', 'L', 'L'] >>> [i if i < 5 else i if i % 2 == 0 else 'L' for i in range(10)] [0, 1, 2, 3, 4, 'L', 6, 'L', 8, 'L'] >>> [x + y for x in range(3) for y in range(3)] [0, 1, 2, 1, 2, 3, 2, 3, 4]
mapやfilterで記述することもできる
>>> [i ** 2 for i in range(5)] [0, 1, 4, 9, 16] >>> list(map(lambda x: x ** 2, range(5))) [0, 1, 4, 9, 16] >>> [i for i in range(10) if i % 2 == 0] [0, 2, 4, 6, 8] >>> list(filter(lambda x: x % 2 == 0, range(10))) [0, 2, 4, 6, 8]
特定の要素が含まれるか
>>> 3 in range(5) True >>> 'c' in "abcdef" True >>> d = { ... "key": "value", ... "num": 1 ... } >>> "key" in d True >>> "value" in d.values() True
>>> s = input() hoge # 入力 >>> s 'hoge' >>> i = int(input()) 7 # 入力 >>> i 7 >>> l = input().split() hoge 7 # 入力 >>> l ['hoge', '7'] >>> l = map(int, input().split()) 3 3 4 # 入力 >>> list(l) [3, 3, 4]
>>> print("hoge") hoge # 改行あり >>> print("hoge", end="") hoge>>> # 改行なし >>> print("hoge", "fuga") hoge fuga # スペース区切り >>> print(", ".join(["hoge", "fuga"])) hoge, fuga # カンマ区切り
>>> def hoge(): ... print("hoge") ... >>> def fuga(x=hoge()): ... pass ... hoge # 関数の定義時に1回だけ評価される >>> fuga() # 評価されない
>>> f = [lambda x: x * i for i in range(5)] >>> f[2](5) # 呼び出した時にはiはすでに4になっている 20 >>> f[3](5) 20 >>> f = [lambda x, i=i: x * i for i in range(5)] >>> f[2](5) # keyword引数に渡すと定義時に評価される 10 >>> f[3](5) 15
>>> def outer1(): ... x = 1 ... def inner1(): ... print(x) ... inner1() ... print(x) ... >>> outer1() 1 # inner1 1 # outer1
>>> def outer2(): ... x = 1 ... def inner2(): ... x = 2 ... print(x) ... inner2() ... print(x) ... >>> outer2() 2 # inner2 1 # outer2
>>> a = list(range(5)) >>> a [0, 1, 2, 3, 4] >>> b = a >>> b [0, 1, 2, 3, 4] >>> b[0] = 5 >>> b [5, 1, 2, 3, 4] >>> a [5, 1, 2, 3, 4]
>>> a = list(range(5)) >>> a [0, 1, 2, 3, 4] >>> b = a[:] >>> b [0, 1, 2, 3, 4] >>> b[0] = 5 >>> b [5, 1, 2, 3, 4] >>> a [0, 1, 2, 3, 4]
出力
1 2 San! 4 5 San! 7 8 San! 10 ...
>>> for i in range(1, 101): ... if i % 3 == 0 or "3" in str(i): ... print("San!") ... else: ... print(i)
入力
3 1 3 7
出力
2, 4, 8
n = int(input()) l = [] for _ in range(n): l.append(input()) print(", ".join(map(lambda x: str(int(x) + 1), l)))