ãªã¹ãå½¢å¼ã¯ããã°ã©ã è¨èªã®Pythonã«ããã¦ãã¼ã¿ãé åã¨ãã¦æ±ãå½¢å¼ã§ããPythonãå¦ã¶ä¸ã§éè¦ãªåºç¤ç¥èã®ã²ã¨ã¤ã¨ãªãã¾ãããã ãåå¦è ã«ã¨ã£ã¦å¤å°ã¤ã¾ãããããã¨ãããããã¾ãã®ã§ãarange()ãmatplotlibãçµã¿åãããç°¡åãªå©ç¨ä¾ã¨ã¨ãã«ããã¤ã³ããã¾ã¨ãã¦ããã¾ããç®æ¬¡ãè¦ã¦ãèªåã«å¿ è¦ãªã¨ããã ãèªãã§ããã ãã¦ãæ§ãã¾ããï¼å®è¡ããã¨ãã¯åºæ¬çã«å é ãããã£ã¦ãã ããï¼ãå®è¡ç°å¢ã¯Python3ã§ãã
- ãªã¹ãã®ä½æ
- è¦ç´ ã®è¿½å
- è¦ç´ ã®è¡¨ç¤º
- æ°å¤ã®ãªã¹ã
- èªåæ¡çªã§ãªã¹ããä½ã
- 空ã®ãªã¹ããä½ã
- ãªã¹ãã«å ¥ããrange()ã®æ°å¤ããã°ã©ããæãã¦ã¿ã
- 空ã®ãªã¹ããä½æãã¦ãè¨ç®çµæãé次追å ããã°ã©ãã§å¯è¦åãã¦ã¿ã
- ãªã¹ãã®è¦ç´ æ°ã®æå®
- ãªã¹ãå士ã®é£çµ
- ãªã¹ãã®ä¸ã®è¦ç´ ãã½ã¼ããã
- ãªã¹ãã®ä¸ã®è¦ç´ ãåé¤ãã
- æå¾ã«
ãªã¹ãã®ä½æ
ãªã¹ããä½æããã«ã¯ã以ä¸ã®ããã«å¤æ°ã[]ã®ä¸ã«ä¸¦ã¹ã¾ãã
data1 = ["a","b","c","d","e"] print(data1) ['a', 'b', 'c', 'd', 'e']
è¦ç´ ã®è¿½å
ãªã¹ãã¸ã®å¤ã®è¿½å ã¯ãappend()ã¨ããã¡ã½ãããå©ç¨ã§ãã¾ãã
data1.append("f") print(data1) ['a', 'b', 'c', 'd', 'e', 'f']
è¦ç´ ã®è¡¨ç¤º
ãªã¹ãã®ä¸ã®ä½çªç®ã®è¦ç´ ãªã®ãæ°åã§æå®ãã¦ããã®è¦ç´ ã表示ãããã¨ãå¯è½ã§ãããã ãããã®ãããªè¦ç´ ã®æå®ã¯1ããã§ã¯ãªã0ããå§ã¾ããã¨ã«æ³¨æãã¦ãã ãããã¤ã¾ããä¸ã¤ç®ã®è¦ç´ ã¯ãªã¹ãå[0]ã§ãäºã¤ç®ã®è¦ç´ ã¯ãªã¹ãå[1]ã§æå®ãã¾ãã
print(data1[0]) a print(data1[4]) e
ç¯å²ã§æå®ãã¦è¡¨ç¤ºããããã¨ãã§ãã¾ãã
print(data1[0:2]) ['a', 'b']
æ°å¤ã®ãªã¹ã
æ°å¤ãã¼ã¿ããªã¹ãã«ã§ãã¾ãã
data2 = [1,2,3,4,5] print(data2) [1, 2, 3, 4, 5]
èªåæ¡çªã§ãªã¹ããä½ã
ãªã¹ãã¨çµã¿åããã¦ãã使ããã®ã«range()ãããã¾ããrangre()ã使ãã¨ããã¨ãã°ã1ãã10ã¾ã§ã®æ°åãèªåçæãã¦ãªã¹ããä½ãã¾ãããããã£ã¦ãåè¿°ãããããªãã¡ãã¡å¤ã並ã¹ã¦ä»£å ¥ããå¿ è¦ããªããªãã¾ãã以ä¸ã®ããã«ä½¿ãã¾ãã
data3 = range(10)
ãã ãããã®é ååããã®ã¾ã¾print()æã§æå®ãã¦ãã以ä¸ã®ããã«åºåããã¦è¦ç´ ã¯ç´æ¥è¡¨ç¤ºããã¾ãããããã¯Pythonã®ä»æ§ã§ãã
print(data3) range(0, 10)
ãã£ã¦ãrange()ã§ä½æããè¦ç´ ãåãåºãã«ã¯ã以ä¸ã®ããã«çªå·ã§è¦ç´ ãç´æ¥æå®ããããforã§ã«ã¼ããåãã¦åãåºãå¿ è¦ãããã¾ãã
print(data3[7]) 7
for i in data3: print(i) 0 1 2 3 4 5 6 7 8 9
空ã®ãªã¹ããä½ã
æåããå¤ãå ¥ããã®ã§ã¯ãªããã¨ããããä½ãå ¥ã£ã¦ããªã空ã®ãªã¹ããä½ã£ã¦ãããã¨ãã§ãã¾ããããã¯ãè¦ç´ ãå¾ã§ãã®ãªã¹ãã«è¿½å ãã¦ãããããªç¨éã«ããã¦ä¾¿å©ã§ãã以ä¸ã®ããã«å®£è¨ãã¾ããã©ãããã¨ãã«ãã®æ©è½ãå½¹ã«ç«ã¤ã®ãã¯å¾ã§å ·ä½ä¾ã示ãã¾ãã
data4 = []
print(data4)
[]
ãªã¹ãã«å ¥ããrange()ã®æ°å¤ããã°ã©ããæãã¦ã¿ã
æ°å¤ããªã¹ãã«å
¥ãã¦æ±ãã¨ããããã°ã©ãã§å¯è¦åããç¨éã«ä½¿ãã¾ããmatplotlibã使ã£ã¦ãrange()ã§çæãããªã¹ãããã°ã©ããæãã«ã¯ã以ä¸ã®ããã«ã§ãã¾ãã
ã¡ãªã¿ã«ãmatplotlibã¯è»¸ãã©ãã«ãã°ãªãããªã©åãã°ãããã§ãã³ã¼ããå¢ãã¦ãæ¤ç´¢ã§ããããããµã³ãã«ããã®ãããªãã®ãå¤ãã¦é¢åã¨ããå°è±¡ãããããããã¾ãããããããå®ã¯ã¨ããããå¯è¦åãã¦ã¿ãã¨ãããã¨ã ãã«éããªãããã£ãããã ãã®ã³ã¼ãéã§ãæ¸ãã¦ãã¾ãã®ã§ãã
from matplotlib import pyplot as plt x = range(100) plt.plot(x, x)
matplotlibã¯ãã®ããã«ãã¼ã¿ã®å¯è¦åã«æçãªãã¼ã«ãªã®ã§ããããã使ãã®ç°å¢ã§matplotlibãã¤ã³ã¹ãã¼ã«ããã¦ããªãå ´åã¯ãããããã®ç°å¢ã«å¿ãã¦pip install ãconda install ã§ã¤ã³ã¹ãã¼ã«ãã¦è©¦ãã¦ã¿ã¦ãã ããã
range()ã«ããæ°å¤çæã§ã¯ã大ããªæ°ããå°ããæ°ã«åãã£ã¦æ°å¤ãçæãããã¨ãå¯è½ã§ãã
from matplotlib import pyplot as plt x = range(100, 0, -1) y = range(0, 100, 1) plt.plot(x, y)
空ã®ãªã¹ããä½æãã¦ãè¨ç®çµæãé次追å ããã°ã©ãã§å¯è¦åãã¦ã¿ã
ããã«ç°¡åãªå¿ç¨ã¨ãã¦ãã¹ãä¹è¨ç®ãè¡ã£ã¦ã°ã©ããæãã¦ã¿ã¾ããå¤æ°yã«ã¤ãã¦ã¯ãæåã«y = []ã§åæåãã¦ç©ºã®ç¶æ ã®ãªã¹ããä½ããforã«ã¼ãã®ä¸ã§append()ã使ã£ã¦è¨ç®çµæãããã«è¿½å ãã¦ãã¾ãã
from matplotlib import pyplot as plt x = range(100) y = [] for i in x: y.append(i**2) plt.plot(x, y)
ãªã¹ãã®è¦ç´ æ°ã®æå®
ãã®ãªã¹ãã«è¦ç´ ãããã¤å ¥ã£ã¦ããããæ ¼ç´ããã¦ããè¦ç´ æ°ã調ã¹ãã«ã¯len()ã使ãã¾ãã
data5 = ["apple","orange","banana","grape","kiwi"] len(data5) 5
ãªã¹ãå士ã®é£çµ
ãªã¹ãå士ãã + ãã§é£çµãããã¨ãå¯è½ã§ããããã¯ç´æçã«ç解ããããã§ããã
data5 = ["apple","orange","banana","grape","kiwi"] data6 = ["pineapple", "strawberry", "cherry"] data7 = data5 + data6 print(data7) ['apple', 'orange', 'banana', 'grape', 'kiwi', 'pineapple', 'strawberry', 'cherry']
ãªã¹ãã®ä¸ã®è¦ç´ ãã½ã¼ããã
ãªã¹ãã®ä¸ã®è¦ç´ ãã½ã¼ããã¦ä¸¦ã¹ããã¦åºåãããã¨ãã§ãã¾ãã
print(sorted(data7)) ['apple', 'banana', 'cherry', 'grape', 'kiwi', 'orange', 'pineapple', 'strawberry']
ããã©ã«ãã¯æé ã§ããã以ä¸ã®ããã«æå®ãããã¨ã§ãéé ã«ä¸¦ã¹æ¿ãã¦åºåãããã¨ãã§ãã¾ãããã¡ãã¯ããªãã·ã§ã³ã¨ãã¦ãreverse=Trueããå ãããã¨ããã¤ã³ãã§ãã
print(sorted(data7, reverse=True)) ['strawberry', 'pineapple', 'orange', 'kiwi', 'grape', 'cherry', 'banana', 'apple']
ãªã¹ãã®ä¸ã®è¦ç´ ãåé¤ãã
ãªã¹ãã®ä¸ã§æå®ããç¯å²ã®è¦ç´ ãåé¤ãããã¨ãå¯è½ã§ãããdelãã¯deleteï¼åé¤ï¼ã§ããã
print(data7) ['apple', 'orange', 'banana', 'grape', 'kiwi', 'pineapple', 'strawberry', 'cherry'] del data7[2:5] print(data7) ['apple', 'orange', 'pineapple', 'strawberry', 'cherry']
æå¾ã«
ãªã¹ãã¯Pythonã§ä½¿ãåºæ¬çãªé
åæ©è½ãªã®ã§ãåå¿è
ã®ã¨ãã«è¦ããå¿
é ã®ç¥èã®ã²ã¨ã¤ã«ãªãã¾ããç¨éã¯åºããä¸åº¦è¦ããã¨ãã¨ã¦ã便å©ã§ããä»ã«ãããã¤ãæ©è½ã¯ããã¾ãããã¾ãã¯ããã§ç´¹ä»ãããããªãã®ã使ãããªããããã«ããã¨ããã§ãããã次ã¯ãè¾æ¸åã«ã¤ãã¦ãä¸æ°ã«èª¬æãã¾ãã
coldsnap.hatenablog.jp