python教學
函式
副程式
def Fac(n):
if n > 1:
return n * Fac(n-1)
else:
return 1
print(Fac(5))
car = {"color":"black" , "brand":"honda"}
color = car["color"]
print(color)
新增新的屬性
car["mileage"] = 100
修改一個原本就有的屬性
car["color"] = "white"
刪除一個原本就有的屬性
del car["mileage"]
檢視字典裡有什麼屬性(key)
print(car.keys())
使用.items()讓for迴圈可以遍訪字典
.keys()可以只遍訪屬性(key)
.values()可以只遍訪數值(value)
for key,value in car.items():
print(key,end=" : ")
print(value)
整理字典
sorted()
fav_fruits = {
"alan":"apple",
"jack":"grape",
"rose":"lemon",
"bonny":"banana",
"stan":"apple"
}
for key in sorted(fav_fruits.keys()):
print(key)
去掉重複的
set()
for value in sorted( set( fav_fruits.values() ) ):
print(value)
students ={
"alan":{
"ID":"D0001",
"age":21,
"city":"taipei",
},
"jack":{
"ID":"D0002",
"age":21,
"city":"taichung",
},
"rose":{
"ID":"D0003",
"age":20,
"city":"taichung",
}
}
for name , infomation in st.items():
print(name,":", infomation["ID"] , "," , infomation["city"] )
#人名: ID , city
內容: 很久很久以前,有一個國家叫做「正值國」,這個國家的人做什麼事都非常正直,做人坦蕩蕩。也因此,國家平安和樂、生活富足。
Mar 18, 2025https://github.com/tolgaatam/ColabTurtle
Mar 18, 2025list_a = list(range(1,20,2)) print(list_a) print(list_a[5]) list_a = [1,3,5,7,9,11,13,15,17,19] print(list_a) => [1,3,5,7,9,11,13,15,17,19]
Nov 6, 2022串列
Nov 6, 2022or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up