跳至主要内容

Python

Python超超超入門

Fan Kang Ting


哈哈想必各位,升大一的暑假很無聊,既不能出門(疫情影響),也不能好好玩,只能在家摳.....丁


好


以上言論不代表本人立場​

總之今天來個輕鬆的


你有學過Python嗎?(我印象很多人有自學)​

學過Python 幫我舉右手 學過JavaScript 舉左手(關JS屁事)


Python 3安裝


網站


記得安裝時要加入PATH(環境變數)​

不然...很痛苦 小建議install for ALL User


VScode 延伸模組​


看到推薦安裝的星星​

請建立一個python file main.py​

然後邊做邊學


基本語法​


變數​
a="123"#str
b=123#int
c=123.456#float
d=True #bool

對就是這麼的簡單,那是因為Python是動態強型別的語言,你把甚麼丟給變數,變數就是甚麼~~~


查看變數型態的方法​
type(a)#str
type(b)#int
type(c)#float
type(d)#bool


容器型別​

  1. list []
  2. tuple ()
  3. dictionary {}
  4. set {}

list​

list1 = ["apple", "banana", "cherry"]
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]
type(list1)
<class 'list'>

tuple​

tuple1 = ("apple", "banana", "cherry")
tuple2 = (1, 5, 7, 9, 3)
tuple3 = (True, False, False)

dictionary​

thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict["brand"]) #Ford

Basic I/O​
ans=input("請輸入您的答案")#注意輸入的型態是字串 EX:helloworld
#input() 括弧裡的東西 是輸入時的提示文字
print(ans)#印出helloworld

print補充​

print的輸出方式有很多種 同時可以用逗點來輸出不同型別的參數

print("范綱庭生日: ",910207,"身高:",171.4)

那還有f-string format output等等等 也有print()裡面可以修改的參數 例如sep end 字串格式化文章


range()​

產生序列list

range(n)#建立一個迭代物件 內容是[0,1,2,3....,n-1]
range(n,m)#內容是[n,n+1,n+2....,m-1]
range(n,m,2)#內容是[n,n+2,n+4....,m-1]

Loop​
for variable in range(9):#in 後面要接迭代物件 比如List tuple dictionary
print(variable) #0 1 2 3 4 5 6 7 8
variable = 8
while variable >0:#while 接 condition
print(variable)# 8 7 6 5 4 3 2 1
variable-=1

if elif else​
if variale==0:
print("variable == 0")
elif variable ==1:
print("variable == 1")
else:
print("variable not 1 or 0")

都學會了嗎 要加入函式搂​


define function​
def 函式名稱(參數名稱1,參數名稱2):
return 參數名稱1+參數名稱2

函式名稱(123,456)#579

強制轉換型別​
int("123")#123
str(123)#"123"
float(123)#123.0

實作環節​

創建一個小程式 功能是 使用者輸入N位同學的姓名後再輸入三個科目成績 分別是 國文,英文,數學成績(使用函數)

創建一個function 每科>=60才能拿到畢業證書 有一顆<60 直接21 其中一科 拿90以上 得到獎學金不累計(只判斷有沒有而已)


Input Example​

2 //同學有N位
張三
90
80
70
李四
50
40
70

Output Example​

張三:獲得畢業證書
張三:有獎學金
李四:21

還是太簡單?​

額外題目