} } }

    Head First Python 进修笔记(第一章)

    添加时间:2013-7-11 点击量:

    应用IDLE来帮助进修Python


    Python法度员凡是把内置函数称为BIF,print() BIF的感化是吧消息显示到标准输出(凡是是屏幕)



    1 printYou can experiment with code within IDELs shell Coool, eh?
    2 if 43>42
    3 printDont panic!


     有效地应用IDEL



    • TAB完成:主动完成语句

    • 回退代码语句:Win/Linux下应用ALT+P/N,Mac下应用Control+P/N

    • 编辑回退语句

    • 调剂IDLE的推荐项


    创建简单的Python列表


    要遵守的四个步调:



    • 在数据两边加引号

    • 用都好将列表项与下一项隔开

    • 再列表的两边加上开端和停止的中括号

    • 应用赋值操纵(=)将这个列表赋至一个标示符


    Python的变量标识符没有类型



    movies =[The Holy GrailThe Life of BrianThe meaning of Life ]


    列表就像是数组


    应用中括号记法接见列表数据



    print(movies[1])


    可应用print() BIF再屏幕上显示一个列表,应用len()BIF得出列表中有几许个数据项



    >>> cast =[CleesePalinJonesIdle]
    
    >>> print(cast)
    [
    CleesePalinJonesIdle]
    >>> print(len(cast))
    4
    >>> print(cast[1])
    Palin


    再列表末尾增长一个数据项应用办法append(),从列表末尾删除数据应用办法(pop()),在列表末尾增长一个数据项凑集extend(); 



     1 >>> cast.append(Gilliam
    2 >>> print(cast)
    3 [CleesePalinJonesIdleGilliam]
    4 >>> cast.pop()
    5 Gilliam
    6 >>> print(cast)
    7 [CleesePalinJonesIdle]
    8 >>> cast.extend([GilliamChapman])
    9 >>> print(cast)
    10 [CleesePalinJonesIdleGilliamChapman]


    删除特定的数据项,在某些特定的地位插入一个元素



    >>> cast.remove(Chapman
    >>> print(cast)
    [
    CleesePalinJonesIdleGilliam]
    >>> cast.(0,Chapman
    >>> print(cast)
    [
    ChapmanCleesePalinJonesIdleGilliam]


    向列表增长更多半据


    在之前的movies每一项前增长时候


    代码一:



    >>> movies.(1,1975
    >>> movies.(3,1979
    >>> movies.append(1983
    >>> print(movies)
    [
    The Holy Grail, 1975, The Life of Brian, 1979, The meaning of Life, 1983]


    办法二:



    >>> movies=[The Holy Grail, 1975, The Life of Brian, 1979, The meaning of Life, 1983]
    
    >>> print(movies)
    [
    The Holy Grail, 1975, The Life of Brian, 1979, The meaning of Life, 1983]


    处理惩罚列表数据


    for迭代处理惩罚数据



    >>> for each_flick in fav_movies:
    
    print(each_flick) 重视前面有TAB


    The Holy Grail
    The Life of Brian


    For轮回处理惩罚随便率性大小的列表


    while和for的对比


    while:



    >>> count=0
    
    >>> while count<len(movies):
    print(movies[count])
    count
    =count+1


    The Holy Grail
    1975
    The Life of Brian
    1979
    The meaning of Life
    1983


    for:



    >>> for each_item in movies:
    
    print(each_item)


    The Holy Grail
    1975
    The Life of Brian
    1979
    The meaning of Life
    1983


    在列表中存储列表


    列表可以存放混淆类型的数据,也包含任何列表



    movies=[The Holy Grail, 1975, Terry Jones & Terry Gilliam,91,[Graham Chapman,[Michael PalinJohn CleeseTerry GilliamEric IdleTerry Jones]]]
    
    print(movies[4][1][3])


    用for轮回来处理惩罚这个列表



    >>> movies=[The Holy Grail, 1975, Terry Jones & Terry Gilliam,91,[Graham Chapman,[Michael PalinJohn CleeseTerry GilliamEric IdleTerry Jones]]]
    
    >>> print(movies[4][1][3])
    Eric Idle
    >>> print(movies)
    [
    The Holy Grail, 1975, Terry Jones & Terry Gilliam, 91, [Graham Chapman, [Michael PalinJohn CleeseTerry GilliamEric IdleTerry Jones]]]
    >>> for each_item in movies:
    print(each_item)


    The Holy Grail
    1975
    Terry Jones
    & Terry Gilliam
    91
    [
    Graham Chapman, [Michael PalinJohn CleeseTerry GilliamEric IdleTerry Jones]]


    在列表中查找列表


    isinstance() 用来断定当前处理惩罚的数据是不是某类型



    for each_item in movies:
    
    if isinstance(each_item,list):
    for nested_item in each_item:
    if isinstance(nested_item,list):
    for deeper_item in nested_item:
    print(deeper_item)
    else:
    print(nested_item)

    else:
    print(each_item)


    The Holy Grail
    1975
    Terry Jones
    & Terry Gilliam
    91
    Graham Chapman
    Michael Palin
    John Cleese
    Terry Gilliam
    Eric Idle
    Terry Jones


    在Python中创建一个函数


    def 函数名(参数):


      函数代码组



    >>> def print_lol(the_list):
    
    for each_item in the_list:
    if isinstance(each_item,list):
    print_lol(each_item)
    else:
    print(each_item)


    >>> print_lol(movies)
    The Holy Grail
    1975
    Terry Jones
    & Terry Gilliam
    91
    Graham Chapman
    Michael Palin
    John Cleese
    Terry Gilliam
    Eric Idle
    Terry Jones


    我俩之间有着强烈的吸引力。短短几个小时后,我俩已经明白:我们的心是一个整体的两半,我俩的心灵是孪生兄妹,是知己。她让我感到更有活力,更完美,更幸福。即使她不在我身边,我依然还是感到幸福,因为她总是以这样或者那样的方式出现在我心头。——恩里克·巴里奥斯《爱的文明》
    分享到: