![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
廖雪峰Python习题集
瞄代码的喵
小白学编程
展开
-
【廖雪峰Python习题集】常用内建模块
datetime:假设你获取了用户输入的日期和时间如2015-1-21 9:01:30,以及一个时区信息如UTC+5:00,均是str,请编写一个函数将其转换为timestamp:import refrom datetime import datetime,timezone,timedeltadef to_timestamp(dt_str,tz_str): #str转换原创 2017-11-07 14:15:00 · 1928 阅读 · 0 评论 -
【廖雪峰Python习题集】面向对象高级编程
使用@property:请利用@property 给一个 Screen 对象加上 width 和 height 属性,以及一个只读属性 resolution:#_*_coding:utf-8_*_class Screen(object): @property def width(self): return self._width @width.原创 2017-11-03 13:10:02 · 1130 阅读 · 0 评论 -
【廖雪峰Python习题集】sorted
假设我们用一组tuple表示学生名字和成绩:L=[('Bob',75),('Adam',92),('Bart',66),('Lisa',88)]请用sorted()对上述列表分别按名字排序:#_*_coding:utf-8_*_def by_name(t): return t[0]if __name__ =="__main__": L = [('Bob'原创 2017-11-02 16:45:59 · 1672 阅读 · 0 评论 -
【廖雪峰Python习题集】filter
回数是指从左向右读和从右向左读都是一样的数,例如12321,909。请利用filter()滤掉非回数:def is_palidrome(n): s = str(n) s1 = s[::-1] #字符串反转 return s1 == sif __name__ =="__main__": output = filter(is_palidrome,range原创 2017-11-02 16:24:00 · 485 阅读 · 0 评论 -
【廖雪峰Python习题集】map/reduce
利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字。输入:['adam','LISA','barT‘],输出:['Adam','Lisa','Bart']:#_*_coding:utf-8_*_def convert(s): length = len(s) if length <= 0: raise TypeError原创 2017-11-02 15:52:38 · 622 阅读 · 0 评论 -
【廖雪峰Python习题集】生成器
杨辉三角定义如下:11 11 2 11 3 3 11 4 6 4 11 5 10 10 5 1把每一行看做一个list,试写一个generator,不断输出下一行的list:#_*_coding:utf-8_*_def triangles(n): L=[] for i in range(n): if len(L)<2:原创 2017-11-02 14:57:00 · 1315 阅读 · 0 评论 -
【廖雪峰Python习题集】列表生成式
如果list中既包含字符串,又包含整数,由于非字符串类型没有lower()方法,所以列表生成式会报错:>>> [s.lower() for s in L]Traceback (most recent call last): File "", line 1, in File "", line 1, in AttributeError: 'int' object has no att原创 2017-11-01 22:46:11 · 1500 阅读 · 0 评论 -
【廖雪峰Python习题集】定义函数
请定义一个函数quadratic(a,b,c)接收3个参数,返回一元二次方程:ax2 + bx + c = 0的两个解。提示:计算平方根可以调用math.sqrt()函数:>>> import math>>> math.sqrt(2)1.4142135623730951 #!/usr/bin/python3#_*_coding:utf-8_*_import原创 2017-11-01 22:07:22 · 1159 阅读 · 0 评论 -
【廖雪峰Python习题集】调用函数
请利用Python内置的hex()函数把一个整数转换成十六进制表示的字符串:#_*_coding:utf-8_*_n1 = 255n2 = 1000#_*_coding:utf-8_*_n1 = 255n2 = 1000s1 = hex(n1)s2 = hex(n2)print('%d = %s'%(n1,s1))print('%d = %s '%(n2,s2))原创 2017-11-01 21:58:18 · 905 阅读 · 0 评论 -
【廖雪峰Python习题集】循环
请利用循环一次对list中的每个名字打印出Hello,xxx!#_*_coding:utf-8_*_L=['Bart','Lisa','Adam']#_*_coding:utf-8_*_L = [ 'Bart' , 'Lisa' , 'Adam' ]for x in L: print('Hello, %s !'%x)显示结果如下图所示:原创 2017-11-01 21:49:56 · 5097 阅读 · 0 评论 -
【廖雪峰Python习题集】条件判断
小明身高1.75,体重80.5kg.请根据BMI公式(体重除以身高的平方)帮小明计算他的BMI指数,并根据BMI指数:低于18.5:过轻18.5-25:正常25-28:过重28-32:肥胖高于32:严重肥胖用 if-elif判断并打印结果: #_*_coding:utf-8_*_height = 17.5weight = 80.5bmi = weight...原创 2017-11-01 21:00:48 · 6327 阅读 · 2 评论 -
【廖雪峰Python习题集】使用list和tuple
请用索引取出下面list的指定元素:#_*_coding:utf-8_*_L=[['Apple',Google','Microsoft'],['Java','Python','Ruby','PHP'],['Adam','Bart','Lisa']]#_*_coding:utf-8_*_L = [[ 'Apple' , 'Google' , 'Mic原创 2017-11-01 19:48:31 · 1739 阅读 · 0 评论 -
【廖雪峰Python习题集】字符串和编码
小明的成功从去年的72分提升到了今年的85分,请计算小明成绩提升的百分点,并用字符串格式化显示出‘xx.x%',只保留小数点后1位:#!/usr/bin/python3#_*_coding:utf-8_*_s1 = 72s2 = 85r = (s2-s1)/s1*100print('提升的百分点:%.2f%%'%r)结果如下所示:原创 2017-11-01 18:15:50 · 1534 阅读 · 0 评论