來學寫程式吧!

程式語言

你聽過幾個?

  • scratch
  • python
  • c++
  • javascript
  • ...

Python 的啦

特色(?

  • 相對比較直觀,容易理解,也好學
  • 物件導向
  • 套件很多,可以執行很多功能
  • 直譯式的語言
  • ...

安裝Python

www.python.org

然後照著安裝就可以了

建議勾選 add python to PATH 喔~

第一支程式碼

打開 Idle

Python 附贈的IDE(整合開發環境)

Hello, world!

print 就是輸出的意思哦

或者你想要開新檔案

剛剛是 shell 的介面

點 new file 開新檔案

小學數學們

變數?

存東西(有可能是數字或其他)的地方

x = 3
print (x)
# output: 3

btw # 號後面是註解喔

(也可以用三個單引號夾住多行的註解)

讓電腦幫你算數學

print(1+1)
print(7122-2217)
print(3*4)
print(14/5) # 除不盡會轉浮點數
print(3**4) # 次方
print(14//5) #除不盡會捨棄小數部分
print(15%4) #取模

型別?

變數可以儲存整數、浮點數(有小數點)、

字串、甚至任何自定義資料結構

型別就是指你的變數內存了哪種東西

x = 7122
print(type(x))
# output: <class 'int'>
y = 'jizz' # 字串用單引號或雙引號夾住
print(type(y))
# output: <class 'str'>

比較一下

x = 3
print (x)
# output: 3
print ("x")
# output: x

輸入

x = int ( input ( "輸入一個整數:" ) )
# int() 用於強制轉型
print (x+3)
# input: 71
# output: 74

玩玩看吧!

清單(List)

將變數們排排站

array = ["Join", "CK", "Infor", 7122]
print(array[0]) # Join
print(array[3]) # 7122

第一項是 array[0],第二項是 array[1] ...

對 同一個清單中可以有不同型別的物件

一些操作們

array = ["Join", "CK", "Infor", 7122]
array.append("8e7orz")
# ["Join", "CK", "Infor", 7122, "8e7orz"]
x = array.pop(0)
# ["CK", "Infor", 7122, "8e7orz"]
print (x)
# output: Join
array.insert(2, "Miku")
# ["CK", "Infor", "Miku", 7122, "8e7orz"]
array.extend([1234, 56])
# ["CK", "Infor", "Miku", 7122, "8e7orz", 1234, 56]

多維清單

array = [["Join", "CK", "Infor"], 7122]
print(array[0])
# output: ['Join', 'CK', 'Infor']
print(array[0][1])
# output: CK

清單裡面也可以塞清單

選擇結構

判斷條件

x = int(input("Enter an integer:"))
if x > 7122:
    print ("x is greater than 7122")
else if x < 7122:
    print ("x is less than 7122")
else if x == 7122: #注意是兩個等號
    print ("jizz")
else:
    print ("error")

用縮排分辨是在 if 的內或外

縮排按 tab (通常idle會幫你)

and / or

x = int(input())
a = int(input())
b = int(input())
if x > 7122 and a > b:
    print ("YA")
if x == 7122 or a == b:
    print("jizz")

練習

輸入兩個正整數,若其中一個大於等於18且一個小於18,或者兩者皆小於16,輸出"FBI OPEN UP",否則輸出"Legal"(不含引號)

(題目敘述 by 8e7)

迴圈

For 迴圈

names = ["Jass921026", "8e7", "Darrell Yu", "Phantom0174"]
for name in names:
    print(("Hello, ", name))
'''
output: 
Hello,  Jass921026
Hello,  8e7
Hello,  Darrell Yu
Hello,  Phantom0174
'''

有了迴圈你就可以做很多事

n = int(input())
# 輸出 0 到 n - 1
for i in range(n):
    print(i)

或是用 While 迴圈

n = int(input())
# 輸出 0 到 n - 1
i = 0
while i < n:
    print(i)
    i += 1

練習

輸入一個整數,判斷是不是質數

練習

輸入71個介於0到22的整數,輸出哪一個數字出現最多次以及出現幾次

(可以配合陣列使用喔)

Python 學習心法

學習資源

  • 各種參考書籍
  • snakify
  • zerojudge
  • google

套件們

Python 有大量的第三方套件

  • 網路爬蟲
  • Pygame
  • 機器學習
  • ......

都可以用 Python 實作

import #套件名稱

放學社課

時間: 每周二18:00-19:00

地點: 電教二(94這裡啦)

內容: Python 小遊戲

講師: 陳炫佑(成功高中)

第[1]堂課

By jass921026

第[1]堂課

  • 587