程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Mathematical function of Python built-in function ---divmod()

編輯:Python

function : divmod() Function to combine the result of divisor and remainder operation , Returns a tuple containing quotient and remainder (a//b, a%b)

grammar :divmod(a, b)

give an example 1. Calculate slave parameters N Start , Every number and parameter decreasing downward A The result value of the divisor and remainder of . The code is as follows :

def getDivmod(A, N):
while N >= 0:
sn = str(N)
sa = str(A)
result = ['divmod({0}, {1}):'.format(sn, sa), str(divmod(N, A))]
print(''.join(result))
N = N -1

call getDivmod() Function and pass in parameter data , The code is as follows :

getDivmod(3, 10)
The operation results are as follows :
divmod(10, 3):(3, 1)
divmod(9, 3):(3, 0)
divmod(8, 3):(2, 2)
divmod(7, 3):(2, 1)
divmod(6, 3):(2, 0)
divmod(5, 3):(1, 2)
divmod(4, 3):(1, 1)
divmod(3, 3):(1, 0)
divmod(2, 3):(0, 2)
divmod(1, 3):(0, 1)
divmod(0, 3):(0, 0)

2. Simulation Implementation of data paging calculation method . The code is as follows :

def getSegment(curIndex, getSize):
abc = ['a','b', 'c', 'd', 'e', 'f', 'g','h','i', 'j', 'k', 'l', 'm','n','o', 'p', 'q', 'r', 's', 't','u','v', 'w', 'x', 'y', 'z']
startGet = (curIndex-1)*getSize
getData = abc[startGet:curIndex*getSize]
totalIndexTuple = divmod(len(abc), getSize)
totalIndex = totalIndexTuple[0] + (1 if totalIndexTuple[1] > 0 else 0)
return (getData, totalIndex)

call getSegment() Function and pass the current page number and the total number of entries per page , Return the data result and the total number of pages , The code is as follows :

result = getSegment(2, 5)
print(result[0])
print(result[1])
Output results :
['f', 'g', 'h', 'i', 'j']
6

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved