随笔分类 - python 学习知识点睛
摘要:如果list中既包含字符串,又包含整数,由于非字符串类型没有lower()方法,所以列表生成式会报错: 使用内建的isinstance函数可以判断一个变量是不是字符串: 请修改列表生成式,通过添加if语句保证列表生成式能正确地执行: 答案: L1 = ['Hello', 'World', 18, '
阅读全文
摘要:1,输出100以内的奇数 法一: n=0while n<100: n=1+n if n % 2 == 0: continueprint(n) print('end') 法二: def even(): n=0 while n < 100: n +=1 if n % 2 == 1: print(n)ev
阅读全文
摘要:练习 1, 请利用Python内置的hex()函数把一个整数转换成十六进制表示的字符串: n1 = 255 n2 = 1000 print(???) 答案: n1 = 255n2 = 1000print(str(hex(n1))+'\n'+str(hex(n2))) print((hex(n1)),
阅读全文
摘要:1) 练习 小明身高1.75,体重80.5kg。请根据BMI公式(体重除以身高的平方)帮小明计算他的BMI指数,并根据BMI指数: 低于18.5:过轻 18.5-25:正常 25-28:过重 28-32:肥胖 高于32:严重肥胖 用if-elif判断并打印结果: 答案: height=1.75wei
阅读全文
摘要:(1) 练习 请用索引取出下面list的指定元素: 1, 2, 答案:1, L = [ ['Apple', 'Google', 'Microsoft'], ['Java', 'Python', 'Ruby', 'PHP'], ['Adam', 'Bart', 'Lisa']] print("L=",
阅读全文
摘要:1),在Python中,采用的格式化方式和C语言是一致的,用%实现,看下例: 这里保存为了1,shuchu.py 文件 print('%2d-%02d' % (3, 1)) print('%.2f' % 3.1415926) 运行结果: 2), 方法二: format() 练习: 小明的成绩从去年的
阅读全文
摘要:>>> n = 123 >>> f = 456.789 >>> s1 = 'hello ,world' >>> s2 = 'hello, \'adam\'' >>> s3 = r'hello, "bart"' >>> s4 = r'''hello, lisa!''' >>> print(n,f,s1
阅读全文

浙公网安备 33010602011771号