Python 基礎操作列表、字符串

Python數據結構的基礎操作

 

列表

'''
Created on 2013-7-22

@author: dream
'''

#修改列表成員的值
lb=[1,2,3]
lb[1]=55
print(lb[0:3])

#刪除成員
del lb[1]


print(len(lb))

li=list("Hell,Python")
li[5:5]=['3','2','1']
print(li)

li[10:]=list("python")
print(li)

#分片刪除元素
del li[10:]
print(li)

'''
Created on 2013-7-22

@author: dream
'''

#append 方法添加一個元素到列表
lst=list("a")
lst.append("q")
print(lst)
#統計一個元素在列表中出現的次數
print(lst.count("q"))

#extend 在當前列表的末尾一次性追加另一個列表的多個值
lst.extend([1,2,3,4,5,6][0:3])
print(lst)

#index
print(lst.index('q'))

#insert
lst.insert(1, ['A','B','C'])
print(lst)
print(lst.remove("a"))

'''
Created on 2013-7-22

@author: dream
'''

database=[
    ["Tom","123"],
    ["Mary","4548"],
    ["Jim","jim"]
]

username=input("請輸入授權用戶名:")
password=input("請輸入授權密碼:")
if [username,password] in database:
    print("驗證通過!")
else:
    print("驗證失敗!")



 

字符串:

'''
Created on 2013-7-19

@author: dream
'''

str1='"hello,world"'
str2="'hello,world'"
str3="hello,world"
str4="\"" #轉義字符
str5="\'" #轉義字符
str6="\\"
str7="aaa'" 'bbbb' #字符串拼接
i=789
lng=78999999999999999999999999999999999999999999999999999999999999999
#str 和repr 字符串轉換函數 ,在早期python 版本中使用``來轉換字符串

#長字符串表示
longString='''
fwefwefjowef
wef

yj

yj\"
'''
#原始字符串 轉義符號會被顯示出來,原始字符串最後一個不能是\
str8=r"\n\n\n\n\n\n"
print(str8)

print(str1)
print(str2)
print(str3)
print(str4)
print(str5)
print(str6)
print(str7)
print(str(i))
print(repr(lng))
print(longString)
#input 與raw_input
rd1=input("input")
print(rd1)

'''
Created on 2013-7-22

@author: dream
'''

#格式化字符串
str1="Hello,Python,%s"
print(str1 % ("你好"))

#格式化實數
str2="%.3f"
print(str2 % 1.2365)

str3="Python%s---%s---%s"
print(str3 % (1,2,3))

print(str3.find("an"))



 

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