List(列表) 例如:[1,2,3],[1,2,3,[1,2,3],4]
Dictionary(字典) 例如:{1:"nihao",2:"hello"}
Tuple(元組) 例如:(1,2,3,abc)
File(文件) 例如:f = open(a.txt,rw)
python的基本數據類型:
1.列表list
list是處理一組有序項目的數據結構,即你可以在一個列表中存儲一個 序列 的項目。列表中的項目應該包括在方括號中,這樣Python就知道你是在指明一個列表。
一旦你創建了一個列表,你可以添加、刪除、修改或是搜索列表中的項目。由於你可以增加或刪除項目,我們說列表是 可變的 數據類型。
shoplist = ['apple', 'mango', 'carrot', 'banana']
for item in shoplist:
print item,
2.元組tuple
元組和列表十分類似,只不過元組和字符串一樣是 不可變的 即你不能修改元組。元組通過圓括號中用逗號分割的項目定義。元組通常用在使語句或用戶定義的函數能夠安全地採用一組值的時候,即被使用的元組的值不會改變。
含有0個或1個項目的元組。一個空的元組由一對空的圓括號組成,如myempty = ()。然而,含有單個元素的元組就不那麼簡單了。你必須在第一個(唯一一個)項目後跟一個逗號,這樣Python才能區分元組和表達式中一個帶圓括號的對象。即如果你想要的是一個包含項目2的元組的時候,你應該指明singleton = (2 , )
age = 22
name = 'Swaroop'
print '%s is %d years old' % (name, age)
print 'Why is %s playing with that python?' % name
3.字典dict
類似Map,注意,你只能使用不可變的對象(比如字符串)來作爲字典的鍵,但是你可以把不可變或可變的對象作爲字典的值。基本說來就是,你應該只使用簡單的對象作爲鍵。
ab = { 'Swaroop' : '[email protected]',
'Larry' : '[email protected]',
'Matsumoto' : '[email protected]',
'Spammer' : '[email protected]'
}
print "Swaroop's address is %s" % ab['Swaroop']
# Adding a key/value pair
ab['Guido'] = '[email protected]'
# Deleting a key/value pair
del ab['Spammer']
print '\nThere are %d contacts in the address-book\n' % len(ab)
for name, address in ab.items():
print 'Contact %s at %s' % (name, address)
if 'Guido' in ab: # OR ab.has_key('Guido')
print "\nGuido's address is %s" % ab['Guido']
4.序列
列表、元組和字符串都是序列。序列的兩個主要特
點是索引操作符和切片操作符。索引操作符讓我們可以從序列中抓取一個特定項目。切片操作符讓我們能夠獲取序列的一個切片,即一部分序列。
shoplist = ['apple', 'mango', 'carrot', 'banana']
# Indexing or 'Subscription' operation
print 'Item 0 is', shoplist[0]
print 'Item 1 is', shoplist[1]
print 'Item 2 is', shoplist[2]
print 'Item 3 is', shoplist[3]
print 'Item -1 is', shoplist[-1] #最後一個
print 'Item -2 is', shoplist[-2]
# Slicing on a list
print 'Item 1 to 3 is', shoplist[1:3]
print 'Item 2 to end is', shoplist[2:]
print 'Item 1 to -1 is', shoplist[1:-1]
print 'Item start to end is', shoplist[:]
# Slicing on a string
name = 'swaroop'
print 'characters 1 to 3 is', name[1:3]
print 'characters 2 to end is', name[2:]
print 'characters 1 to -1 is', name[1:-1]
print 'characters start to end is', name[:]
索引同樣可以是負數,在那樣的情況下,位置是從序列尾開始計算的。因此,shoplist[-1]表示序列的最後一個元素而shoplist[-2]抓取序列的倒數第二個項目。
切片操作符是序列名後跟一個方括號,方括號中有一對可選的數字,並用冒號分割。注意這與你使用的索引操作符十分相似。記住數是可選的,而冒號是必須的。切片操作符中的第一個數(冒號之前)表示切片開始的位置,第二個數(冒號之後)表示切片到哪裏結束。但並不包含尾。。。
5.引用
類似java的概念,對象以引用的方式使用。
print 'Simple Assignment'
shoplist = ['apple', 'mango', 'carrot', 'banana']
mylist = shoplist # mylist is just another name pointing to the same object!
del shoplist[0]
print 'shoplist is', shoplist
print 'mylist is', mylist
# notice that both shoplist and mylist both print the same list without
# the 'apple' confirming that they point to the same object
print 'Copy by making a full slice'
mylist = shoplist[:] # make a copy by doing a full slice
del mylist[0] # remove first item
print 'shoplist is', shoplist
print 'mylist is', mylist
# notice that now the two lists are different
6.字符串str
name = 'Swaroop' # This is a string object
if name.startswith('Swa'):
print 'Yes, the string starts with "Swa"'
if 'a' in name:
print 'Yes, it contains the string "a"'
if name.find('war') != -1: #find方法用來找出給定字符串在另一個字符串中的位置,或者返回-1以表示找不到子字符串。
print 'Yes, it contains the string "war"'
delimiter = '_*_'
mylist = ['Brazil', 'Russia', 'India', 'China']
print delimiter.join(mylist)