A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 我是色色 于 2017-12-19 10:45 编辑

例1:
有四个数字:1、2、3、4能组成多少个互不相同且无重复的数字的三位数?各是多少?
审题:
1.去重
2.计算总数
程序代码:
方法1:

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:

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:列表

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[0]:
        count += (Profit - BonusList[0])* BonusList[1]
        Profit = BonusList[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。

例3:
一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
审题:
求解,在用代码实现计算。
程序源代码:
方法1:
思想:
1、设n+100=x^2,n+100+168=y^2
2、所n=x^2-100(求n)
3、故x^2-y^2=(x+y)(x-y)=168

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的,然后并集操作得结果。

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 函数返回值的新列表。

例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)
{:8_507:}
Python基础:Python函数




4 个回复

倒序浏览
干货,希望继续更新
回复 使用道具 举报
同意楼上的
回复 使用道具 举报
+1111111很赞

回复 使用道具 举报
55555555555555555555555555555
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马