タグ

ブックマーク / www.python-izm.com (2)

  • 乱数値の取得

    Pythonで乱数値(ランダムな値)を取得する方法です。 random randomモジュールをにはさまざな乱数値の取得方法があります。用途に応じて適切なものを選ぶとよいでしょう。 import random print(random.random()) print(random.uniform(1,100)) print(random.randint(1,100)) print(random.choice('1234567890abcdefghij')) sample_list = ['python', 'izm', 'com', 'random', 'sample'] random.shuffle(sample_list) print(sample_list) ※実行するごとに異なる出力結果となります。 0.667824083865 70.6645798726 74 2 ['com',

    乱数値の取得
    reboot_in
    reboot_in 2018/04/03
  • インポート

    Pythonのインポート(import)は標準ライブラリのインポートはもちろん、自ら作成したモジュールのインポートも行うことができます。 import, from まずは簡単なモジュールファイルを用意します。下記コードをtestmod.pyという名前で作業ディレクトリに保存してください。 class TestClass: def __init__(self): print('create TestClass') def test_method(self, val): print('call test_method') print(val) 次は上記モジュールを実行(インポート)するコードを記述します。Pythonはコード中のどこにimport、fromを記述してもエラーにはなりません。 import testmod test_class_1 = testmod.TestClass() te

    インポート
    reboot_in
    reboot_in 2017/06/11
    “from testmod import TestClass”
  • 1