• Skip to main content
  • Skip to primary sidebar

学習記録

python3

グローバル変数の設定

2018年9月7日 by 河副 太智 Leave a Comment

自作関数の外部、内部のどちらで変数を宣言するかによって
使用できる範囲が異なる

1
2
3
4
5
6
name = "max"
def get_name():
    name = input("Your name:")
    
get_name()
print(name)

1
2
3
>>>
Your name:Michael
max

 

上記の例では自作関数の外部でname=maxが宣言されているので
自作関数内部で別の変数が代入されたとしてもそれは
自作関数内部でのみ有効となるのでコードの最後でprintで変数name
を呼び出しても最初に宣言したmaxが表示される事になる

 

コードの最後のprintの部分で自作関数内で設定された変数の値を
反映させるには自作関数内部でglobal nameと記述し、
変数nameはグローバル関数と宣言する
そうすればコードの最後の部分のprintでは自作関数内で上書きされた
変数の内容が反映される事になる

1
2
3
4
5
6
7
name = "max"
def get_name():
    global name
    name = input("Your name:")
    
get_name()
print(name)

アウトプット
1
2
3
>>>
Your name:Michael
Michael

 

Filed Under: python3

パスが正しいかを調べる

2018年5月7日 by 河副 太智 Leave a Comment

パスにファイルかフォルダーが存在すればTrue
無ければFalse

1
2
3
import os
if not os.path.exists('./assets/nyc_taxi.csv'):
    print('Taxi dataset not found.')

パスにファイルかフォルダーが存在し、かつ
ファイルであればTrueそうでなければFalse

1
2
3
import os
if not os.path.isfile('./assets/nyc_taxi.csv'):
    print('Taxi dataset not found.')

 

Filed Under: python3

from PIL import Imageから出てくるエラー文 ”Pillow or PIL”

2018年4月14日 by 河副 太智 Leave a Comment

from PIL import Imageから出てくるエラー文
ImportError: The _imaging extension was built for another version of Pillow or PIL
で長時間引っかかった

調べてみるとpython 3.6 自体に問題があるようで
C:\Anaconda\lib\site-packages\PIL\Image.pyの

1
2
3
if PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None):
raise ImportError("The _imaging extension was built for another " "
version of Pillow or PIL")

の部分を

1
2
3
if <strong><span style="color: #ff0000;">core.</span></strong>PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None):
raise ImportError("The _imaging extension was built for another " "
version of Pillow or PIL")

に変更したら直りました。

Filed Under: python3 Tagged With: PIL, Pillow, The _imaging extension was built for another version of Pillow or PIL

プログラミング作業休憩タイマー

2018年4月10日 by 河副 太智 Leave a Comment

プログラミングをやりすぎると首、肩がこってパフォーマンスが最悪になるので
一定時間経つと音を鳴らして、その間に目を閉じてリラックスするようにした

10分作業したら休憩開始の音
2分休憩したら作業開始の音

これを10回繰り返したらタイマー終了

1
2
3
4
5
6
7
8
9
10
11
12
import winsound as ws
from time import sleep
 
a = 0
while a < 10:
    sleep(600)
    ws.Beep(523, 500)
    sleep(120)
    ws.PlaySound( 'SystemAsterisk', ws.SND_ALIAS )
    a += 1
    
ws.PlaySound( 'SystemExclamation', ws.SND_ALIAS )

作業音の種類はこちらを参照させていただきました

Filed Under: python3

複数言語のチートシート

2018年2月13日 by 河副 太智 Leave a Comment

複数言語のチートシート

Filed Under: python3

Python3チートシート

2018年2月13日 by 河副 太智 Leave a Comment

Python3チートシート

その他チートシート多数

Filed Under: python3, チートシート

  • « Go to Previous Page
  • Page 1
  • Page 2
  • Page 3
  • Page 4
  • Page 5
  • Page 6
  • Interim pages omitted …
  • Page 12
  • Go to Next Page »

Primary Sidebar

カテゴリー

  • AWS
  • Bootstrap
  • Dash
  • Django
  • flask
  • GIT(sourcetree)
  • Plotly/Dash
  • VPS
  • その他tool
  • ブログ
  • プログラミング
    • Bokeh
    • css
    • HoloViews
    • Jupyter
    • Numpy
    • Pandas
    • PosgreSQL
    • Python 基本
    • python3
      • webアプリ
    • python3解説
    • scikit-learn
    • scipy
    • vps
    • Wordpress
    • グラフ
    • コマンド
    • スクレイピング
    • チートシート
    • データクレンジング
    • ブロックチェーン
    • 作成実績
    • 時系列分析
    • 機械学習
      • 分析手法
      • 教師有り
    • 異常値検知
    • 自然言語処理
  • 一太郎
  • 数学
    • sympy
      • 対数関数(log)
      • 累乗根(n乗根)
    • 暗号学

Copyright © 2025 · Genesis Sample on Genesis Framework · WordPress · Log in