python 练习

 1 import types
 2 x=20
 3 
 4 print type(x) 
 5 
 6 print x.__class__ #判断x的类型 <type int>
 7 
 8 print x #x的值,20
 9 
10 x=123
11 
12 print globals() #{'__builtins__': <module '__builtin__' (built-in)>, '__file__': 'test.py', '__package__': None, 'x': 123, '__name__': '__main__', '__doc__': None, 'types': <module 'types' from '/usr/lib/python2.7/types.pyc'>}
13 
14 
15 globals()["y"]='hello world!'
16 
17 print y #hello world
18 print type(y) #<type str>
19 
20 import string
21 y=__import__('string')
22 
23 print type(y) #<type 'module'>
24 
25 
26 print y.digits #0123456789
27 
28 import gc
29 print gc.get_threshold() #获取各级代龄阀值
30 
31 print gc.get_count() #各级代龄阀值追踪对象的
32 
33 
34 print gc.collect() #回收
35 
36 from string import letters,digits,Template
37 
38 print letters
39 
40 l=list('abc') //l是小写的L,这次是我的没考虑到,不太好区分
41 
42 l[1]=2 #替换l[1]的数值为2,从0开始
43 
44 print l#['a', 2, 'c']
45 
46 
47 l.append('d')#追加元素
48 
49 print l  #['a', 2, 'c','d']
50 
51 l.insert(1,'e路相扶')#在l[1]处插入'e路相扶'
52 
53 print l#['a','e路相扶 ', 2, 'c','d']
54 
55 l.insert(4,'zjzj')#在l[4]插入'zjzj' 
56 
57 print l #['a','e路相扶 ', 2, 'c','d','zjzj']
58 
59 l.pop(2) #删除并弹出l[2]
60 
61 print l #['a','e路相扶 ', 'c','d','zjzj']
62 
63 print l.pop() #删除最后一个元素,并弹出
64  
65 print l#['a','e路相扶 ', 'c','d']
66  
67 import bisect
68  
69 bisect.insort(l,'n')
70  
71 print l
72  
73  
原文地址:https://www.cnblogs.com/zhangjun516/p/3222993.html