深入 Python :Dive Into Python第一天

1:一上午看Dive Into Python,在线到第7章,有些细节我过滤了

总结下python和java的区别:

             没有数据类型的定义

             内置的dict,list,等和hashmap,arrylist等有些类似

             python中import和java中import意思相同

             python的语法很不同

                       可以寻列赋值

                       严格的缩进 表示一个语句块

                       def 定义一个函数 当然还有lamab方法

                       if--elif--elif-  :中判断没有()用:

                       万物皆对象,模块什么的

                       for循环的不同

                       写python就像写英语一样

                       面向对象,class 同样的,(fater)fater就是父类

                       写在类中属性是类所共有的,就是java中static的意思

                       类中有_init_相当与构造函数,在类初始化的时候执行

                       self相当于java中this的

                       实例化对象p=Person()不需要java中的new

                       处理异常的

                       ..................................

 

python有很多模块

1:学习 urllib模块 -------------------相当于java中的jsoup,解析html文件的


推荐的书籍:深入 Python :Dive Into Python

个人觉得比简明教程好看点,跟着作者来 基本上没问题

http://sebug.net/paper/books/dive-into-python/html/index.html  

 

一篇关于urllib模块的blog

http://www.blogjava.net/ashutc/archive/2011/03/21/346695.html

 

实例:

 

>>> def fib(n):
	print 'n=',n
	if n>1:
		return n*fib(n-1)
	else:
		print 'end of the line'
		return 1

	
>>> d = {"server":"hello","database":"mysql"}
>>> d
{'database': 'mysql', 'server': 'hello'}
>>> fib(5)
n= 5
n= 4
n= 3
n= 2
n= 1
end of the line
120
>>> d["server"]
'hello'
>>> d["database"]
'mysql'
>>> d["database"] = 'oracle'
>>> d["database"]
'oracle'
>>> del d["server"]
>>> d
{'database': 'oracle'}
>>> d["boy"] ='ljq'
>>> d
{'boy': 'ljq', 'database': 'oracle'}
>>> d.clear()
>>> d
{}
>>> d
{}
>>> li = ['a','b','c','woaini']
>>> li
['a', 'b', 'c', 'woaini']
>>> li[0]
'a'
>>> li[4]

Traceback (most recent call last):
  File "<pyshell#27>", line 1, in <module>
    li[4]
IndexError: list index out of range
>>> li[3]
'woaini'
>>> li[-1]
'woaini'
>>> li[-3]
'b'
>>> li
['a', 'b', 'c', 'woaini']
>>> li[1:-1]
['b', 'c']
>>> li[1,2]

Traceback (most recent call last):
  File "<pyshell#33>", line 1, in <module>
    li[1,2]
TypeError: list indices must be integers, not tuple
>>> li
['a', 'b', 'c', 'woaini']
>>> li[0:2]
['a', 'b']
>>> li[1:2]
['b']
>>> li[1:-1]
['b', 'c']
>>> li[1:4]
['b', 'c', 'woaini']
>>> li[1:0]
[]
>>> li
['a', 'b', 'c', 'woaini']
>>> li[:3]
['a', 'b', 'c']
>>> li[1:]
['b', 'c', 'woaini']
>>> li[2:]
['c', 'woaini']
>>> li[:]
['a', 'b', 'c', 'woaini']
>>> li.append('girl')
>>> li
['a', 'b', 'c', 'woaini', 'girl']
>>> li.insert(2,'new')
>>> li
['a', 'b', 'new', 'c', 'woaini', 'girl']
>>> li.extend(["two","element"])
>>> li
['a', 'b', 'new', 'c', 'woaini', 'girl', 'two', 'element']
>>> li
['a', 'b', 'new', 'c', 'woaini', 'girl', 'two', 'element']
>>> li.extend(['d','ff','ddd'])
>>> li
['a', 'b', 'new', 'c', 'woaini', 'girl', 'two', 'element', 'd', 'ff', 'ddd']
>>> len(li)
11
>>> li[-1]
'ddd'
>>> li.append(['2','dd','dddddd'])
>>> li
['a', 'b', 'new', 'c', 'woaini', 'girl', 'two', 'element', 'd', 'ff', 'ddd', ['2', 'dd', 'dddddd']]
>>> li.index('new')
2
>>> li.index('wddddd')

Traceback (most recent call last):
  File "<pyshell#59>", line 1, in <module>
    li.index('wddddd')
ValueError: list.index(x): x not in list
>>> "a" in li
True
>>> "ddddddd"  in li
False
>>> li.remove('a')
>>> li
['b', 'new', 'c', 'woaini', 'girl', 'two', 'element', 'd', 'ff', 'ddd', ['2', 'dd', 'dddddd']]
>>> li.pop()
['2', 'dd', 'dddddd']
>>> li
['b', 'new', 'c', 'woaini', 'girl', 'two', 'element', 'd', 'ff', 'ddd']
>>> li = ['a','d','mygirl']
>>> li
['a', 'd', 'mygirl']
>>> li = li +['hello','mybory']
>>> li
['a', 'd', 'mygirl', 'hello', 'mybory']
>>> li = li * 2
>>> l

Traceback (most recent call last):
  File "<pyshell#71>", line 1, in <module>
    l
NameError: name 'l' is not defined
>>> li
['a', 'd', 'mygirl', 'hello', 'mybory', 'a', 'd', 'mygirl', 'hello', 'mybory']
>>> t = ('a','b','c')
>>> t
('a', 'b', 'c')
>>> t[1]
'b'
>>> t[-1]
'c'
>>> t.append('new')

Traceback (most recent call last):
  File "<pyshell#77>", line 1, in <module>
    t.append('new')
AttributeError: 'tuple' object has no attribute 'append'
>>> "a" in t
True
>>> v=('a','b','c')
>>> v
('a', 'b', 'c')
>>> (x,y,z)=v
>>> x
'a'
>>> y
'b'
>>> b

Traceback (most recent call last):
  File "<pyshell#84>", line 1, in <module>
    b
NameError: name 'b' is not defined
>>> c

Traceback (most recent call last):
  File "<pyshell#85>", line 1, in <module>
    c
NameError: name 'c' is not defined
>>> z
'c'
>>> v
('a', 'b', 'c')
>>> range(7)
[0, 1, 2, 3, 4, 5, 6]
>>> (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7)
>>> MONDAY
0
>>> K='UID'
>>> V = 'SA'
>>> '%S=%S' % (K,V)

Traceback (most recent call last):
  File "<pyshell#93>", line 1, in <module>
    '%S=%S' % (K,V)
ValueError: unsupported format character 'S' (0x53) at index 1
>>> k = "uid"
>>> v = "sa"
>>> "%s=%s" % (k, v)
'uid=sa'
>>> print "%s is not a good password for %s" % (pwd, uid)

Traceback (most recent call last):
  File "<pyshell#97>", line 1, in <module>
    print "%s is not a good password for %s" % (pwd, uid)
NameError: name 'pwd' is not defined
>>> print "%s is not a good password for %s" % (K, V)
UID is not a good password for SA
>>> print "today" stock price : %f" % 50.5556666
SyntaxError: invalid syntax
>>>  print "Today's stock price: %f" % 50.4625
 
  File "<pyshell#100>", line 1
    print "Today's stock price: %f" % 50.4625
   ^
IndentationError: unexpected indent
>>> print "Today's stock price: %f" % 50.4625
Today's stock price: 50.462500
>>> print "Today's stock price: %.2f" % 50.4625
Today's stock price: 50.46
>>> print "Today's stock price: %f+.2" % 50.4625
Today's stock price: 50.462500+.2
>>> li = [1,9,8,4]
>>> [elem*2 for elem in li]
[2, 18, 16, 8]
>>> li
[1, 9, 8, 4]
>>> li = [elem*2 for elem in li]
>>> li
[2, 18, 16, 8]
>>> d
{}
>>> d =[]params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}
SyntaxError: invalid syntax
>>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}
>>> params
{'pwd': 'secret', 'database': 'master', 'uid': 'sa', 'server': 'mpilgrim'}
>>> params.keys()
['pwd', 'database', 'uid', 'server']
>>> params.values();
['secret', 'master', 'sa', 'mpilgrim']
>>> params.items()
[('pwd', 'secret'), ('database', 'master'), ('uid', 'sa'), ('server', 'mpilgrim')]
>>> li = ['server=mpilgrim', 'uid=sa', 'database=master', 'pwd=secret']
>>> li
['server=mpilgrim', 'uid=sa', 'database=master', 'pwd=secret']
>>> s=":".join(li)
>>> s
'server=mpilgrim:uid=sa:database=master:pwd=secret'
>>> s.split(':')
['server=mpilgrim', 'uid=sa', 'database=master', 'pwd=secret']
>>> type(1)
<type 'int'>
>>> type("hello")
<type 'str'>
>>> type[d]

Traceback (most recent call last):
  File "<pyshell#123>", line 1, in <module>
    type[d]
TypeError: 'type' object is unsubscriptable
>>> type(li)
<type 'list'>
>>> type(params)
<type 'dict'>
>>> str(1)
'1'
>>> dir(li)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> li=[]
>>> dir(li)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> li=["boy","girl"]
>>> li
['boy', 'girl']
>>> li.pop()
'girl'
>>> li
['boy']
>>> getattr(li,"pop")
<built-in method pop of list object at 0x011CE080>
>>> li = ["a", "mpilgrim", "foo", "b", "c", "b", "d", "d"]
>>> li
['a', 'mpilgrim', 'foo', 'b', 'c', 'b', 'd', 'd']
>>> [elem for elem in li if len(elem)>2]
['mpilgrim', 'foo']
>>> li=[elem for elem in li if len(elem)>2]
>>> li
['mpilgrim', 'foo']
>>> 'a' adn 'b'
SyntaxError: invalid syntax
>>> 'a' and 'b'
'b'
>>> '' and 'b'
''
>>> def f(x)
SyntaxError: invalid syntax
>>> def f(x):
	return x*2

 
>>> 
>>> 

>>> f(4)
8
>>> g = lambda x:x*2
>>> g(3)
6
>>> s = "this   is\na\ttest"
>>> print s
this   is
a	test
>>> print s.split()
['this', 'is', 'a', 'test']
>>> print " ".join(s.split)

Traceback (most recent call last):
  File "<pyshell#159>", line 1, in <module>
    print " ".join(s.split)
TypeError
>>> print " ".join(s.split())
this is a test
>>> import types
>>> types.FunctionType
<type 'function'>
>>> FunctionType

Traceback (most recent call last):
  File "<pyshell#163>", line 1, in <module>
    FunctionType
NameError: name 'FunctionType' is not defined
>>> from types import FunctionType
>>> FunctionType
<type 'function'>
>>> fsoc = open('/onttt','r')

Traceback (most recent call last):
  File "<pyshell#166>", line 1, in <module>
    fsoc = open('/onttt','r')
IOError: [Errno 2] No such file or directory: '/onttt'
>>> try:
	fsock = open('/onttt','r')
     except IOError
     
  File "<pyshell#173>", line 3
    except IOError
                 ^
IndentationError: unindent does not match any outer indentation level
>>> 
>>> try:
	fsock = open('/onttt','r')
     except IOError:
	     
  File "<pyshell#175>", line 3
    except IOError:
                  ^
IndentationError: unindent does not match any outer indentation level
>>> try:
	fsock = open('/dddddd')
    except IOError:
	    
  File "<pyshell#178>", line 3
    except IOError:
                  ^
IndentationError: unindent does not match any outer indentation level
>>> try:
	fsock = open('/dddd')
    except IOError:
	    
  File "<pyshell#202>", line 3
    except IOError:
                  ^
IndentationError: unindent does not match any outer indentation level
>>> try:
	fscok = open('/ddddd')
     except IOError:
	     
  File "<pyshell#205>", line 3
    except IOError:
                  ^
IndentationError: unindent does not match any outer indentation level
>>> import urllib
>>> sock = urllib.urllib.urlopen("www.baidu.com")

Traceback (most recent call last):
  File "<pyshell#207>", line 1, in <module>
    sock = urllib.urllib.urlopen("www.baidu.com")
AttributeError: 'module' object has no attribute 'urllib'
>>> sock = urllib.urlopen("www.baidu.com")

Traceback (most recent call last):
  File "<pyshell#208>", line 1, in <module>
    sock = urllib.urlopen("www.baidu.com")
  File "C:\Python26\lib\urllib.py", line 86, in urlopen
    return opener.open(url)
  File "C:\Python26\lib\urllib.py", line 205, in open
    return getattr(self, name)(url)
  File "C:\Python26\lib\urllib.py", line 467, in open_file
    return self.open_local_file(url)
  File "C:\Python26\lib\urllib.py", line 481, in open_local_file
    raise IOError(e.errno, e.strerror, e.filename)
IOError: [Errno 2] : 'www.baidu.com'
>>> urllib
<module 'urllib' from 'C:\Python26\lib\urllib.py'>
>>> sock = urllib.URLopener
>>> sock
<class urllib.URLopener at 0x011F7990>
>>> htmlSource = sock.read()

Traceback (most recent call last):
  File "<pyshell#212>", line 1, in <module>
    htmlSource = sock.read()
AttributeError: class URLopener has no attribute 'read'
>>> import urllib
>>> print urllib.urlopen('http://www.baidu.com').read()

你可能感兴趣的:(python)