python3

python3 でグローバル変数とローカル変数で同じ名前の変数を使う

投稿日:2020年12月4日 更新日:

グローバル変数とローカル変数で同じ名前の変数を使うことができる。

x = 10

def myfunc():
    x = 20
    print('関数内: x = '+str(x))

myfunc()
print('外側: x = ' + str(x))

結果

関数内: x = 20
外側: x = 10

-python3
-

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

matplotlib で、凡例を表示する方法

データに label を関連付け、plt.legend() で凡例を表示する。 例 from matplotlib import pyplot as plt data_x = [25,26,2 …

no image

matplotlib で、縦横の線(グリッド)を描く方法

pyplot grid で縦横線(罫線のようなもの)を引くことができる。 例 from matplotlib import pyplot as plt data_x = [25,26,27] …

no image

matplotlib で、グラフの線の幅を変更する方法

lw で、線幅を数字で指定すればよい。 例 from matplotlib import pyplot as plt data_x = [25,26,27] data_y = [39, …

no image

python3 で、配列の配列をソートする

ラムダ式を使って、任意の要素についてソートすることができる。 例 arr = [[1,2,3],[2,1,2],[3,3,4],[4,4,1]] for i i …

no image

matplotlib で、x軸・y軸の目盛りを反対方向につけたい場合。

デフォルトでは、x軸はグラフの下に、y軸は左側につけられる。 例 import numpy as np from matplotlib import pyplot as plt x = np.lins …