例1:
有四個數字:一、二、三、4能組成多少個互不相同且無重複的數字的三位數?各是多少?
審題:
1.去重
2.計算總數
程序代碼:
方法1:app
dict=[] for in range(1,5): #i變量賦值 1 2 3 4 for j in range(1,5): for k in range(1,5): if i != j and i != k and j != k: #當變量i不等於變量j,同時變量i不等於變量k,變量j不等於變量k時。條件成立 dict.append([i,j,k]) #追加到列表裏 print("總數量:",len(dict)) #統計數量 print(dict)
方法2:ide
num=[1,2,3,4] count=0 for x in num: for y in num: for z in num: if x != y and x != z and y != z: count +=1 #循環一次累計一次 print(x,y,z) print("總數是>>>",count)
方法3:整合for與if函數
list_num = [1,2,3,4] list = [x*100 + y*10 + z*1 for x in list_num for y in list_num for z in list_num if (x != y) and (x != z) and ( y != z)] print(list,len(list)) #打印不重複三位數及總數
例2:
題目:企業發放的獎金根據利潤提成。利潤(I)低於或等於10萬元時,獎金可提10%;利潤高於10萬元,
低於20萬元時,低於10萬元的部分按10%提成,
高於10萬元的部分,可提成7.5%;20萬到40萬之間時,
高於20萬元的部分,可提成5%;40萬到60萬之間時
高於40萬元的部分,可提成3%;60萬到100萬之間時,
高於60萬元的部分,可提成1.5%,
高於100萬元時,超過100萬元的部分按1%提成,
從鍵盤輸入當月利潤I,求應發放獎金總數?
程序分析:請利用數軸來分界,定位。注意定義時需把獎金定義成長整型。
方法1:列表code
i = int(input('淨利潤:')) Bonus = [1000000, 600000, 400000, 200000, 100000, 0] #利潤 Bonuslist = [0.01, 0.015, 0.03, 0.05, 0.075, 0.1] #利潤對應利率 count = 0 #計數器 for idem in range(0, len(Bonus)): if i > Bonus[idem]: #大於獎勵金額 count += (i - Bonus[idem]) * Bonuslist[idem] #計算 count+本金*提成 print((i - Bonus[idem]) * Bonuslist[idem]) #打印每一個層次獲得提成 i = Bonus[idem] print (count) #提成
方法2:嵌套列表排序
count=0 BonusList=[[100,0.01],[60,0.15],[40,0.03],[20,0.05],[10,0.075],[0,0.1]] Profit= 210000 Profit /=10000 for i in range(0,len(BonusList)): if Profit > BonusList[i][0]: count += (Profit - BonusList[i][0])* BonusList[i][1] Profit = BonusList[i][0] #print(Profit) print(count * 10000)
方法3:字典字符串
num=int(input('請輸入利潤>>> ')) Bonus_dict={100:0.01,60:0.015,40:0.03,20:0.05,10:0.075,0:0.1} keys=Bonus_dict.keys() count=0 for i in keys: if num > i: count += (num - i) * Bonus_dict.get(i) # num =i print("獎金:",count * 10000)
get函數描述
Python 字典 get() 函數返回指定鍵的值,若是值不在字典中返回默認值。
語法:
dict.get(key, default=None)
參數:
key -- 字典中要查找的鍵。
default -- 若是指定鍵的值不存在時,返回該默認值值。
返回值:
返回指定鍵的值,若是值不在字典中返回默認值 None。get
例3:
一個整數,它加上100後是一個徹底平方數,再加上168又是一個徹底平方數,請問該數是多少?
審題:
求解,在用代碼實現計算。
程序源代碼:
方法1:
思想:
一、設n+100=x^2,n+100+168=y^2
二、所n=x^2-100(求n)
三、故x^2-y^2=(x+y)(x-y)=168input
list=[] for x in range(168): for y in range(x): if x**2 - y**2 == 168: x = y**2 -100 list.append(x) print("符合條件的有: ",list)
方法2: 並集
思路:根據公式求x和y的,而後並集操做得結果。it
x=map(lambda n:n**2-100,range(1,100)) y=map(lambda n:n**2-100-168,range(1,100)) print(set(list(x))&set(list(y)))
這裏涉及map函數實用方法。
map() 會根據提供的函數對指定序列作映射。
第一個參數 function 以參數序列中的每個元素調用 function 函數,返回包含每次 function 函數返回值的新列表。io
例4:
題目:輸入某年某月某日,判斷這一天是這一年的第幾天?
思考:特殊狀況年份有閏年
方法1:用list框出閏年與平年的月份時間
#輸入年月日 year=int(input("To new year>>>")) month=int(input("Input Month>>>")) day=int(input("Input Day>>>")) months1=[0,31,60,91,121,152,182,213,244,274,305,335,366] #閏年 months2=[0,31,59,90,120,151,181,212,243,273,304,334,365] #平年 if year%4==0 and year%100!=0 or year%100==0 and year%400==0: YMD=months1[month-1]+day else: YMD=months2[month-1]+day print("是%s年的第%s" %(year,YMD))
方法2:快捷,實用time模塊
import time print(time.strptime('2013-11-24', '%Y-%m-%d')[7])
方法3:calendar日曆模塊
import calendar year = int(input("year: ")) month = int(input("month: ")) day = int(input("day: ")) days = 0 if 0 < month <= 12: _, month_day = calendar.monthrange(year, month) if day <= month_day: for m in range(1, month): _, month_day = calendar.monthrange(year, m) days += month_day days += day print (days)**** else: print( "input day error") else: print ("input month error")
import calendar此模塊的函數都是日曆相關的,例如打印某月的字符月曆。
星期一是默認的每週第一天,星期天是默認的最後一天。更改設置需調用calendar.setfirstweekday()函數。模塊包含了如下內置函數:
序號 函數及描述
1 calendar.calendar(year,w=2,l=1,c=6)
返回一個多行字符串格式的year年年曆,3個月一行,間隔距離爲c。 每日寬度間隔爲w字符。每行長度爲21 W+18+2 C。l是每星期行數。
2 calendar.firstweekday( )
返回當前每週起始日期的設置。默認狀況下,首次載入caendar模塊時返回0,即星期一。
3 calendar.isleap(year)
是閏年返回True,不然爲false。
4 calendar.leapdays(y1,y2)
返回在Y1,Y2兩年之間的閏年總數。
5 calendar.month(year,month,w=2,l=1)
返回一個多行字符串格式的year年month月日曆,兩行標題,一週一行。每日寬度間隔爲w字符。每行的長度爲7* w+6。l是每星期的行數。
6 calendar.monthcalendar(year,month)
返回一個整數的單層嵌套列表。每一個子列表裝載表明一個星期的整數。Year年month月外的日期都設爲0;範圍內的日子都由該月第幾日表示,從1開始。
7 calendar.monthrange(year,month)
返回兩個整數。第一個是該月的星期幾的日期碼,第二個是該月的日期碼。日從0(星期一)到6(星期日);月從1到12。
8 calendar.prcal(year,w=2,l=1,c=6)
至關於 print calendar.calendar(year,w,l,c).
9 calendar.prmonth(year,month,w=2,l=1)
至關於 print calendar.calendar(year,w,l,c)
10 calendar.setfirstweekday(weekday)
設置每週的起始日期碼。0(星期一)到6(星期日)。
11 calendar.timegm(tupletime)
和time.gmtime相反:接受一個時間元組形式,返回該時刻的時間輟(1970紀元後通過的浮點秒數)。
12 calendar.weekday(year,month,day)
返回給定日期的日期碼。0(星期一)到6(星期日)。月份爲 1(一月) 到 12(12月)。
例5:
隨機輸入三個數x、y、z,並把這三個數有小到大排列。
方法1:利用sort函數排序
l=[] for i in range(3): x = int(input("Result>>> ")) l.append(x) l.sort() print(l)
sort() 函數用於對原列表進行排序,若是指定參數,則使用比較函數指定的比較函數。
方法2:使用 列表 sort=,可接受參數 reverse,默認爲布爾值 false,按升序排序;設置爲 true 則按降序排序。
x= int(input("x== ")) y= int(input("y== ")) z= int(input("z== ")) num= [x,y,z] num.sort() # 對列表進行升序排序 print("從小到大爲:",num) rnum = [x,y,z] # 對列表進行降序排序 rnum.sort(reverse=True) print("從大到小爲:",rnum)