このサイトを参考にしながら、Python programmingの基礎である変数とデータ型(Variables and data types)についての学習を試みる。
Integers¶
a = 30
b = a
print('a:', a, '\nb:', b)
a = a + 5
a += 10
print('a:', a, '\nb:', b)
x = ['foo', [1, 2], 3, 4]
print('x:', x)
y = x
print('y:', y)
x = 22
print('x:', x, '\ny:', y)
x = y
x[0] = 55
print('x:', x)
Unlike integers, lists are mutable:
整数と違ってリストは可変。
print('y:', y)
As seen above, we have to be careful about sharing (also known as “aliasing”) mutable data!
このように、可変データのシェア(エイリアシングとしても知られている)には注意しなければならない。
a = [1, 2, 3]
b = [a, a, a]
print(b)
b[0][0] = 3
print(b)
print(a)
b[0][1] = 5
print(b)
print(a)
b[0][2] = 8
print(b)
print(a)
b[1][2] = 4
print(b)
print(a)
b[0][2] = 8
print(b)
print(a)
Tuples¶
Tuples are a lot like lists, except that they are immutable.
タプルは、それらが不変であることを除けば、リストによく似ている。
x = ('foo', [1, 2], 3, 4)
y = x
print('x:', x, '\ny:', y)
Unlike a list, we can’t change the top most structure of a tuple. What happens if we try the following?
リストと違って、タプルの最上構造は変えられない。以下を試みた場合どうなるだろうか?
x[0] = 88
What will happen in the following (operating on x)?
以下(xに対する操作)ではどうなるだろうか?
x[1][0] = 22
print('x:', x, '\ny:', y)
So we still need to be careful! The tuple didn’t change at the top level — but it might have members that are themselves mutable.
以上のように注意が必要な事が分かる。タプルはトップレベルでは変化しないが、自身が可変のメンバーを有している可能性があるからだ。
Strings¶
Strings are also immutable. We can’t change them once created.
文字列も不変なので作成後は変えることができない。
a = 'hi'
b = a + 'gh'
print('a:', a, '\nb:', b)
a[0] = 'H'
c = 'hello'
d = c
c += ' there'
print('c:', c, '\nd:', d)
That’s a little bit tricky. Here the ‘+=’ operator makes a copy of c first to use as part of the new string with ‘ there’ included at the end.
少しトリッキーで、ここでは、+=演算子が、最後に新しい文字列thereを含んだ新しい文字列の一部として使う目的で最初にcをコピーしている。
Back to lists¶
append, extend¶
x = [1, 2, 3]
y = x
x.append([4, 5])
print('x:', x, '\ny:', y)
So again, we have to watch out for aliasing/sharing, whenever we mutate an object.
タプル同様、リストのエイリアシング/シェアリングに対しても、オブジェクトを変化させる時は常に注意しなければならない。
x = [1, 2, 3]
y = x
x.extend([4, 5])
print('x:', x, '\ny:', y)
‘+’ and ‘+=’ operators¶
What happens when using the ‘+’ operator used on lists?
リストに+演算子を使うとどうなるか?
x = [1, 2, 3]
y = x
x = x + [4, 5]
print('x:', x)
So the ‘+’ operator on a list looks sort of like extend. But has it changed x in place, or made a copy of x first for use in the longer list? And what happens to y in the above?
よって+演算子はextendのように見えなくもないが、xを直接書き換えているのか、または、より長いリストに使えるように最初にxのコピーを作成しているのか?また、上の例でyはどうなっているのか?
print('y:', y)
So that clarifies things — the “+” operator on a list makes a (shallow) copy of the left argument first, then uses that copy in the new larger list.
明確に言えば、+演算子はリストに対して最初に左項の浅いコピーを作り、その後そのコピーをより大きな新しいリストで使用する。
Another case, this time using the “+=” operator with a list. Note: in the case of integers, a = a + and a += gave exactly the same result. How about in the case of lists?
別のケースで、今回は+=演算子をリストで使う。整数の場合、a = a +とa +=は全く同じ結果になる。リストの場合はどうだろうか?
x = [1, 2, 3]
y = x
x += [4, 5]
print('x:', x, '\ny:', y)
So x += is NOT the same thing as x = x + if x is a list! Here it actually DOES mutate or change x in place, if that is allowed (i.e., if x is a mutable object).
x +=は、xがリストだとx = x +と別物になる。+=は、それが許されるなら(つまり、xが可変オブジェクトの場合に限り)、実際にxを直接書き換える。
Contrast this with the same thing, but for x in the case where x was a string. Since strings are immutable, python does not change x in place. Rather, the += operator is overloaded to do a top-level copy of the target, make that copy part of the new larger object, and assign that new object to the variable.
この事をxが文字列の場合と対比する。文字列は不変であることから、pythonはxを直接書き換えない。代わりに、+=演算子は標的のトップレベルコピーを作成するよう多重定義され、そのコピーを新しいより大きなオブジェクトの一部にし、その新しいオブジェクトを変数に割り当てる。
Let’s check your understanding. What will happen in the following, that looks just like the code above for lists, but instead using tuples. What will x and y be after executing this?
上のリスト用コードのように見えるが、代わりにタプルを使用している以下の場合どうなるか?これを実行後、xとyはどうなるか?
x = (1, 2, 3)
y = x
x += (4, 5)
print('x:', x, '\ny:', y)