ds_using_list.py
# This is my shopping list
# list is mutable but string is immutable
# create a list using list = []
# at least has append() sort() method
# want to see more,please help(list)
# list is a sequence
shoplist = ['apple','mango','carrot','banana']
print('I have', len(shoplist), 'items to purchase.')
print('These items are:',end=' ')
for item in shoplist:
print(item, end=' ')
print('\nI also have to buy rice.')
shoplist.append('rice')
print('Now my shopping list is',shoplist)
print('I will sort my list now')
shoplist.sort()
print('Sorted shop list is',shoplist)
print('The first item I will buy is', shoplist[0])
olditem = shoplist[0] # list取元素是用list[Number]
del shoplist[0]
print('I bought the', olditem)
print('My shop list is now',shoplist)
ds_using_tuple.py
# Tuple 可以近似看做列表,但是元組功能沒有列表豐富
# 元組類似于字符串 所以 Tuple is immutable
# create a tuple tuple = ()
# tuple is a sequence
zoo = ('python','elephant','penguin')
print('Number of animals in the zoo is', len(zoo))
new_zoo = ('monkey','camel',zoo)
print('Number of cages in the new zoo is', len(new_zoo))
print('All animals in new zoo are', new_zoo)
print('Animals brought from old zoo are',new_zoo[2]) #Tuple取元素也是 tuple[Number]
print('Last animal brought from old zoo is',new_zoo[2][2])
print('Number of animals in the new zoo is',len(new_zoo)-1+len(new_zoo[2]))
# 空的元組由一堆圓括號構(gòu)成 myempty = ()
# 但是只擁有一個項目的元組 必須在第一個(唯一一個)項目后面加上一個逗號
# 這是為了區(qū)別這到底是一個元組 還是 只是一個被括號所環(huán)繞的對象
# example:singleton = (2,) sigleton is a tuple object
# singleton = (2) sigleton is a int object
ds_using_dict.py
# 字典將鍵值(Keys)與值(Values)聯(lián)系到一起
# 鍵值必須是唯一的,而且鍵值必須是不可變(immutable)的對象(str)
# 值可以是可變或者不可變的對象
# create a dictionary dict = {key1:value1,key2:value2}
# 成對的鍵值-值不會排序
# 字典是屬于dict類下的實例
# 字典不是序列
# 字典只能根據(jù)key來確定
# ab作為Address Book縮寫
ab = {
'Swaroop':'swaroop@swaroopch.com',
'Larry':'larry@wall.org',
'Matsumoto':'matz@ruby-lang.org',
'Spammer':'spammer@hotmail.com'
}
# use key to find its value
print("Swaroop's address is",ab['Swaroop'])
# delete a pair of key-value
del ab['Spammer']
print('\nThere are {} contacts in the address-book\n'.format(len(ab)))
for name,address in ab.items(): #items() 返回一個包含元組的列表
print('Contact {} at {}'.format(name,address))
# add a pair of key-value
ab['Guido'] = 'guido@python.org'
if 'Guido' in ab: #in運算符 可以來檢查某對鍵值-值配對是否存在
print("\nGuido's address is", ab['Guido'])
# 了解更多的有關(guān)dict類的方法 help(dict)
# 其實在定義函數(shù)的參數(shù)列表時躏筏,就是指定了鍵值-值配對
ds_seq.py
# 列表(list) 元組(tuple) 字符串(str) 都可以看做是序列(Sequence)的某種表現(xiàn)形式
# 序列(Sequence)的主要功能是資格測試 也就是 in and not in 和 索引操作
# 序列還有切片(Slicing)運算符 它使我們能夠獲得序列的一部分
shoplist = ['apple','mango','carrot','banana']
name = 'swaroop'
# 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])
print('Character 0 is', name[0])
# Slicing on a list
# 切片中冒號(:)是必須的,但是數(shù)字是可選的
# 第一個數(shù)字是切片開始的位置梢莽,第二個數(shù)字是切片結(jié)束的位置。
# 如果第一個數(shù)字沒有指定,Python將會從序列起始處開始操作
# 如果第二個數(shù)字留空捂贿,Python將會在序列的末尾結(jié)束操作
# 切片操作將包括起始位置捞附,但不包括結(jié)束位置
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]) #不包括-1
print('Item start to end is', shoplist[:])
# Slice on a character
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[:])
# Slice 第三個參數(shù)是Step 默認為1
print(shoplist[::1])
print(shoplist[::2]) #得到第0 2 4 ...個項目
print(shoplist[::3]) #得到第0 3 6 ...個項目
print(shoplist[::-1]) #得到第-1 -2 -3 ...個項目巾乳,即逆序了
print(shoplist[::-2]) #得到第-1 -3 -5 ...個項目
print(shoplist[::-3]) #得到第-1 -4 -7 ...個項目
ds_set.py
# 集合(Set)是無序容器(Collection)
# 當容器中的項目是否存在比起次序,即出現(xiàn)次數(shù)更重要的我們就會使用集合
# 集合可以測試某些對象的資格 檢查他們是否是其他集合的子集
# 找到兩個集合的交集等
bri = set(['brazil','russia','india'])
print('Is India in bri?', 'india' in bri)
print('Is usa in bri?', 'usa' in bri)
bric = bri.copy()
bric.add('China')
print('Is bric the superset of bri?',bric.issuperset(bri))
bri.remove('russia')
# bri & bric
print("bri and bric 's intersection is", bri.intersection(bric))
ds_reference.py
# 創(chuàng)建了一個對象以后并將其分配給某個變量后 變量會查閱Refer該對象
# 但是變量不會代表對象本身
# 變量名指向計算機內(nèi)存中存儲了該對象的那一部分
# 即將名稱綁定Binding給那個對象
print('Simple Assignment')
shoplist = ['apple', 'mango', 'carrot', 'banana']
# mylist 只是指向同一對象的另一種名稱
mylist = shoplist
# 將第一項刪掉
del shoplist[0]
print('shoplist is', shoplist)
print('mylist is', mylist)
# shoplist 和 mylist 二者都沒有了apple
# 因此我們可以確定它們指向了同一個對象
print('Copy by making a full slice')
# 通過切片來制作一份副本
mylist = shoplist[:]
#再次刪除第一個項目
del mylist[0]
print('shoplist is', shoplist)
print('mylist is', mylist)
# 現(xiàn)在他們出現(xiàn)了不同
# 因此如果你希望創(chuàng)建一份序列的副本鸟召,那么必須用切片操作胆绊。
# 僅僅將一個變量名賦予給另一個名稱,那么它們都將查閱同一個對象欧募。
ds_str_method.py
# str更多方法可以help(str)
name = 'Swaroop'
if name.startswith('Swa'): #startswith()用于查找字符串是否以給定的字符串內(nèi)容開頭
print('Yes, the string starts with "Swa"')
if 'a' in name: #in運算符用以檢查給定的字符串是否是查詢的字符串中的一部分
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)) #join()用以聯(lián)結(jié)序列中的項目跟继,其中字符串會作為每一項目間的
#分隔符种冬,并以此返回一串更大的字符串