List, Dictionaries & Tuples Basics

2009/10/10 發表迴響

語法簡介

  1. List
    1. var1 = [] (空的 List)
    2. var2 = [1, 2, 3] (assignment)
    3. var3 = ['str1', 'str2', ['sub1', 'sub2']] (巢狀宣告)
    4. var2.append(4)  and var2.extend([5, 6, 7])   (擴充語法)
    5. del var2[2]  and del var2[i:j] and var2.pop()  (shrinking)
    6. [1, 2, 3] + [4, 5, 6] (concatenation)
    7. print var2[2] (indexing methon)
    8. var3[0:2] = ['new1', 'new2'] (刪除+新置入字串)
  2. Dictionary
    1. D1 = {} (空的 Dictionary)
    2. D2 = {‘key1′: 1, ‘key2′:2} (Key & the value)
    3. D2['ke3'] = 3 (新增)
    4. del D2['key3'] (刪除)
    5. print D2['key1'] (Indexing)
    6. D3 = dict (zip (key_list, value_list)) (construction)
    7. ‘key1′ in D2   and  D2.has_key(‘key1′   (the memebership testing)
  3. Tuple
    1. Tuple (almost = list), A Group/Set of items. 但內容不可變。
    2. () (空的 tuple)
    3. t1 = (0, ‘str’, 1.2, 3) (four elements)
    4. t1 = 0, ‘str’, 1.2, 3 (另表達一種方法)
    5. (1, 2) * 4 (重覆)
      (1, 2, 1, 2, 1, 2, 1, 2)
    6. t1[0] (indexing)

當 Dictionary 遇上 simplejson

Source Code:

from django.utils import simplejson
d2 = {‘key1′:1, ‘key2′:2}
test_string = simplejson.dumps(d2)

Output:
{『key2″: 2, 『key1″: 1}

類別:Python Basics 標籤:, , , ,

Python Modules 大小事

2009/10/06 發表迴響

File Import

  1. 任一 .py附屬檔名 的python原始碼檔案都是一個module。
  2. 在同一 session (process)裏,相同的module只會實際被 import一次。
  3. Simple Import Statements
    (1) import myfile
    myfile.funcCall()

    (2) from myfile import myAttr
    print myAttr

    (3) from myFile import *  # 複製所有的變數
    (4) delete myModule # just like unload the specified moudle

  4. Python Module 路徑搜尋順序
    (1) Top-Level File的所在目錄
    (2) PYTHONPATH 定義的目錄
    (3) Standard Library 的目錄
    (4) .pth 檔案內的內容
    .pth (即為 path之意) 為 Python特有之檔案格式。其內存有使用者定義的額外搜尋目錄。

Package Import

  1. Import 也可以直接指命目錄名稱,某目錄下意同某一 Package。如:
    import myDir # import 『myDir』 這個 package
    import myDir2.myModule
  2. 如要 import package, 則要 import 的目錄內需含有 __init__.py 檔。否則 import 將會失敗。
類別:Python Basics 標籤:, ,

Android, iPhone 及 Symbian OS 開發環境之比較

2009/10/05 發表迴響

Android

iPhone

Nokia

作業系統

Android 1.5

Android 1.6

(Linux Kernel Based)

iPhone OS

Series 40 Platform

S60 Platform

開發工具

Eclipse

Xcode

NetBeans / Eclipse

Carbide.c++(Eclipse)

Language

Java

Objective-C

J2ME

Symbian C++

Java

Python

Python String (字串) 基礎

2009/10/02 發表迴響

字串表示式:

Python提供兩種字串表式式  – 雙引號/單引號。例如: 『這是一個雙引號字串』 ,   ‘這是一個單引號字串’

然而對於Python語言而言,使用 單引號/雙引號 對於內部運作並無不同。其最主要目的為「讓你在字串表示中能更方便的使用引號」。

例如: ‘這個字串裏面請特別注意 『這裏』 ‘

整數的字串格式化

str(my_int) or repr(my_int)

長字串的使用

如果有跨行的字串則可於字串前後使用連續三個單引號來包含字串內容。例如:

』’這是第一行
這是第二行
這是第三行』’

另外因字串前後已有三個引號,故在長字串內的引號(不論單/雙)皆不需要再使用跳脫字元。例如:

』’這是第一行
這是第二行,但是不用』跳』囉
這是第三行』’

Raw String (myString = r’This is a raw string’)

Raw string最主要目的為「使字串用的跳脫字元失效」,也就是如,字串內的\n等,不再定義為它原本的換行功能。幾個範例如下:

>>>print r’String line one, \nString Line2′
String line one, \nString Line2

>>>print r’Hello, Who\’s site here?』
Hello, Who\’s site here?

跳脫字元的失效有一個唯一例外,就是最後一個字元的位置時。
>>>print r’Hello, this is invalid statement\’
為什麼? 因這樣的語法將使得直譯器搞不清楚字串是哪裡結束阿!?

Unicode String

>>>print u’this is the unicode string’
this is the unicode string

字串的連結

>>>print ‘the part01 ‘ + ‘ concated with this one’
the part01 concated with this one

字串格式化

>>>stmt = 『%s, this is a %s sample』
>>>values= (‘Part01′, ‘Python String’)
>>>print stmt % values
Part01, this is a Python String sample

類別:Python Basics 標籤:,

Joomla v1.5 預設樣板

2009/07/31 發表迴響

The default template Jommla v1.5 offered.

  1. beez
    beez_template_thumbnail
  2. ja_purity
    ja_purity_template_thumbnail
  3. rhuk_milkyway
    rhuk_milkyway_template_thumbnail
Follow

Get every new post delivered to your Inbox.