2011年10月25日 星期二

[Javascript]語法基礎

  • 變數宣告  
    『Javascript』的變數是『弱型別』,使用時只需要用『var』宣告這是一個變數,不用宣告型別。

    變數命名規則 :
     a.首字要是『英文大、小寫字母』、『"_"(底線)』或『$』。

var VariableName[ = "Value"];  





    事實上就算是沒有作任何宣告也可以。
  • Case sensitive
    不管變數、函式名稱......都區分大小寫
  • 程式碼區塊要用『{.......}』包住

if (<Boolean Expression>) {                            
 
.........;
.........;
}  











  • 註解符號
    分成單行跟多行註解兩種符號。

// 單行註解                                            
/*
   多 行 註 解
*/









  • 程式碼包在『<script>.....</script>』之間

<head> 
<script language="javascript">                         
    ...............;
    ...............;
</script>
</head> 











  • 每行結尾用『;』隔開

    也可以不用,但是用比較好。

2011年10月19日 星期三

[Learning Python] Reference

  • Reference :






  •              1. Programming in Python 3: A Complete Introduction to the Python Language 
                   2. Learning Python: Powerful Object-Oriented Programming (4th Edition)    
                   3. Learning Python,3rd Edition 
                   4. Python for Unix and Linux System Administration 
                   5. Python Cookbook (2nd Editiom)
                   6. Python Testing Cookbook
                   7. The Python Standard Library by Example (Developer's Library)
                   8. Programming Python(4th Edition)
                   9. A Byte of Python v1.92 (for Python 3.0 
                 10. Python Documentation 
                 11. The Python Standard Library 
                 12. PyPI - the Python Package Index
                 13. Python 3 技術手冊
                 14. Beginning Python: Using Python 2.6 and Python 3.1 

    [Learning Python] Collection Data Type -- List(列表)

    1.Collection Data Type and Sequence Data Type

        由於『List』中的元素可以是不同的資料型別物件,所以與『Tuple』都是屬於『Collection Data Type』; 同時,『List』中的各元素的存放方式與『String』一樣都是以循序的方式存放,所以『List』也是屬於『Sequence Data Type』的一種,因此只要是『Sequence Data Type』可以用的操作方法,如『Index』、『Slice』...等,都可以用在『List』上面。


    2.可直接變更內容

        『String』與『Tuple』是屬於不可變的資料型別,所有看起來像是變更內容的操作其實是變成一個新物件再將原變數參照到新物件。

         而『List』就可以直接變更原『List Object』的內容,所以有一些『List』專用的內容原地變更用操作並不能用在同為『Sequence Data Type』的『String』與『Tuple』身上。


    3.每一個元素都是『Reference(參照)』

        與『Tuple』一樣,『List』中各元素的值實際上是存放在『List』中各元素所『Reference』過去的物件那邊,而『List』這邊存放的是各元素的『Reference』。

    4.建立『 List』的語法

        建立『 List』的語法如下,

    ListName=[Item1,Item2,Item3....]       




        事實上用『[....]』 括起來,其中的元素用『,』隔開,Python就知道這個物件是『List』。

    [Item1,Item2,Item3....]                

    2011年10月18日 星期二

    [Linux]...... 應該是超完整版的Unix 發展大系

    http://www.levenez.com/unix/unix_a4.pdf

    @@~~好在考試不考這個.........

    『1977年之後 UNIX Fork 出那些支系........』 ~~~~~~ 神經病才記得住!!!!!!!!

    [Learning Python] Sequence Data Type - String - 字串輸出格式控制

        Python 提供兩種字串輸出格式控制的方式,第一種與『C Language』"printf" 的輸出格式控制方法相類似;第二種則需要 call 字串輸出格式的 method。

        a. 像『C Language』"printf" 的 String formatting expressions

            這個方式需要用到下列資料型別的代碼

    Code                                  Meaning
    -------------------------------------------
    s                            String (or any object’s str(X) string)
    r                            s, but uses repr, not str
    c                           Character
    d                          Decimal (integer)
    i                            Integer
    u                          Same as d (obsolete: no longer unsigned)                                  
    o                         Octal integer
    x                          Hex integer
    X                          x, but prints uppercase
    e                          Floating-point exponent, lowercase
    E                          Same as e, but prints uppercase
    f                          Floating-point decimal
    F                         Floating-point decimal
    g                         Floating-point e or f
    G                         Floating-point E or F
    %                        Literal %




















        語法如下,

    '%[flags][width][.precision]typecode'    %(Value)                   



        整數的輸出格式控制大概會遇到下列幾種狀況,


    #整數
    >>> '%d' %(77)
    '77'

    #%+<typecode >   :  加正負號
    >>> '%+d  %+d' %(100,-100)
    '+100  -100'

    #%<Width><typecode >   :   控制輸出位數
    >>> '%4d %6d %2d' %(100,100,100)
    ' 100    100 100'

    #%<0Width><typecode >   :   控制輸出位數,不足位補'0'                                                 
    >>> '%04d %06d %02d' %(100,100,100)
    '0100 000100 100'

    #%<-Width><typecode >   :   控制輸出位數,向左靠攏
    >>> '%4d %6d %2d' %(100,100,100)
    ' 100    100 100'
    >>> '%-4d %-6d %-2d' %(100,100,100)
    '100  100    100'








     

        浮點數的輸出格式控制大概會遇到下列幾種狀況,

    #浮點數
    >>> '%f' %(100.123)
    '100.123000'

    #%+<typecode >   :  加正負號
    >>> '%+f  %+f' %(100.123,-100.123 )
    '+100.123000  -100.123000'

    #%<Width.Precidion><typecode >   :   控制輸出位數
    >>> '%6.3f  %2.2f' %(100.123,100.123 )
    '100.123  100.12'


    #%<0Width.Precidion><typecode >   :   控制輸出位數,不足位補'0'                                                 

    >>> '%07.3f  %08.3f' %(100.123,100.123 )
    '100.123  0100.123'


    #%<-Width.Precidion><typecode >   :   控制輸出位數,向左靠攏

    >>> '%08.3f  %-8.3f' %(100.123,100.123 )
    '0100.123  100.123 '
























        『Width』: 總寬度。
        『Precidion』: 小數部份的位數。

        位數超過所設定的總寬度時,整數部份不會被截斷 ; 小數部份所設定的位數若比實際位數少,則超過的部份會被截斷。

    >>> '%2.2f' %(100.125)                                               
    '100.12'             



    • 配合字典控制輸出
             語法如下,

    '%(key)[flags][width][.precision]typecode'    %{key:Value}         




        後面Key所對應的值會被送到前面key的位置,


    >>> '%(key1)d  %(key2)f' %{"key1":123,"key2":123.321}                 
    '123  123.321000'    




        b. call method 來控制字串輸出的格式

        語法如下,

    '{FieldName}'.format(Value)              



          『FieldName』可以使用『位置代號』或『keyword (其實跟剛剛配合字典的方法差不多)』的對應來控制輸出。


    '{Position} {Keyword}}'.format(PValue,,Keyword=KValue)




        使用『位置代號』會依序到後面取用對應的值,使用『keyword 』則取與『Keyword』所對應的值。

    >>> '{0} {1}'.format('string',1.23)
    'string 1.23'
    >>> '{Keyword1} {Keyword2}'.format(Keyword1=3.21,Keyword2='KValue')
    '3.21 KValue'
    >>> '{0} {Keyword}'.format('String',Keyword='KValue')
    'String KValue'









        『位置代號』可以與字典搭配使用

    >>> 'Dic&Pos {0[Key]}'.format({"Key":'Value'})
    'Dic&Pos Value'





        還可以使用『keyword 對應』搭配字典

    >>> 'Dic&Keyword {Keyword[Key]}'.format(Keyword={"Key":'KValue'})
    'Dic&Keyword KValue'




            不過新的語法可以直接省略掉『FiledName』

    >>> '{} {}'.format('string',1.23)                    
    'string 1.23'






          如果是序列型別的資料,可以使用索引或『Slice』的方式取用序列內的元素。

    >>> List=('LVaule1','LVakue2')
    >>> List
    ('LVaule1', 'LVakue2')
    
    >>> Tuple=['TVaule1','TVakue2']
    >>> Tuple
    ['TVaule1', 'TVakue2']
    
    >>> String='AString'
    >>> String
    'AString'
    
    >>> 'String={2[2]} Tuple={1[1]} List={0[0]}'.format\                                       
    ... (List,Tuple,String)
    'String=t Tuple=TVakue2 List=LVaule1'

















        『slice』的話要先在後面處理掉

    >>> 'String={2} Tuple={1} List={0}'.format \
    ... (List[0:1],Tuple[1:2],String[2:3])
    "String=t Tuple=['TVakue2'] List=('LVaule1',)"
     
    #在輸出的位置『Slice』會發生『ValueErroe』的例外
    >>> 'String={2[2:3]} Tuple={1[1:2]} List={0[0:1]}'.format \
    ... (List,Tuple,String)
    Traceback (most recent call last):
      File "", line 2, in 
    ValueError: Missing ']' in format string
    















        如果要進一步控制輸出的位數及表示方式語法如下,


    '{FieldName!ConverFlag:FormatSpec}'.foramt(<Value>)
    





        『ConverFlag』則用『r』、『s』、『a』表示要同時 call 『repr()』、『str()』、『ascii()』進行格式轉換。
       
        『FormatSpec』則是用來進行輸出位數、對齊方式.....等控制,語法如下,

    [Align][0][Width][.Precidion][TypeCode]    
    


       


      『Align』用來控制向左對齊(<)、至中(^)或向右對齊(>)。

        需要不足位補『0』的話就在整數位數前加上『0』。

    #只設定總寬度,預設會靠右對齊 
    >>> '{0:10}.format(100.123)                     
    '   100.123'
     
    #未指定『TypeCode』浮點數會以指數形表示
    >>> '{0:10.2}'.format(100.123)
    '     1e+02'
     
    #加上『TypeCode』
    >>> '{0:10.2f}'.format(100.123)
    '    100.12'
     
    #未設定小數部份位數,浮點數的小數部份會補『0』
    >>> '{0:10f}'.format(100.123)
    '100.123000'
    >>> '{0:010f}'.format(100.123)
    '100.123000'
    #要在整數部份補『0』要設定小數的位數 
    >>> '{0:010.2f}'.format(100.123)
    '0000100.12'
    #整數的三種對齊方式 
    >>> '{0:<10} {1:^10} {2:>10}'.format(66,77,88)
    '66             77             88'
    >>> '{0:<010} {1:^010} {2:>010}'.format(66,77,88)
    '6600000000 0000770000 0000000088'
    #浮點數的三種對齊方式 
    >>> '{0:<10.2f} {1:^10.2f} {2:>10.2f}'.format(12.3,12.3,12.3)
    '12.30        12.30         12.30'
    >>> '{0:<010.2f} {1:^010.2f} {2:>010.2f}'.format(12.3,12.3,12.3)
    '12.3000000 0012.30000 0000012.30'
     
    #thousand-separator syntax for numbers
    >>> '{0:,d}'.format(12345678)
    '12,345,678'
    >>> '{0:15,f}'.format(12345678.9123)
    '12,345,678.912300'
    >>> '{0:15,.4f}'.format(12345678.9123)
    '12,345,678.9123'
     

    2011年10月11日 星期二

    [Learning Python] Collection Data Type -- Tuple(元組)

    A. Tuple

        1. Tuple 是個可以將不同資料型別的 Object  Reference 放到一個單一序列的集合,而 Tuple 一旦被建立其內容就無法被改變。

        2. 建立 Tuple 的語法如下,

    (Item1 , Item2 , Item3,....)                             




    tupleName = (Item1 , Item2 , Item3,....)                             



        3. 可以用索引的方式取用 Tuple 中的元素

    定義Tuple
    >>> demoTuple=(1,2,3,'a','B','Ayana','satomi')                                         

    印出整個Tuple
    >>> print demoTuple
    (1, 2, 3, 'a', 'B', 'Ayana', 'satomi')

    印出第二個元素
    >>> print demoTuple[1]
    2

    印出最後一個元素
    >>> print demoTuple[-1]         
    satomi
    >>>
                     


















         4. 與其他 Object 合併結合成為新的 Tuple

    定義整數物件
    >>> x=123

    定義另一個 Tuple
    >>> otherTuple=(1,2,3,4)

    合併
    >>> demoTuple+(x,otherTuple)
    (1, 2, 3, 'a', 'B', 'Ayana', 'satomi', 123, (1, 2, 3, 4))                                                     











        5. 要變更 Tuple 的資料項有兩個方法

            a. list()

            b. Temp Tuple 

        6. tupleName.count(Target)

            計算 Target 在 Tuple 中出現的次數。

        7.  tupleName.index(Target)

            回傳 Target 在  Tuple 中首次出現的位置,若 Target 不存在於 Tuple 中則拋出『ValueError』Exception。

       8. Tuple 可使用的運算 :
                a. concatenation : 『+』。
                b.replication :『*』。
                c. slice : 『[]』。
               d. membership operation : 『in』、『not in』。
              e. augmented assignment operation : 『+=』、『-=』。
              f. binary comparison operation : 『<』、『<=』、『==』、『!=』、『>=』、『>』。



        

    2011年10月10日 星期一

    [Learning Python] Control Flow

    1. Conditional Branching

        毫無意外的 Python 的 conditional branching還是由 if  來提供。

        if 的一般語法如下,

    if <Boolean Expression 1>:
        suite_1
    elif <Boolean Expression 2>:                                                         
        suite_2
    elif <Boolean Expression 3>:
        suite_3
        ................
        ................
        ................
    else:
        Default_suite 













    2. Ternary  Expression

        實作 Ternary Expression 的 if 語法。

    <Var = Value_A> if <Boolean Expression> else <Value_B>                 




         或

    <Var = Value_A> if not <Boolean Expression> else <Value_B>                 



    3.Multiway  Branching

        Multiway Branching 也可以用字典來實作

    check = Value
    print({Value1:OutputInfo1,
                 Value2:OutputInfo2,
                 Value3:OutputInfo3,
                  ...................
                  ...................   

                 ValueN:OutputInfoN}[check])                                               













    2. while loops

        while loops 的語法如下,

    while <Boolean Expression>:                     
         while suite
    else:
        else suite                                 








    3.for loops

        for loops  的語法如下,

    for <Target> in <Object>:                     

        for suite
    else:
        else suite                                 








    • Iteration(迭代)
            所謂『Iteration』是指重複進行同一項作業,直到所有可處理的對象都被處理完畢為止,其實也就是『重複操作』的意思,不過,用『迭代』稱呼,光看就覺得.....『 好厲害啊...迭代耶~~~』,所以後面還是稱『Iteration』為『迭代』。

        『Sequence Data Type』類的資料型別,如『String』、『List』、『Tuple』都可以利用『for loops』執行『迭代』 重複對其中的所有元素執行相同的操作。

        『for loops』的『迭代』語法如下,

        for loops  的『迭代』語法如下,

    for <ControlVariable> in <Object>:          
        suite



     
     
        或

    for <ControlVariable> in <Object>: suite   



        以『迭代』的方式列印字串

    >>> for I in 'Iteration': print I        
    ... 
    I
    t
    e
    r
    a
    t
    i
    o
    n
       



       






     
             以『迭代』的方式列印『List』


    >>> for I in ['Iteration1','Iteration2','Iteration3']: print I
    ... 
    Iteration1
    Iteration2
    Iteration3
    



       

      

             以『迭代』的方式列印『Tuple』


    >>> for I in ('Iteration1','Iteration2','Iteration3'): print I
    ... 
    Iteration1
    Iteration2
    Iteration3
    



       



        可以用『迭代』的方式一一將序列中的元素印出來,當然也可以作別的事情。










    以『迭代』的方式列印字串


            將『String』

    [Learning Python] Programming in Python

                  String

                      建立字串
                      不同型態的引號
                     字串輸出格式控制 
               
                  List
    •  Collection Data Type
                   List

    2011年10月6日 星期四

    [Learning Python] 基本預備知識

        在開始動手寫之前,先整理一下需要知道的預備知識。


    1. Python2 & Python3  together

        我的實驗環境是『Ubuntu 11.04』上面已經裝好了『Python 2.7.1+』,『Python 2』直譯器的所在位置是『/usr/bin/python』。

    $ python -V
    Python 2.7.1+
     
    $ which python
    /usr/bin/python                                         
    






        




        如果直接從『 Ubuntu』的套件庫安裝『Python 3』,雖然『Python 3』直譯器也是會被安裝在『/usr/bin』目錄中,但是名稱會變成『python3』因此同時並存並不會發生衝突,所以就直接安裝啦~~~


    $ sudo apt-get install python3                          
     
    $ python -V
    Python 2.7.1+
    $ python3 -V
    Python 3.2
     
    $ which python
    /usr/bin/python
    $ which python3
    /usr/bin/python3              
    







     











    2.誰要來解釋Script 的內容?

        當妳在『Linux』作業系統執行一支 『可執行檔』時,妳所使用的『Shell』會先讀取 『可執行檔』最開頭的兩個『Byte』。

        如果最開頭的兩個『Byte』是『#!』,也就是整支程式第一行最開頭的兩個『Byte』是『#!』,『Shell』會假設這是一支『Script』,這支 『Script』第二行起以後的內容『Shell』就會交由『#!』這一行所標示的直譯器來解釋。

        所以如果所撰寫的『Script』是 for『Python 2』,『Script』的第一行就是,

    #!/usr/bin/python                                                




    #!/usr/bin/env python                                   



        如果所撰寫的『Script』是 for『Python 3』,『Script』的第一行就是,

    #!/usr/bin/python3                                                




    #!/usr/bin/env python3                                   



        當然,如果要讓一支『 Script』變成『可執行檔』需要賦予這支『Script』可執行屬性,

    chmod  +x  <ScriptName>                                  



    3.副檔名

        雖然在『Linux』作業系統中『副檔名』並不具檔案類型關聯的功能,不過為了方便識別,習慣上我們會以『.py』做為『Python』程式的副檔名,以『.pyw』做為『Python GUI』程式的副檔名。

    4.如何執行

        如果『Python』Script 已經被賦予了『可執行屬性』可以直接以完整路徑來執行這支 Script,

    $ /PATH/.../<ScriptName>                              


     
        或切換工作目錄到 Script 所在目錄再執行它,
     
    $ ./<ScriptName>                              


     
        如果『Python』Script未被賦予『可執行屬性』,可以把它當成『Python』直譯器的參數"喂"給『Python』直譯器
     
    $ python   <ScriptName>                            


                          
    $ python3   <ScriptName>                          


     
    5.註解

        『 Python』有兩種註解符號,單行註解符號是『#』,多行註解則是以『'''』開頭『'''』結尾或『"""』開頭『"""』結尾。


    #!/usr/bin/python3  
    #
    #『 #』之後的文字都會被視為註解文字
    #
    '''   從這一行開始
       
        之間所有的文字都會被視為註解文字                                          
        
    '''   到這一行結束

    """ 這樣
          也是
          多行
          註解
    """          
















        

    6. 中文註解

        如果是使用『Python 2』直譯器,要在 Script 加中文註解,需先作如下宣告 ,
           
    #-*- coding: utf-8 -*                       



        否則會出現如下錯誤,
           
    $ python notest.py
      File "notest.py", line 3
    SyntaxError: Non-ASCII character '\xe4' in file notest.py on line 3, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details


    $ python3 notest.py
    中文註解測試
                   




        

        『Python 3』就不需要了。

    7.  資料型別(Data Type) / 物件型別(Object Type)

       由於 Python 將所有的資料(Data)都當成物件(Object),所以之後無論是 Data Type 或 Object Type 其實都是指同一件事,不過之後都一律統一稱為『 Data Type』。

        另外 Python  並不需要對變數的『 Data Type』作宣告, Python 會依照變數賦值時所用的語法來決定變數的『 Data Type』。

    8. 變數(variable)與變數賦值

        雖然行為是一樣的,但 Python 其實並沒有『變數』這種東西,她是用『Object reference(物件參照)』的方法處理『變數賦值』這個行為。

        與多數程式語言一樣,在進行所謂『變數賦值』這個行為時 Python會用到『=』這個符號,,例如,

          
    String1 = 'String Object'                     



        由於給定的值被『'』刮起來,所以 Python會建立一個『str』物件 -- 'String Object',及一個指向這個物件的  『Object reference(物件參照)』-- String1  。

       值給定後 Python 會建立一個物件及指向這個物件的『Object reference(物件參照)』,也就是...
           
    變數賦值......

    VariableName = <Vaule>                                                 


    其實就是建立『Object reference(物件參照)』


    Object reference = <Vaule>               


      


     



        所以有『 Data Type』的是物件本身而非『Object reference(物件參照)』。

    9. Identifier (識別字)

        變數名稱也就是『Object reference(物件參照)』的名稱稱為『 Identifiers (識別字)』或是『name』,命名時需依據下列規則,

        a. 首字要是一個英文字母(其實可以是任何Unicode中各種語系的字母,不過如果沒有特殊的嗜好,最好就限定只用英文大小寫字母會)或『_ (under line)』。
        b. 第二個字元起可以使用所有 Unicode 所定義的字元符號,但空白字元符號除外。
        c. 大小寫有別(Case Sensitive),沒有長度限制。
        d. 不能與 Python 的關鍵字(Keywords)撞名。

    10. Compound statement (複合敘述)and 縮排

        每個 Compound statement 的首行會以『:』結尾,而 Python 是以縮排來決定 Block(程式區塊)的邊界,同時也是以縮排與否來判定 suite 是不是屬於某個 Compound statement ,所以屬於 if 、while 這一類Compound statement的 suite 一定要作縮排。

         可以用『tab』或『space』來縮排,不過習慣上每一層級縮排會用四個『space』。


    Compound Statement:                    
    
        suite                              





    11.Logical Operation(邏輯運算)

        a. Identity  Operator : 『is』、『is not』。
        b. Binary Comparison Operator : 『<』、『<=』、『==』、『!=』、『>=』、『>』。
        c. Membership Operator : 『in』、『not in』。
        d. Logical Operator : 『and』、『not』、『or』。
    Powered By Blogger