### ex3-1 ### 下載與安裝 Thonny https://thonny.org ### ex3-2 ### >>> print("Hello World") ### ex3-3 ### print("Hello World") ### ex3-4 ### import time from machine import Pin led = Pin(15, Pin.OUT) led.value(1) time.sleep(3) led.value(0) ### ex3-5 ### >>> "Hello World!".upper() # 使用字串物件 "Hello World!" 的 upper() 方法,將字串轉成大寫 >>> "Hello World!".find('r') # find() 方法尋找 'b' 的位置 (從 0 開始) >>> "Hello World!".replace('r', 'u') # replace() 方法將所有的 'r' 取代成 'b' ### ex3-6 ### >>> 111 + 111 # 整數物件相加 >>> "111" + "111" # 字串物件串聯 ### ex3-7 ### >>> 111 + "111" # 不同型別的資料相加發生錯誤 ### ex3-8 ### >>> n1 = 123456789 # 將整數物件 123456789 指派給變數 n1 >>> n2 = 987654321 # 將整數物件 987654321 指派給變數 n2 >>> n1 + n2 # 實際上是 123456789 + 987654321 ### ex3-9 ### >>> print("abc") # 顯示字串物件 >>> print("abc".upper()) # 顯示字串物件.方法的執行結果 >>> print(111 + 111) # 顯示整數物件運算的結果 ### ex3-10 ### >>> import time # 匯入時間相關的 time 模組 >>> time.sleep(3) # 執行 time 模組的 sleep() 函式,暫停 3 秒 >>> from time import sleep # 從 time 模組裡匯入 sleep() 函式 >>> sleep(5) # 執行 sleep() 函式,暫停 5 秒 ### ex3-11 ### # 暫停 5 秒後,印出 Hello World! from time import sleep sleep(5) print("Hello World!") ### ex3-12 ### # The CP210x USB to UART Bridge Virtual COM Port (VCP) drivers for ESP32 https://www.silabs.com/developers/usb-to-uart-bridge-vcp-drivers ### ex3-13 ### >>> from machine import Pin # 從 machine 模組匯入 Pin 物件 >>> led = Pin(2, Pin.OUT) # 建立 2 號腳位的 Pin 物件,設定為輸出腳位,並命名為 led >>> led.value(1) # 高電位,點亮 LED >>> led.value(0) # 低電位,關閉 LED ### ex3-14 ### from machine import Pin # 從 machine 模組匯入 Pin 物件 import time # 匯入時間相關的 time 模組 led = Pin(2, Pin.OUT) # 建立 2 號腳位的 Pin 物件,設定為輸出腳位,並命名為 led while True: # 一直重複執行 led.value(1) # 高電位,點亮 LED time.sleep(0.5) # 暫停 0.5 秒 led.value(0) # 低電位,關閉 LED time.sleep(0.5) # 暫停 0.5 秒