除法的运算
'/' 无论是否整除返回的都是 float ,暂且叫它精确除法
例如 : 10/5,的到的结果是 2.0
'//' 无论是否整除返回的都是 int ,而且是去尾整除
例如 :5//2,得到的结果是 2
'%' 是取余运算,返回两个余数,经常在判断是否整除上运用
例如 :5%2,得到的结果是 1
向上向下取整(要先导入模块 math )
向上取整
math.ceil()
返回值为 int
向下取整
math.floor()
返回值为 int
四舍五入
内置函数 round()
返回值为 int
import math def binary_search(list,item): low = 0 high = len(list) - 1 #print(high) #print(len(list)) while low <= high: mid = (low + high) / 2 mid = math.floor(mid) guess = list[mid] if guess == item: return mid if guess > item: high = mid -1 else: low = mid + 1 #return None my_list = [1,3,5,7,9] print (binary_search(my_list,7)) print (binary_search(my_list,-1)) ''' 1.定义一个函数,两个参数,列表及要查找的对象 2.初始化要查找的列表下标,初始下标及列表最大长度下标 3.进行遍历,如果最小下标小于等于最大下标,取下标的中间值,如果中间值为小数向下取整 4.如果中间位置的数字等于要查找的数,直接返回。如果要查的数大于中间位置的数, '''