python學習筆記——4_數據結構

#coding:UTF-8
#列表開始
#help(list)

shopList=['apple','mango','carrot','banana']
print 'I have',len(shopList),'items to purchase'

print 'These items are:'
for item in shopList:
    print item

print '\n I also have to buy rice.'
shopList.append('rice')
print'My shopping list is now',shopList

print '\nI will sort my list now'
shopList.sort()
print 'Sorted shopping list is',shopList

print '\nI would not want to buy apple'
del shopList[0]
print 'At last,my shopping list is',shopList

'''
結果
I have 4 items to purchase
These items are:
apple
mango
carrot
banana

 I also have to buy rice.
My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice']

I will sort my list now
Sorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice']

I would not want to buy apple
At last,my shopping list is ['banana', 'carrot', 'mango', 'rice']
'''

#列表結束

#==================================================

#元組開始

'''
元組和列表十分類似,只不過元組和字符串一樣是 不可變的 即你不能修改元組。
元組通過圓括號中用逗號分割的項目定義。
元組通常用在使語句或用戶定義的函數能夠安全地採用一組值的時候,即被使用的元組的值不會改變。
'''
zoo=('python','wolf','tiger')
print 'Number of animals in the zoo is',len(zoo)

new_zoo=('monkey','dolphin',zoo)
print 'Number of animals int the new_zoo is',len(new_zoo)

print 'All the animals in the new_zoo is',new_zoo
print 'Animals brought from old zoo are',new_zoo[2]
print 'Last animal brought from old zoo is',new_zoo[2][2]

'''
結果
Number of animals in the zoo is 3
Number of animals int the new_zoo is 3
All the animals in the new_zoo is ('monkey', 'dolphin', ('python', 'wolf', 'tiger'))
Animals brought from old zoo are ('python', 'wolf', 'tiger')
Last animal brought from old zoo is tiger
'''

'''
含有0個或1個項目的元組。一個空的元組由一對空的圓括號組成,如myempty = ()。
然而,含有單個元素的元組就不那麼簡單了。
你必須在第一個(唯一一個)項目後跟一個逗號,這樣Python才能區分元組
和表達式中一個帶圓括號的對象。
即如果你想要的是一個包含項目2的元組的時候,你應該指明singleton = (2 , )。
'''


'''
print語句可以使用跟着%符號的項目元組的字符串。
這些字符串具備定製的功能。定製讓輸出滿足某種特定的格式。
定製可以是%s表示字符串或%d表示整數。
print的這個用法使得編寫輸出變得極其簡單,它避免了許多字符串操作。
它也避免了我們一直以來使用的逗號。
在第二個print語句中,我們使用了一個定製,後面跟着%符號後的單個項目——沒有圓括號。
這隻在字符串中只有一個定製的時候有效。
'''
age=22
name="sky"

print '%s is %d years old'%(name,age)
print 'Why is %s playing with that python?'%name
print '\n'
'''
結果
sky is 22 years old
Why is sky playing with that python?
'''
#元組結束

#=====================================================

#字典開始
#help(dict)

ab={u'小李':'[email protected]',
    u'小紅':'[email protected]',
    u'小黑':'[email protected]',
    u'小王':'[email protected]'
    }
print ab
print u'小王的郵箱爲%s'% ab[u'小王']
ab[u'小趙']='[email protected]'
print ab
del ab[u'小李']
print ab
for name,address in ab.items():
    print u'%s的地址爲%s' % (name,address)
if u'小黑' in ab:#or ab.has_key(u'小黑')
    print u'小黑的地址爲%s'% ab[u'小黑']
print '\n'
'''
結果
{u'\u5c0f\u674e': '[email protected]', u'\u5c0f\u738b': '[email protected]', u'\u5c0f\u7ea2': '[email protected]', u'\u5c0f\u9ed1': '[email protected]'}
小王的郵箱爲[email protected]
{u'\u5c0f\u674e': '[email protected]', u'\u5c0f\u8d75': '[email protected]', u'\u5c0f\u738b': '[email protected]', u'\u5c0f\u7ea2': '[email protected]', u'\u5c0f\u9ed1': '[email protected]'}
{u'\u5c0f\u8d75': '[email protected]', u'\u5c0f\u738b': '[email protected]', u'\u5c0f\u7ea2': '[email protected]', u'\u5c0f\u9ed1': '[email protected]'}
小趙的地址爲[email protected]
小王的地址爲[email protected]
小紅的地址爲[email protected]
小黑的地址爲[email protected]
小黑的地址爲[email protected]
'''
#字典結束

#=====================================================

#序列開始

'''
列表、元組和字符串都是序列,但是序列是什麼,它們爲什麼如此特別呢?
序列的兩個主要特點是索引操作符和切片操作符。
索引操作符讓我們可以從序列中抓取一個特定項目。
切片操作符讓我們能夠獲取序列的一個切片,即一部分序列。
'''

fruitList=['apple','banana','pear','watermelon']
print 'Item 0 is ',fruitList[0]
print 'Item -1 is ',fruitList[-1]
print 'Item -2 is ',fruitList[-2]
#print 'Item 4 is',fruitList[4] list index out of range

print 'Item 1 to 3 is',fruitList[1:3]
print 'Item 2 to end is',fruitList[2:]
print 'Item 1 to -1 is',fruitList[1:-1]
print 'Item start to end is',fruitList[:]

name='abcdefg'

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[:]
print 'characters 1 to 0 is',name[1:0] #空
print 'characters start to -1 is',name[:-1]
'''
結果
Item 0 is  apple
Item -1 is  watermelon
Item -2 is  pear
Item 1 to 3 is ['banana', 'pear']
Item 2 to end is ['pear', 'watermelon']
Item 1 to -1 is ['banana', 'pear']
Item start to end is ['apple', 'banana', 'pear', 'watermelon']
characters 1 to 3 is bc
characters 2 to end is cdefg
characters 1 to -1 is bcdef
characters start to end is abcdefg
characters 1 to 0 is 
characters start to -1 is abcdef
'''

'''
注意
Python從0開始計數
索引同樣可以是負數,在那樣的情況下,位置是從序列尾開始計算的。
因此,shoplist[-1]表示序列的最後一個元素而shoplist[-2]抓取序列的倒數第二個項目。

切片操作符是序列名後跟一個方括號,方括號中有一對可選的數字,並用冒號分割。
記住數是可選的,而冒號是必須的。
返回的序列從開始位置 開始 ,剛好在 結束 位置之前結束。
即開始位置是包含在序列切片中的,而結束位置被排斥在切片外。
'''
#序列結束

#=============================================================

#參考開始

'''
如果你想要複製一個列表或者類似的序列或者其他複雜的對象(不是如整數那樣的簡單 對象 ),
那麼你必須使用切片操作符來取得拷貝。
如果你只是想要使用另一個變量名,兩個名稱都 參考 同一個對象,
那麼如果你不小心的話,可能會引來各種麻煩。
'''

print '\nSimple Assignment'
a=2;
b=a;
a=3;
print a,b

shopList=['apple','mango','pear','watermelon']
myList=shopList
print 'shopList is',shopList
print 'myList is',myList

del shopList[0]
print 'shopList is',shopList
print 'myList is',myList

myList=shopList[:]
del myList[0]
myList[0]='cabage'
shopList.append('orange')
print 'shopList is',shopList
print 'myList is',myList

'''
結果
Simple Assignment
3 2
shopList is ['apple', 'mango', 'pear', 'watermelon']
myList is ['apple', 'mango', 'pear', 'watermelon']
shopList is ['mango', 'pear', 'watermelon']
myList is ['mango', 'pear', 'watermelon']
shopList is ['mango', 'pear', 'watermelon', 'orange']
myList is ['cabage', 'watermelon']
'''



#參考結束

#=========================================================

#字符串 開始
#help(str)

print '\n'
name="Swaroop"
if name.startswith('Swa'):
    print'Yes,%s start with "Swa"'% name
if 'a' in name:
    print 'Yes,a is in',name
if name.find('war')!=-1:
    print 'Yes,it contains the string "war"'
delimiter ="_*_"
myList=('a','b','c')
print delimiter.join(myList) #str類也有以一個作爲分隔符的字符串join序列的項目的整潔的方法,它返回一個生成的大字符串

'''
結果
Yes,Swaroop start with "Swa"
Yes,a is in Swaroop
Yes,it contains the string "war"
a_*_b_*_c
'''

#字符串 結束

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章