投稿

The Zen of Python

Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those! The meaning of the 6th line is as follows. Sparse is better than dense. — In short, “don't try to stick too much code on one line.” [X] Although efficien...

Sequence Types -- str, unicode, list, tuple, buffer, xrange

イメージ
There are six sequence types: strings, Unicode strings, lists, tuples, buffers, and xrange objects. This table lists the sequence operations sorted in ascending priority (operations in the same box have the same priority). In the table, s and t are sequences of the same type; n, i and j are integers: from here

time モジュール & datetime モジュール

import time as t now=t.localtime() print('localtime()') print(now) print() print("t.strftime('%y-%m-%d %H:%M:%S',now)") print(t.strftime('%y-%m-%d %H:%M:%S',now)) import datetime as dt nowdt= dt.datetime.now() print() print('nowdt') print(nowdt) print('nowdt.isoformat()') print(nowdt.isoformat()) nextyear=dt.datetime(2022,1,1) print() print('delta.days') delta=nextyear-nowdt print(delta.days) ==== RESTART: C:/python/time&datetimeTest.py === localtime() time.struct_time(tm_year=2021, tm_mon=2, tm_mday=28, tm_hour=10, tm_min=37, tm_sec=53, tm_wday=6, tm_yday=59, tm_isdst=0) t.strftime('%y-%m-%d %H:%M:%S',now) 21-02-28 10:37:53 nowdt 2021-02-28 10:37:53.398604 nowdt.isoformat() 2021-02-28T10:37:53.398604 delta.days 306 >>>

turtle 多角形描画

イメージ
import turtle as t def takakkei(n,w): p=t.Pen() p.reset() p.up() p.goto(-25,-120) p.down() c=('red','green','pink','aqua','blue') deg=360//n for x in range(1,n+1): z=x%5 p.color(c[z]) p.forward(w) p.left(deg) t.setup(width=300,height=300) t.bgcolor("white") for i in range(3,16): takakkei(i,50) 実行結果 >>> === RESTART: C:/python/turtleDrawTakakkei.py === >>>

pandas csv入力 ピボット集計

import pandas as pd import os import numpy as np path=os.getcwd() print(path) def2=pd.read_csv('c:\python\sample2.csv') print(def2) table=pd.pivot_table(def2,values='price', index=['date'], columns=['item'],aggfunc=np.sum) print() print("ピボット集計 結果") print(table) ==== RESTART: C:/Users/ooshi/AppData/Local/Programs/Python/Python39/pandasCloss.py ==== C:\Users\ooshi\AppData\Local\Programs\Python\Python39 date item price 0 2015-01-01 apple 300 1 2015-01-01 apple 300 2 2015-01-01 orange 250 3 2015-01-02 orange 240 4 2015-01-02 orange 240 5 2015-01-02 banana 260 ピボット集計 結果 item apple banana orange date 2015-01-01 600.0 NaN 250.0 2015-01-02 NaN 260.0 480.0 >>>

pandas データ定義とピボット集計

import pandas as pd import numpy as np df = pd.DataFrame({"A": ["foo", "foo", "foo", "foo", "foo", "bar", "bar", "bar", "bar"], "B": ["one", "one", "one", "two", "two", "one", "one", "two", "two"], "C": ["small", "large", "large", "small", "small", "large", "small", "small", "large"], "D": [1, 2, 2, 3, 3, 4, 5, 6, 7], "E": [2, 4, 5, 5, 6, 6, 8, 9, 9]}) print(df) table = pd.pivot_table(df, values='D', index=['A', 'B'], columns=['C'], aggfunc=np.su...

Pythonのおもな組み込み型の分類表

イメージ
Pythonのおもな組み込み型の分類表 変更不可(イミュータブル、immutable) 値を変更するには、箱ごと変えなければいけない(中身を変えられない箱) 変更可(ミュータブル、mutable) 箱はそのままで、中身の値だけを変えることができる(中身を変えられる箱) 反復可(イテラブル、iterable) 要素を1つずつ返すことができるオブジェクト」です。 わかりやすく言うと、for文のinのうしろに置いて、 1つずつデータを取り出せるオブジェクトです。 シーケンス(sequence) 整数のインデックスを指定して、要素にアクセスできる」データ型です。 組み込み関数len()で要素数(長さ)を取得できます。 文字列(str)もシーケンスです。 マッピング(mapping) 任意のキーで要素を検索できる」データ型です。 シーケンスが整数のインデックスを指定して要素にアクセスするのに対して、 マッピングは任意のキーを使用できます。 辞書(dict)が代表的なマッピング型です。