可以写简单的函数,f1 = f2, f3 = f4
1 # 1 2 def f1(): 3 return 123 4 print(f1, f1()) 5 # <function f1 at 0x10217c950> 123 6 f2 = lambda: 123 7 print(f2, f2()) 8 # <function <lambda> at 0x10217c9d8> 123 9 10 # 2 11 def f3(a1, a2): 12 return a1 + a2 13 print(f3, f3(1, 2)) 14 # <function f3 at 0x10217ca60> 3 15 f4 = lambda a1, a2: a1 + a2 16 print(f4, f4(1, 2)) 17 # <function <lambda> at 0x10217cae8> 3
# 绝对值 a = abs(-1) print("1.abs ==>> ", a) # 1.abs ==>> 1
# 循环参数,若每一个都为真,返回True,否则返回False a = all(["", 11, "jksf"]) print("2.all ==>> ", a) # 2.all ==>> False
# 循环参数,若有一个都为真,返回True,否则返回False a = any(["", 11, "jksf"]) print("3.any ==>> ", a) # 3.any ==>> True
# 到对象的类中寻找__repr__方法,获取返回值 class Foo(): def __repr__(self): return "hello" obj = Foo() r = ascii(obj) print("4.obj ==>> ", r) # 4.obj ==>> hello
# 二进制 a = bin(10) print("5.bin ==>> ", a) # 5.bin ==>> 0b1010
# 八进制 a = oct(10) print("6.oct ==>> ", a) # 6.oct ==>> 0o12
# 十进制 a = int(10) print("7.int ==>> ", a) # 7.int ==>> 10
# 十六进制 a = hex(10) print("8.hex ==>> ", a) # 8.hex ==>> 0xa
# 布尔值,判断真假 a = bool("") b = bool(" ") print("9.bool ==>> ", "a:", a, "b:", b) # 9.bool ==>> a: False b: True
# 字节列表 a = bytearray("一个", encoding="utf-8") print("11.bytearray ==>> ", a) # 11.bytearray ==>> bytearray(b‘\xe4\xb8\x80\xe4\xb8\xaa‘)
# 字节 a = bytes("一个", encoding="utf-8") print("10.bytes ==>> ", a) # 10.bytes ==>> b‘\xe4\xb8\x80\xe4\xb8\xaa‘
a = chr(65) b = ord("F") print(a,b) # A 70
# 实例:随机验证码 import random temp = "" for i in range(6): # 生成0-9的随机数 num = random.randrange(0, 10) # 生成的数字为3或者7的时候 if num == 3 or num == 7: # 生成0 - 9的随机数 rad1 = random.randrange(0, 10) # 将随机数转换成字符串 c1 = str(rad1) temp += c1 else: # 否则 # 生成65-90的随机数 rad2 = random.randrange(65, 91) # 将随机数转换成大写字母 c2 = chr(rad2) temp += c2 print(temp)
# 检测能否被执行,调用 def f1(): pass f2 = 1223 a = callable(f1()) b = callable(f2) print("12.callable ==>> ", "a:", a, "b:", b) # 12.callable ==>> a: False b: False
1 # compile() # 编译 2 exec() # 执行Python代码或字符串,接受代码或表达式,无返值 3 s = "print(123)" 4 r = compile(s,"<string>","exec") # 第三位为编译模式,single:单行;eval:表达式;exec:与python一样的代码 5 print(r) # <code object <module> at 0x101337ed0, file "<string>", line 1> 6 # 执行python代码 7 exec(r) 8 9 # eval() # 执行表达式并获取结果,有返回值, 10 s = "8*8" 11 ret = eval(s) 12 print(ret) # 64 13 """
# 计算用于得出商,余 a = divmod(10, 3) print("13.divmod ==>> ", a) # 13.divmod ==>> (3, 1)
def f2(a): if a > 22: return True list_1 = [11, 22, 33, 44, 55] ret = filter(f2, list_1) print(list(ret)) # [33, 44, 55] # 上述可写成lambda表达式为: list_1 = [11, 22, 33, 44, 55] result = filter(lambda a: a > 22, list_1) print(list(result)) # [33, 44, 55]
li = [11, 22, 33, 44, 55] def f1(a): return a + 100 result = map(f1,li) print(list(result)) # [111, 122, 133, 144, 155] # 上述可写成lambda表达式为: li = [11, 22, 33, 44, 55] result = map(lambda a: a + 100, li) print(list(result)) # [111, 122, 133, 144, 155]
1 # iter,# next 2 obj = iter([11, 22, 33, 44]) 3 print("14.iter ==>> ", obj) 4 # 14.iter ==>> <list_iterator object at 0x102079c50> 5 6 r = next(obj) 7 print("15.next ==>> ", r) 8 # 15.next ==>> 11
# 1 l1 = ["I", 11, 22, 33] l2 = ["am", 44, 55, 66] l3 = ["R.H", 77, 88, 99] r = zip(l1, l2, l3) temp = list(r)[0] ret = " ".join(temp) print("16.zip ==>> ", "r:", r, "ret:", ret) # 16.zip ==>> I am R.H # 2 l1 = ["I", 11, 22, 33] l2 = ["am", 44, 55, 66] l3 = ["R.H", 77, 88, 99] r = zip(l1, l2, l3) l = [] for i in r: l.append(i) print("16.zip ==>> ", l) # 16.zip ==>> [(‘I‘, ‘am‘, ‘R.H‘), (11, 44, 77), (22, 55, 88), (33, 66, 99)]
1 # 1-1 2 f = open("ha.txt") # 打开文件 ==>> 打开方式默认"只读" 3 data = f.read() # 读取文件 4 f.close() # 关闭文件 5 print(data) # 输出 6 # 1-2 7 f = open("ha2.txt", "w") 8 f.write("121111111111") 9 f.close()
原文:http://www.cnblogs.com/grissom/p/6376216.html