1. 计算100-300之间所有能被3和7整除的所有数之和
# -*- coding: UTF-8 -*- #blog:http://www.cnblogs.com/linux-chenyang/ count = 0 i=100 while i <= 300: if i % 3 ==0 or i % 7 ==0: count = count+i i += 1 print(count)
2. 定义函数统计一个字符串中大写字母, 小写字母, 数字的个数, 并返回结果
def counts_str_type(str): ''' str必须为'0~9,A~Z,a~z' :param str: :return: ''' number = [] upper = [] lower = [] for i in str: if ord(i) >= 48 and ord(i) <= 57: number.append(i) elif 65 <= ord(i) <= 90: upper.append(i) elif 97 <= ord(i) <= 122: lower.append(i) result = "数字的个数有:%s个,大写字符串有%s个,小写字符串有%s个" % (len(number), upper.__len__(), lower.__len__()) return result input_str = '1faef42r39rufnalkdnfAJnfndkjALSLfNF' res = counts_str_type(input_str) print(res)
3. 获取两个列表l1=[11,22,33]和l2=[22,33,44]中相同的元素集合:
l1=[11,22,33] l2=[22,33,44] l1 = set(l1) print(l1 & set(l2))
4.将字符串"老男人"转换成UTF-8编码的字节类型
str = '老男人' print(str.encode('utf-8'))
5. 如何计算数字绝对值
test1=abs(-5) print(test1) test2=abs(5) print(test2)
6. 列举布尔值为False的值
为空的数据类型或者数字是“0”的布尔值都为假(即False)
7.简述深浅copy原理