目錄
1.認識Python
2.環境與工具
2.1 python環境
2.2 Visual Studio Code編譯
3.代碼示例
4.運行結果
1.認識Python
Python 是一個高層次的結合了解釋性、編譯性、互動性和面向對象的腳本語言。
Python 的設計具有很強的可讀性,相比其他語言經常使用英文關鍵字或標點符號,它具有比其他語言更有特色的語法結構。
2.環境與工具
2.1 python環境
在Windows上使用命令行窗口查看所安裝的python版本
python --version
2.2 Visual Studio Code編譯
Visual Studio Code是一款由微軟開發且跨平臺的免費源代碼編輯器。該軟件以擴展的方式支持語法高亮、代碼自動補全、代碼重構功能,并且內置了命令行工具和Git 版本控制系統。
3.代碼示例
def binsearch(lst, search_value):# 指定查找位置的起始索引low_index = 0# 指定查找位置的結束索引high_index = len(lst) - 1while low_index <= high_index:# 計算中間位置mid_index = (low_index + high_index) // 2# 中間位置的值mid_value = lst[mid_index]# 如果中間位置的值等于要查找的數值if mid_value == search_value:# 返回找到數值的索引值return mid_index# 如果中間位置的值大于要查找的數值elif mid_value > search_value:# 將結束索引值設置為中間位置的索引值減一high_index = mid_index - 1else:# 將起始索引值設置為中間位置的索引值加一low_index = mid_index + 1return None# 定義一個列表
test_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# 要查找的數值
search_value = int(input("請輸入要查找的數值:"))
vret = binsearch(test_list, search_value)if vret is not None:print("你在",test_list,'中查找:', search_value,',找到了,它的索引是:', vret)
else:print('你在',test_list,'中查找:', search_value,',沒有找到。')