In [2]:
return "Positive" if var >=0 else "Negative"


  File "<ipython-input-2-deee312052d3>", line 1
    return "Positive" if var >=0 else "Negative"
SyntaxError: 'return' outside function

In [9]:
a =1 
assert a==2  , "fuck"


---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-9-f5d506428bb1> in <module>()
      1 a =1
----> 2 assert a==2  , "fuck"

AssertionError: fuck

In [10]:
"condition"
for x in xrange(1,20):
    print x


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

In [13]:
range(10)


Out[13]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [15]:
xrange(10)


Out[15]:
xrange(10)

In [16]:
li = ['a', 'b','c' ]
for index in xrange(len(li)):
    print index


0
1
2

In [17]:
li = ['a', 'b','c' ]
for i,e in enumerate(li):
    print i, e


0 a
1 b
2 c

In [19]:
li = ['a', 'b','c' ]
for i,e in enumerate(li,start=3):
    print i, e


3 a
4 b
5 c

In [24]:
li = ['a', 'b','c','d' , 'e' ]
for i,e in enumerate(reversed(li), start=100):
    print i, e


100 e
101 d
102 c
103 b
104 a

In [26]:
def test1():pass
def test1(name):
    print name

In [27]:
test1("hello")


hello

In [28]:
test1()


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-28-e3097d5bf3e6> in <module>()
----> 1 test1()

TypeError: test1() takes exactly 1 argument (0 given)

In [36]:
def test(): pass

In [37]:
ret = test()

In [38]:
test


Out[38]:
<function __main__.test>

In [39]:
test.a = 1

In [40]:
test.a


Out[40]:
1

In [41]:
def test():
    """this is doc"""""

In [42]:
test.func_doc


Out[42]:
'this is doc'

In [ ]:


In [43]:
__builtin__


Out[43]:
<module '__builtin__' (built-in)>

In [44]:
dir(__builtin__)


Out[44]:
['ArithmeticError',
 'AssertionError',
 'AttributeError',
 'BaseException',
 'BufferError',
 'BytesWarning',
 'DeprecationWarning',
 'EOFError',
 'Ellipsis',
 'EnvironmentError',
 'Exception',
 'False',
 'FloatingPointError',
 'FutureWarning',
 'GeneratorExit',
 'IOError',
 'ImportError',
 'ImportWarning',
 'IndentationError',
 'IndexError',
 'KeyError',
 'KeyboardInterrupt',
 'LookupError',
 'MemoryError',
 'NameError',
 'None',
 'NotImplemented',
 'NotImplementedError',
 'OSError',
 'OverflowError',
 'PendingDeprecationWarning',
 'ReferenceError',
 'RuntimeError',
 'RuntimeWarning',
 'StandardError',
 'StopIteration',
 'SyntaxError',
 'SyntaxWarning',
 'SystemError',
 'SystemExit',
 'TabError',
 'True',
 'TypeError',
 'UnboundLocalError',
 'UnicodeDecodeError',
 'UnicodeEncodeError',
 'UnicodeError',
 'UnicodeTranslateError',
 'UnicodeWarning',
 'UserWarning',
 'ValueError',
 'Warning',
 'ZeroDivisionError',
 '__IPYTHON__',
 '__IPYTHON__active',
 '__debug__',
 '__doc__',
 '__import__',
 '__name__',
 '__package__',
 'abs',
 'all',
 'any',
 'apply',
 'basestring',
 'bin',
 'bool',
 'buffer',
 'bytearray',
 'bytes',
 'callable',
 'chr',
 'classmethod',
 'cmp',
 'coerce',
 'compile',
 'complex',
 'copyright',
 'credits',
 'delattr',
 'dict',
 'dir',
 'divmod',
 'dreload',
 'enumerate',
 'eval',
 'execfile',
 'file',
 'filter',
 'float',
 'format',
 'frozenset',
 'get_ipython',
 'getattr',
 'globals',
 'hasattr',
 'hash',
 'help',
 'hex',
 'id',
 'input',
 'int',
 'intern',
 'isinstance',
 'issubclass',
 'iter',
 'len',
 'license',
 'list',
 'locals',
 'long',
 'map',
 'max',
 'memoryview',
 'min',
 'next',
 'object',
 'oct',
 'open',
 'ord',
 'pow',
 'print',
 'property',
 'range',
 'raw_input',
 'reduce',
 'reload',
 'repr',
 'reversed',
 'round',
 'set',
 'setattr',
 'slice',
 'sorted',
 'staticmethod',
 'str',
 'sum',
 'super',
 'tuple',
 'type',
 'unichr',
 'unicode',
 'vars',
 'xrange',
 'zip']

In [ ]:


In [45]:
type(str)


Out[45]:
type

In [3]:
import pandas as pd

In [ ]:


In [ ]:


In [ ]:
""" """
def func(p1, p2, p3=None, p4=None, *args, **kwargs)

In [5]:
pd.readfile


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-707d7daa2d69> in <module>()
----> 1 pd.readfile

AttributeError: 'module' object has no attribute 'readfile'

In [ ]:
sorted()

In [ ]:


In [1]:
lst = list("abccbas")

In [2]:
sorted(lst)


Out[2]:
['a', 'a', 'b', 'b', 'c', 'c', 's']

In [ ]:
sorted(lst)

In [ ]:


In [11]:
def add(v1,v2,v3):
    return v1+v2+v3

In [12]:
v1,v2,v3=1,2,3
add(v1,v2,v3)


Out[12]:
6

In [13]:
tpl=(1,2,3)

In [16]:
add(*tpl) #解包tuple, 也可以是list


Out[16]:
6

In [ ]:


In [19]:
def add2(v1=0,v2=0,v3=0):
    return v1+v2+v3

In [20]:
dct = {"v1":1,"v2":2,"v3":3}

In [22]:
add2(**dct) #双*高级解包


Out[22]:
6

In [ ]:


In [23]:
dct2={1:"xx"}

In [25]:
def add(v1,v2,**hello):
    return hello

In [29]:
add(1,2,a=1,b=1)


Out[29]:
{'a': 1, 'b': 1}

In [ ]:


In [ ]:


In [30]:
def change_list(lst):
    lst[0] =2
    lst = []
    print "2:", id(lst)
    
    
lst = [1,2,3]
print "1:" , id(lst)
change_list(lst)
print lst


1: 4418840552
2: 4400802200
[2, 2, 3]

In [32]:
lst


Out[32]:
[2, 2, 3]

In [ ]:


In [31]:
reversed(lst)


Out[31]:
<listreverseiterator at 0x108458290>

In [ ]:


In [ ]:


In [33]:
sorted2([1,2,3,1]) ==> [3,2,1,1]
sorted2([123, 22, 0000], key=len)  ==> [0000, 123, 22]
sorted2([1,2,3,1], reverse=False) ==> [1,1,2,3]


  File "<ipython-input-33-ff340125247a>", line 1
    sorted2([1,2,3,1]) ==> [3,2,1,1]
                         ^
SyntaxError: invalid syntax

In [35]:
sorted([1,2,3,1])#==> [3,2,1,1]


Out[35]:
[1, 1, 2, 3]

In [ ]:


In [ ]:
sorted(iterable, cmp=None, key=None, reverse=False)

In [77]:
def sorted2(iterable, cmp=None, key=None, reverse=False):
    return sorted(iterable, cmp=cmp, key=key, reverse=reverse)

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [76]:
sorted2([1,2,3,1])


Out[76]:
[1, 1, 2, 3]

In [ ]:


In [73]:
sorted2([1,2,3,1], reverse=True)


Out[73]:
[3, 2, 1, 1]

In [74]:
sorted2([1,2,3,1], reverse=False)


Out[74]:
[1, 1, 2, 3]

In [ ]:


In [50]:
sorted2(["123", "22", "0000"], key=len)


Out[50]:
['0000', '123', '22']

In [72]:
assert [1, 1, 2, 3] == sorted2([1,2,3,1], reverse=True) , "not reverse"


---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-72-3332d1b61752> in <module>()
----> 1 assert [1, 1, 2, 3] == sorted2([1,2,3,1], reverse=True) , "not reverse"

AssertionError: not reverse

In [ ]:


In [ ]:


In [ ]:


In [ ]:
sorted2([1,2,3,1], reverse=False)

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [44]:
sorted(["123", "22", "0000"], key=len)


Out[44]:
['22', '123', '0000']

In [ ]:


In [ ]:


In [46]:
sorted(["123", "22", "0000"], key=len, reverse=True)


Out[46]:
['0000', '123', '22']

In [47]:
sorted(["123", "22", "0000"], key=len)


Out[47]:
['22', '123', '0000']

In [ ]:


In [63]:
sorted([1,2,3,1], reverse=True)


Out[63]:
[3, 2, 1, 1]

In [ ]:


In [40]:
sorted.__doc__


Out[40]:
'sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list'

In [ ]:


In [ ]:


In [ ]:


In [ ]: