Python:変数とデータ型 (整数・文字列・リスト・タプル)

その買うを、もっとハッピーに。|ハピタス

このサイトを参考にしながら、Python programmingの基礎である変数とデータ型(Variables and data types)についての学習を試みる。

スポンサーリンク

Integers

a = 30
b = a
print('a:', a, '\nb:', b)
a: 30 
b: 30
a = a + 5
a += 10
print('a:', a, '\nb:', b)
a: 45 
b: 30

So far so good — integers, and variables pointing to integers, are straightforward.
ここまではすんなりと行く。整数と整数を指し示す変数は単純明快だ。

x = ['foo', [1, 2], 3, 4]
print('x:', x)
x: ['foo', [1, 2], 3, 4]
y = x
print('y:', y)
y: ['foo', [1, 2], 3, 4]
x = 22
print('x:', x, '\ny:', y)
x: 22 
y: ['foo', [1, 2], 3, 4]
x = y
x[0] = 55
print('x:', x)
x: [55, [1, 2], 3, 4]

Unlike integers, lists are mutable:
整数と違ってリストは可変。

print('y:', y)
y: [55, [1, 2], 3, 4]

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)
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
b[0][0] = 3
print(b)
print(a)
[[3, 2, 3], [3, 2, 3], [3, 2, 3]]
[3, 2, 3]
b[0][1] = 5
print(b)
print(a)
[[3, 5, 3], [3, 5, 3], [3, 5, 3]]
[3, 5, 3]
b[0][2] = 8
print(b)
print(a)
[[3, 5, 8], [3, 5, 8], [3, 5, 8]]
[3, 5, 8]
b[1][2] = 4
print(b)
print(a)
[[3, 5, 4], [3, 5, 4], [3, 5, 4]]
[3, 5, 4]
b[0][2] = 8
print(b)
print(a)
[[3, 5, 8], [3, 5, 8], [3, 5, 8]]
[3, 5, 8]
スポンサーリンク

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)
x: ('foo', [1, 2], 3, 4) 
y: ('foo', [1, 2], 3, 4)

Unlike a list, we can’t change the top most structure of a tuple. What happens if we try the following?
リストと違って、タプルの最上構造は変えられない。以下を試みた場合どうなるだろうか?

x[0] = 88
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-29-9ea8cc8497bf> in <module>
----> 1 x[0] = 88

TypeError: 'tuple' object does not support item assignment

What will happen in the following (operating on x)?
以下(xに対する操作)ではどうなるだろうか?

x[1][0] = 22
print('x:', x, '\ny:', y)
x: ('foo', [22, 2], 3, 4) 
y: ('foo', [22, 2], 3, 4)

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: hi 
b: high
a[0] = 'H'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-4903e04a4d6c> in <module>
----> 1 a[0] = 'H'

TypeError: 'str' object does not support item assignment
c = 'hello'
d = c
c += ' there'
print('c:', c, '\nd:', d)
c: hello there 
d: hello

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)
x: [1, 2, 3, [4, 5]] 
y: [1, 2, 3, [4, 5]]

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)
x: [1, 2, 3, 4, 5] 
y: [1, 2, 3, 4, 5]

‘+’ and ‘+=’ operators

What happens when using the ‘+’ operator used on lists?
リストに+演算子を使うとどうなるか?

x = [1, 2, 3]
y = x
x = x + [4, 5]
print('x:', x)
x: [1, 2, 3, 4, 5]

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)
y: [1, 2, 3]

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)
x: [1, 2, 3, 4, 5] 
y: [1, 2, 3, 4, 5]

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)
x: (1, 2, 3, 4, 5) 
y: (1, 2, 3)
スポンサーリンク
スポンサーリンク