《笨辦法學Python》 第18課手記 本節課將建立函數,跟C語言裡面的的定義函數差不多。 作者在每個函數開頭都加瞭解釋該函數功能的英文注釋。為了方便理解,我將在原代碼後直接解釋涉及到知識點的部分代碼。這裡說的定義函數,就是作者所說的建立函數的意思。 經過部分修改的原代碼如下: # this one is like your scripts with argv 第一個建立的函數類似上一節的argvdef print_two(*args):
基礎操作: ##########################列表#############################aList = [1,2,3,4,5]#python 中 list 是可變類型,像下面這樣修改是合法的#增aList.append(6)#刪aList.pop()#改aList[0] = 100#查print(aList[0])for v in aList:
21、python list 列表中可以用的swap方法 mylist[i], mylist[j] = mylist[j], mylist[i] 這樣就可以swap兩個元素了 還有 a ^= b b ^= a a ^= b 22、python取整及保留小數 #encoding:utf-8import math#向上取整print math.ceil(2.6)3.0#向下取整print math.floor(2.6)2.0#四捨五入print
16. 3Sum Closest【Medium】 Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For
根據成績高低,計算排名 1.利用zip將字典資料轉化元組 from random import randintd = {x: randint(60, 100) for x in 'abcdef'}print dprint sorted(d)print zip(d.itervalues(), d.iterkeys())print sorted(zip(d.itervalues(), d.iterkeys())) 2.傳遞sorted函數的key參數