Python 首頁 | Function | 檔案處理 | 模組 |
參見 Python初學 資料形態 串列 串列操作 字典 排序
score = int(input("請輸入成績")) if score >= 60: print("成績及格!") else: print("成績不及格!")
x = 3.14 print(x) print(type(x))
a = True print(a) print(type(a)) b = False print(b)
song = "Hello, Goodbye" print(song) print(type(song)) str_len = len(song)
# 字串連接 str1 = "Lucy in the sky" str2 = " with diamonds" str = str1 + str2 print(str)
# 字串 slicing str1 = "Python" str2 = str[1:4] # yth (1~3) str3 = str[4] # o print(str)
my_list.append(object)
my_list.insert(position, object)
my_list.extend(list_2)
my_list.remove(object)
my_list.pop() # 移除這個 list 的最後一個元素
my_list.sort() # 排序 list
my_list.sort(reverse=True) # 遞減排序 list
sorted_list = sorted(my_list) # 取得排序過的資料
max_item = max(my_list) # 最大值
min_item = min(my_list) # 最小值
total = sum(my_list) # 總和
element in my_list # 一個元素是否存在於 list 中
list_str = 'seperateStr(要用來隔開的符號)'.join(my_list) # 把 list 轉換為字串
dangerous = ['Jam', 'In the Closet', 'Remember the Time',"Heal the World","Black or White","Who Is It","Give In to Me", "Dangerous"] dangerous_str = ', '.join(dangerous) print(dangerous_str) new_dangerous_list = dangerous_str.split(", ") print(new_dangerous_list)
new_list = str.split() # 把字串用空白隔開的值切成 list
new_list = str.split(my_list) # 把字串用逗號隔開的值切成 list
theBeatles = ['John Lennon', 'Paul McCartney', 'Ringo Starr', 'George Harrison'] for i in theBeatles: print(i) print(theBeatles[0]) # 第一個 print(theBeatles[-1]) # 最後一個
lst = [int(num) for num in input().split() # 把 list 中的數字都存成整數的形態
dict_1 = {}
dict_2 = dict()
movie_1 = {'name': 'Saving Private Ryan', 'year': 1998, 'director': 'Steven Spielberg'} movie_2 = dict(name='The Breakfast Club', year=1985, director='John Hughes') movie_3 = {'name': 'Catch Me If You Can', 'year': 2002, 'director': 'Steven Spielberg'} print(movie_1['name']) # 執行後會得到 Saving Private Ryan print(movie_1.get("cast")) # 如果key不存在的話,程式會回傳None,不會出現錯誤訊息 print(movie_1.get("cast", "not found")) # 可以自己設定空訊息 print(movie_2['year']) #執行後會得到 1985 print(movie_3['director']) #執行後會得到 Steven Spielberg
# 產生或列印字典中的鍵值對
for key, value in dict.items(): print(key, value)
check = 'name' in() movie_1 # 檢查指定的 key 是否存在於字典中