In [4]:
s1 = "abc"
tpl = ()
lst = []
st = set()
dct = {}

In [ ]:


In [3]:
def show(lst):
    print [(type(x)) for x in lst]

In [5]:
show((s1,tpl,lst,st,dct))


[<type 'str'>, <type 'tuple'>, <type 'list'>, <type 'set'>, <type 'dict'>]

In [7]:
print ord('a')
print chr(97)


97
a

In [10]:
print '\\127.0.0.1\share\\'


\127.0.0.1\share\

In [22]:
print r"\\127.0.0.1\share\\"


\\127.0.0.1\share\\

In [ ]:


In [ ]:


In [19]:
s1 = "你好"
s2 = u"你好"
print type(s1)
print s1


print type(s2)
print s2


<type 'str'>
你好
<type 'unicode'>
你好

In [23]:
dir(str)


Out[23]:
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__getnewargs__',
 '__getslice__',
 '__gt__',
 '__hash__',
 '__init__',
 '__le__',
 '__len__',
 '__lt__',
 '__mod__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rmod__',
 '__rmul__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '_formatter_field_name_split',
 '_formatter_parser',
 'capitalize',
 'center',
 'count',
 'decode',
 'encode',
 'endswith',
 'expandtabs',
 'find',
 'format',
 'index',
 'isalnum',
 'isalpha',
 'isdigit',
 'islower',
 'isspace',
 'istitle',
 'isupper',
 'join',
 'ljust',
 'lower',
 'lstrip',
 'partition',
 'replace',
 'rfind',
 'rindex',
 'rjust',
 'rpartition',
 'rsplit',
 'rstrip',
 'split',
 'splitlines',
 'startswith',
 'strip',
 'swapcase',
 'title',
 'translate',
 'upper',
 'zfill']

In [ ]:


In [ ]:


In [20]:
def sub_string(s1):
    if isinstance(s1,str):
        print s1.decode("utf8")[0]
    elif isinstance(s1,unicode)    :
        print s1[0]

In [25]:
"x    something x   ".lstrip(" x").rstrip()


Out[25]:
'something x'

In [41]:
s1= "0123456789"
print s1[1:-1:1]
print s1[::-2]
print s1[-3:-1]


12345678
97531
78

In [42]:
range(0,19)


Out[42]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]

In [43]:
range(0,12,3)


Out[43]:
[0, 3, 6, 9]

In [44]:
range(10,1,-1)


Out[44]:
[10, 9, 8, 7, 6, 5, 4, 3, 2]

In [ ]:


In [45]:


In [52]:
lst = [0,1,2,3,4,5,6,7]

del lst[-1]
print lst


lst[-2:] = [0,0]
print lst


lst[-2:] = [1]
print lst


[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 0, 0]
[0, 1, 2, 3, 4, 1]

In [53]:
s1 = "abcde123"

In [54]:
import StringIO

In [55]:
so = StringIO.StringIO()

In [56]:
so.write("abc)")

In [59]:
import cStringIO as StringIO
so = StringIO.StringIO()

In [ ]:
"%s = %s "

In [61]:
print "{1}={0} {1}".format("k","v",123)


v=k v

In [62]:
print "%s=%s %s" % ("k","v","k")


k=v k

In [63]:
print "{k1}={k2}".format(k1=1,k2=2)


1=2

In [64]:
print "%s" % "123321"


123321

In [70]:
class MyObj(obj):
    pass


obj = MyObj()
obj.age =10
obj.name ="lalala"

print "{obj.age}, {obj.name}".format(obj)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-70-3aa74c73a119> in <module>()
----> 1 class MyObj(obj):
      2     pass
      3 
      4 
      5 obj = MyObj()

TypeError: Error when calling the metaclass bases
    this constructor takes no arguments

In [77]:
s1 = "abc"
str(s1), repr(s1)


Out[77]:
('abc', "'abc'")

In [75]:
s2 = u"abc"
str(s2), repr(s2)


Out[75]:
('abc', "u'abc'")

In [ ]:


In [ ]:


In [78]:
tp = ("asdb",123,str,type,[1,3,2])

In [79]:
tp[1]


Out[79]:
123

In [82]:
tp[2]("hello")


Out[82]:
'hello'

In [85]:
st = {1,23,4, "abc"}
%timeit "abc" in st


10000000 loops, best of 3: 47.4 ns per loop

In [ ]:


In [86]:
lst = [1, 23, 4, "abc"]
%timeit "abc" in lst


10000000 loops, best of 3: 148 ns per loop

In [93]:
for x in range(0,10):
    print x


0
1
2
3
4
5
6
7
8
9

In [89]:
xrange(1000)


Out[89]:
xrange(1000)

In [ ]:


In [90]:
%time
for x in range(10000000):
    pass


CPU times: user 2 µs, sys: 0 ns, total: 2 µs
Wall time: 5.01 µs

In [91]:
%time
for x in xrange(10000000):
    pass


CPU times: user 2 µs, sys: 1e+03 ns, total: 3 µs
Wall time: 7.15 µs

In [92]:
dct = {1:[]}

In [ ]:


In [97]:
lst =["abc","x",'nn']
print sorted(lst)
print sorted(lst,key=len)


['abc', 'nn', 'x']
['x', 'nn', 'abc']

In [ ]:


In [95]:



Out[95]:
['abc', 'nn', 'x']

In [96]:



Out[96]:
['x', 'nn', 'abc']

In [ ]:


In [118]:
dct = {"xiao":(18,"shanghai","boy"),
       "mei":(20,"nanjing","girl") ,
       "mei2":(20,"shanghai","boy") ,
       "mei3":(20,"nanjing","girl") ,
       "mei4":(20,"nanjing","girl") ,      }

In [119]:
lst = []
for name, info in dct.viewitems():
    if info[0] >= 20 and info[2] =="boy" and info[1] =="shanghai":
        lst.append(name)
print lst


['mei2']

In [120]:
[name
 for name, info in dct.viewitems()
 if info[0] >= 20 and info[2] =="boy" and info[1] == "shanghai"
]


Out[120]:
['mei2']

In [ ]:


In [121]:



---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-121-e60602e455e4> in <module>()
----> 1 import collection

ImportError: No module named collection

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: