More Related Content Similar to 天気予報ガジェットを作りな がら、Pythonを勉強しよう (20) 天気予報ガジェットを作りな がら、Pythonを勉強しよう9. Raspberry Piのラインナップ
• Raspberry Pi:初代ラズパイ
• Raspberry Pi 2:CPU4コア+メモリ1GBで
性能強化したラズパイ
• Raspberry Pi Zero:5ドルの小さなラズパイ
• スペックは以下を参照。
https://ja.wikipedia.org/wiki/Raspberry_Pi
17. 変数と型
• x = 4
• y = “Python”
• z = True
• x = “Test” + 5
• y = “Test” + “Python”
19. 条件比較
• 2 == 2, 3 != 10
• “Python” == “Ruby”
• 3 >= 10, 3 < 10
• not x == 2
• “P” in “Python”, “1” not in “Python”
• x > 3 and y > 2, x > 3 or z < 2
20. 条件分岐とインデント
• if x >= 20:
print(“large”)
elif x > 5:
print(“medium”)
else:
print(“small”)
• if “Python” in “PythonPython”:
print(“true”)
• if x > 10 and y > 5:
print(“Large”)
21. リスト
• list = [ 10, 20, 30, “Python”, “RasPi” ]
list[1], list[-1]
len(list)
list[2:4], list[-4:-2], list[1:]
• list.append(100)
• list.remove(“Python”)
• list.reverse()
• numbers = [ 5, 3, 10, 2]
numbers.sort()
max(numbers), min(numbers)
22. 辞書型
• a = { ‘First’:”Python”, ‘Second’:”Raspberry Pi” }
a[‘First’]
• b = {‘First’:”Python”, “Second”:[1,2,3,4,5]}
b[‘Second’][2]
23. ループ
• for member in list:
print(member)
• for i in range(10):
print(i)
• x = 0
while x < 10:
print(x)
x = x + 1
24. 関数
• def sum(a, b):
return a + b
sum(10, 20)
• def multiple(a,b=10):
return a*b
multiple(5)
multiple(5,4)
28. PythonでLチカしてみよう
#!/use/bin/python3
# -*- coding: utf-8 -*-
# モジュールのロード
import time
import RPi.GPIO as GPIO
# GPIOの設定
GPIO.setmode(GPIO.BCM)
# 出力LEDのGPIOピン番号
GPIO_LED = 17
# GPIOのピン番号を出力に設定
GPIO.setup(GPIO_LED, GPIO.OUT)
try:
while(1):
GPIO.output(GPIO_LED, GPIO.HIGH)
time.sleep(1)
GPIO.output(GPIO_LED, GPIO.LOW)
time.sleep(1)
except KeyboardInterrupt:
pass
# GPIOの終了処理
GPIO.cleanup()