`
iluoxuan
  • 浏览: 579821 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

深入 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()
分享到:
评论

相关推荐

    dive-into-python3 (英文版)+深入python3(中文版)

    《Dive Into Python3》和《深入Python3》是两本深受Python爱好者欢迎的书籍,分别提供了英文和中文的学习资源,旨在帮助读者全面理解和掌握Python3编程语言。这两本书覆盖了Python3的基础语法、高级特性以及实际应用...

    深入 Python :Dive Into Python 中文版

    Dive Into Python 是为有经验的程序员编写的一本 Python 书

    dive into python (深入python)中文教程

    在Python的世界里,"dive into"通常意味着深入探索,这表明本书将带领读者逐步深入Python的核心概念和技术。教程内容可能涵盖变量、数据类型(如整型、浮点型、字符串、列表、元组、字典等)、控制结构(如条件语句...

    深入Python (Dive Into Python)

    深入python,深入Python (Dive Into Python) 译者序 by limodou 主页(http://phprecord.126.com) Python论坛 本书英文名字为《Dive Into Python》,其发布遵守 GNU 的自由文档许可证(Free Document Lience)的...

    深入 Python_Dive Into Python 中文版

    《深入 Python_Dive Into Python 中文版》是一本专为Python初学者和进阶者设计的教程,旨在帮助读者深入理解和掌握Python编程语言的核心概念、语法结构以及实用技巧。这本书全面覆盖了Python的基础知识和高级特性,...

    深入Python 3Dive Into Python 3

    Dive Into Python 3涵盖了Python 3及其与Python 2的区别。与Dive Into Python相比,Dive Into Python 3的修订版约为20%,新材料为80%。

    Dive Into Python 3

    《深入Python 3》是一本全面且深入介绍Python 3编程语言的电子书籍,旨在帮助读者从...压缩包中的文件“diveintomark-diveintopython3-793871b”很可能是该书的源代码或HTML文件,可以配合阅读,加深对书中示例的理解。

    dive into python3 (中文版)

    《深入Python3(中文版)》是一本系统介绍Python 3的书籍,旨在帮助读者深入学习Python 3的基本知识与应用。本文将根据给定文件的信息,展开介绍Python 3的基础知识点。 首先,安装Python是学习Python语言的第一步...

    深入理解python dive into python

    《深入理解Python Dive Into Python》是一本专注于Python编程语言的深度学习书籍,旨在帮助读者全面掌握Python的核心概念、语法和编程技巧。这本书以其详尽的解释和实例,为初学者和有经验的开发者提供了深入了解...

    深入python 3 (DIVE INTO PYTHON 3)最新版,加源代码

    "DiveIntoPython3.pdf"应该是书籍的PDF版,而"DiveIntoPythonExamples"很可能是书中示例代码的集合,这些资源对于自学Python 3非常有帮助。通过实践,读者可以更好地掌握书中的知识点,并逐步提升自己的Python编程...

    Dive into Python3

    《Dive into Python3》的压缩包文件名为diveintopython3-r860-2010-01-13,这可能表示它是2010年1月13日发布的第860个修订版。这个版本可能包含了作者对初版的修正和更新,以适应Python 3的最新发展。 通过阅读这...

    《Dive Into Python 3中文版》PDF

    《Dive Into Python 3中文版》是一本深入学习Python 3编程语言的教程,适合初学者和有一定编程基础的开发者。这本书详细介绍了Python 3的各种特性,包括语法、数据结构、函数、类、模块、异常处理、输入/输出、网络...

    DiveIntoPython

    《Dive Into Python》是一本深受编程初学者和有经验开发者喜爱的Python编程教程。这本书以其深入浅出的讲解方式,让学习者能够快速掌握Python编程语言的核心概念和实际应用,特别是对于想要涉足Web开发领域的读者,...

    diveintopython3

    在“diveintopython3-master”这个压缩包中,包含了这本书的所有源代码示例。通过这些代码,我们可以学习到以下关键知识点: 1. **Python基础**:包括变量、数据类型(如整型、浮点型、字符串、列表、元组、字典)...

    Dive Into Python 中文译文版

    PDF版本的《Dive Into Python 中文译文版》(diveintopython-pdfzh-cn-5.4b.zip)提供了完整的书籍内容,涵盖了Python的基础知识到高级特性。书中通过实际案例引导读者深入学习,包括但不限于变量、数据类型、控制...

    Dive Into Python 3 深入Python3中文版 PDF格式

    深入Python3中文版 PDF格式 深入 Python 3》 的内容涵盖了 Python 3 及其与 Python 2 的区别。相对《深入 Python》而言,它 20% 的内容进行了修订,80% 的内容是全新的。

    Dive Into Python 2 中文版

    《Dive Into Python 2 中文版》是一本深度探讨Python编程语言的教程,适合已经有一定编程基础,希望深入理解Python特性和应用的读者。这本书以其详尽的解释和丰富的实例,为Python初学者和进阶者提供了全面的学习...

Global site tag (gtag.js) - Google Analytics