LoginSignup
0
1

More than 3 years have passed since last update.

値渡しと参照渡し

Posted at
#参照渡し
i = [1,2,3,4,5]
j = i
j[0] = 100 #jを書き換えるとiも書き換わる
print('j=', j)
print('i=', i)

x = [1, 2, 3, 4, 5]
y = x.copy()
y[0] = 100 #この場合はxは書き換わらない
print('y=', y)
print('x=', x)

#値渡し
X = 20
Y = X
Y = 5
print(X)
print(Y)

実行結果:

j= [100, 2, 3, 4, 5]
i= [100, 2, 3, 4, 5]
y= [100, 2, 3, 4, 5]
x= [1, 2, 3, 4, 5]
20
5
0
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1